1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
|
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.tomcat.util.xreflection;
import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.net.InetAddress;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import org.apache.tomcat.util.IntrospectionUtils;
public final class ObjectReflectionPropertyInspector {
public static void main(String... args) throws Exception {
if (args.length == 0) {
System.err.println("Usage:\n\t" + "org.apache.tomcat.util.xreflection.ObjectReflectionPropertyInspector" +
" <destination directory>");
System.exit(1);
}
File outputDir = new File(args[0]);
if (!outputDir.exists() || !outputDir.isDirectory()) {
System.err.println("Invalid output directory: " + outputDir.getAbsolutePath());
System.exit(1);
}
Set<SetPropertyClass> baseClasses =
getKnownClasses().stream().map(ObjectReflectionPropertyInspector::processClass)
.collect(Collectors.toCollection(LinkedHashSet::new));
generateCode(baseClasses, "org.apache.tomcat.util", outputDir, "XReflectionIntrospectionUtils");
}
private static Set<Class<?>> getKnownClasses() throws ClassNotFoundException {
return Collections.unmodifiableSet(new LinkedHashSet<>(Arrays.asList(
Class.forName("org.apache.catalina.authenticator.jaspic.SimpleAuthConfigProvider"),
Class.forName("org.apache.catalina.authenticator.jaspic.PersistentProviderRegistrations$Property"),
Class.forName("org.apache.catalina.authenticator.jaspic.PersistentProviderRegistrations$Provider"),
Class.forName("org.apache.catalina.connector.Connector"),
Class.forName("org.apache.catalina.core.AprLifecycleListener"),
Class.forName("org.apache.catalina.core.ContainerBase"),
Class.forName("org.apache.catalina.core.StandardContext"),
Class.forName("org.apache.catalina.core.StandardEngine"),
Class.forName("org.apache.catalina.core.StandardHost"),
Class.forName("org.apache.catalina.core.StandardServer"),
Class.forName("org.apache.catalina.core.StandardService"),
Class.forName("org.apache.catalina.filters.AddDefaultCharsetFilter"),
Class.forName("org.apache.catalina.filters.RestCsrfPreventionFilter"),
Class.forName("org.apache.catalina.loader.ParallelWebappClassLoader"),
Class.forName("org.apache.catalina.loader.WebappClassLoaderBase"),
Class.forName("org.apache.catalina.realm.UserDatabaseRealm"),
Class.forName("org.apache.catalina.valves.AccessLogValve"),
Class.forName("org.apache.coyote.AbstractProtocol"),
Class.forName("org.apache.coyote.ajp.AbstractAjpProtocol"),
Class.forName("org.apache.coyote.ajp.AjpAprProtocol"),
Class.forName("org.apache.coyote.ajp.AjpNio2Protocol"),
Class.forName("org.apache.coyote.ajp.AjpNioProtocol"),
Class.forName("org.apache.coyote.http11.AbstractHttp11JsseProtocol"),
Class.forName("org.apache.coyote.http11.AbstractHttp11Protocol"),
Class.forName("org.apache.coyote.http11.Http11AprProtocol"),
Class.forName("org.apache.coyote.http11.Http11Nio2Protocol"),
Class.forName("org.apache.coyote.http11.Http11NioProtocol"),
Class.forName("org.apache.tomcat.util.descriptor.web.ContextResource"),
Class.forName("org.apache.tomcat.util.descriptor.web.ResourceBase"),
Class.forName("org.apache.tomcat.util.modeler.AttributeInfo"),
Class.forName("org.apache.tomcat.util.modeler.FeatureInfo"),
Class.forName("org.apache.tomcat.util.modeler.ManagedBean"),
Class.forName("org.apache.tomcat.util.modeler.OperationInfo"),
Class.forName("org.apache.tomcat.util.modeler.ParameterInfo"),
Class.forName("org.apache.tomcat.util.net.AbstractEndpoint"),
Class.forName("org.apache.tomcat.util.net.AprEndpoint"),
Class.forName("org.apache.tomcat.util.net.Nio2Endpoint"),
Class.forName("org.apache.tomcat.util.net.NioEndpoint"),
Class.forName("org.apache.tomcat.util.net.SocketProperties"))));
}
// types of properties that IntrospectionUtils.setProperty supports
private static final Set<Class<?>> ALLOWED_TYPES = Collections.unmodifiableSet(
new LinkedHashSet<>(Arrays.asList(Boolean.TYPE, Integer.TYPE, Long.TYPE, String.class, InetAddress.class)));
private static final Map<Class<?>,SetPropertyClass> classes = new LinkedHashMap<>();
public static void generateCode(Set<SetPropertyClass> baseClasses, String packageName, File location,
String className) throws Exception {
String packageDirectory = packageName.replace('.', '/');
File sourceFileLocation = new File(location, packageDirectory);
ReflectionLessCodeGenerator.generateCode(sourceFileLocation, className, packageName, baseClasses);
}
private static boolean isAllowedField(Field field) {
return ALLOWED_TYPES.contains(field.getType()) && !Modifier.isFinal(field.getModifiers());
}
private static boolean isAllowedSetMethod(Method method) {
return method.getName().startsWith("set") && method.getParameterTypes().length == 1 &&
ALLOWED_TYPES.contains(method.getParameterTypes()[0]) && !Modifier.isPrivate(method.getModifiers());
}
private static boolean isAllowedGetMethod(Method method) {
return (method.getName().startsWith("get") || method.getName().startsWith("is")) &&
method.getParameterTypes().length == 0 && ALLOWED_TYPES.contains(method.getReturnType()) &&
!Modifier.isPrivate(method.getModifiers()) && isPresentInJava8Api(method);
}
private static boolean isPresentInJava8Api(Method method) {
// Up to Java 18 EA 30 this was the only problematic method so this
// approach is OK. If we get more than a few of these this code may
// need to be refactored.
if (ClassLoader.class.isAssignableFrom(method.getDeclaringClass())) {
if ("getName".equals(method.getName())) {
return false;
}
}
return true;
}
private static SetPropertyClass getOrCreateSetPropertyClass(Class<?> clazz) {
boolean base = (clazz.getSuperclass() == null || clazz.getSuperclass() == Object.class);
SetPropertyClass spc = classes.get(clazz);
if (spc == null) {
spc = new SetPropertyClass(clazz, base ? null : getOrCreateSetPropertyClass(clazz.getSuperclass()));
classes.put(clazz, spc);
}
return spc;
}
static Method findGetter(Class<?> declaringClass, String propertyName) {
for (String getterName : Arrays.asList("get" + IntrospectionUtils.capitalize(propertyName),
"is" + propertyName)) {
try {
Method method = declaringClass.getMethod(getterName);
if (!Modifier.isPrivate(method.getModifiers())) {
return method;
}
} catch (NoSuchMethodException e) {
// Ignore
}
}
try {
Method method = declaringClass.getMethod("getProperty", String.class, String.class);
if (!Modifier.isPrivate(method.getModifiers())) {
return method;
}
} catch (NoSuchMethodException e) {
// Ignore
}
return null;
}
static Method findSetter(Class<?> declaringClass, String propertyName, Class<?> propertyType) {
try {
Method method = declaringClass.getMethod("set" + IntrospectionUtils.capitalize(propertyName), propertyType);
if (!Modifier.isPrivate(method.getModifiers())) {
return method;
}
} catch (NoSuchMethodException e) {
// Ignore
}
try {
Method method = declaringClass.getMethod("setProperty", String.class, String.class);
if (!Modifier.isPrivate(method.getModifiers())) {
return method;
}
} catch (NoSuchMethodException e) {
// Ignore
}
return null;
}
static String decapitalize(String name) {
if (name == null || name.isEmpty()) {
return name;
}
if (name.length() > 1 && Character.isUpperCase(name.charAt(1)) && Character.isUpperCase(name.charAt(0))) {
return name;
}
char[] chars = name.toCharArray();
chars[0] = Character.toLowerCase(chars[0]);
return new String(chars);
}
static SetPropertyClass processClass(Class<?> clazz) {
SetPropertyClass spc = getOrCreateSetPropertyClass(clazz);
final Method[] methods = clazz.getDeclaredMethods();
for (Method method : methods) {
if (isAllowedSetMethod(method)) {
String propertyName = decapitalize(method.getName().substring(3));
Class<?> propertyType = method.getParameterTypes()[0];
Method getter = findGetter(clazz, propertyName);
Method setter = findSetter(clazz, propertyName, propertyType);
ReflectionProperty property =
new ReflectionProperty(spc.getClazz().getName(), propertyName, propertyType, setter, getter);
spc.addProperty(property);
} else if (isAllowedGetMethod(method)) {
boolean startsWithIs = method.getName().startsWith("is");
String propertyName = decapitalize(method.getName().substring(startsWithIs ? 2 : 3));
Class<?> propertyType = method.getReturnType();
Method getter = findGetter(clazz, propertyName);
Method setter = findSetter(clazz, propertyName, propertyType);
ReflectionProperty property =
new ReflectionProperty(spc.getClazz().getName(), propertyName, propertyType, setter, getter);
spc.addProperty(property);
}
}
final Field[] fields = clazz.getDeclaredFields();
for (Field field : fields) {
if (isAllowedField(field)) {
Method getter = findGetter(field.getDeclaringClass(), IntrospectionUtils.capitalize(field.getName()));
Method setter = findSetter(field.getDeclaringClass(), IntrospectionUtils.capitalize(field.getName()),
field.getType());
ReflectionProperty property = new ReflectionProperty(spc.getClazz().getName(), field.getName(),
field.getType(), setter, getter);
spc.addProperty(property);
}
}
if (!spc.isBaseClass()) {
SetPropertyClass parent = getOrCreateSetPropertyClass(spc.getClazz().getSuperclass());
parent.addSubClass(spc);
return processClass(parent.getClazz());
} else {
return spc;
}
}
}
|