-
Notifications
You must be signed in to change notification settings - Fork 4
/
ReflectUtils.java
168 lines (138 loc) · 7 KB
/
ReflectUtils.java
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
package org.otaibe.commons.quarkus.reflection.utils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.graalvm.nativeimage.hosted.RuntimeReflection;
import org.reflections.ReflectionUtils;
import org.reflections.Reflections;
import org.reflections.scanners.SubTypesScanner;
import org.reflections.scanners.TypeAnnotationsScanner;
import java.lang.annotation.*;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.WildcardType;
import java.util.*;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.function.Consumer;
import java.util.stream.Stream;
public class ReflectUtils {
public static final Collection<Class<?>> cache = new ConcurrentLinkedQueue<>();
public static Stream<Class<?>> getAllClassesFromPackage(String packageName, Class... baseClasses) {
if (StringUtils.isBlank(packageName) || ArrayUtils.isEmpty(baseClasses)) {
return new ArrayList<Class<?>>().stream();
}
Reflections reflections = new Reflections(packageName, new SubTypesScanner(false));
return Arrays.stream(baseClasses)
.flatMap(aClass -> reflections.getSubTypesOf(aClass).stream())
.filter(aClass -> !((Class) aClass).isInterface())
.filter(aClass -> ((Class) aClass).getPackage().getName().equals(packageName));
}
public static Stream<Class<?>> getAllAnnotatedClassesFromPackage(String packageName, Class<? extends Annotation>... annotations) {
if (StringUtils.isBlank(packageName) || ArrayUtils.isEmpty(annotations)) {
return new ArrayList<Class<?>>().stream();
}
Reflections reflections = new Reflections(packageName,
new SubTypesScanner(false),
new TypeAnnotationsScanner()
);
return Arrays.stream(annotations)
.flatMap(aClass -> reflections.getTypesAnnotatedWith(aClass).stream())
.filter(aClass -> !((Class) aClass).isInterface())
;
}
public static void registerAdditionalClassesForReflection(String packageName) {
registerAdditionalClassesForReflection(packageName, aClass -> ReflectUtils.registerForReflectionClass(aClass));
}
public static void registerAdditionalClassesForReflection(String packageName, Consumer<Class<?>> registerConsumer) {
getAllAnnotatedClassesFromPackage(packageName, AdditionalClassesForReflection.class)
.forEach(aClass -> {
Arrays.stream(aClass.getAnnotation(AdditionalClassesForReflection.class).targets())
.forEach(registerConsumer);
registerClassMethodReturnTypes(aClass, registerConsumer);
});
}
public static void registerClassMethodReturnTypes(Class<?> aClass, Consumer<Class<?>> registerConsumer) {
ReflectionUtils.getAllMethods(aClass, ReflectionUtils.withModifier(Modifier.PUBLIC))
.stream()
.filter(method -> {
Class<?> returnType = method.getReturnType();
return !returnType.isPrimitive();
})
.flatMap(method -> {
//log.info("method name: {}", method.getName());
Class<?> returnType = method.getReturnType();
Type type = method.getGenericReturnType();
Optional<ParameterizedType> parameterizedType1 = Optional.ofNullable(type)
.filter(ParameterizedType.class::isInstance)
.map(ParameterizedType.class::cast);
if (parameterizedType1.isPresent()) {
ParameterizedType parameterizedType = parameterizedType1.get();
//log.info("parameterizedType: {}", parameterizedType.getTypeName());
return Arrays.stream(parameterizedType.getActualTypeArguments())
.filter(type1 -> !WildcardType.class.isInstance(type1))
.filter(type1 -> !ParameterizedType.class.isInstance(type1))
.map(type1 -> (Class<?>) type1);
}
return Arrays.asList(returnType).stream();
})
.forEach(registerConsumer);
}
public static void registerPackage(String packageName, Class... baseClasses) {
getAllClassesFromPackage(packageName, baseClasses)
.forEach(aClass -> ReflectUtils.registerForReflectionClass(aClass));
}
public static void registerPackage(String packageName, Consumer<Class<?>> registerConsumer, Class<?>... baseClasses) {
getAllClassesFromPackage(packageName, baseClasses).forEach(registerConsumer);
}
public static void registerForReflectionClass(Class<?> clazz) {
registerForReflectionClass(false, clazz);
}
public static void registerForReflectionClass(boolean finalIsWritable, Class<?> clazz) {
try {
if (cache.contains(clazz)) {
return;
}
Arrays.stream(clazz.getMethods())
.filter(method -> method.isAccessible())
.forEach(method -> RuntimeReflection.register(method));
Arrays.stream(clazz.getFields())
.filter(field -> field.isAccessible())
.forEach(field -> RuntimeReflection.register(finalIsWritable, field));
for (Class<?> clazz1 = clazz; !Object.class.equals(clazz1); clazz1 = clazz1.getSuperclass()) {
if (clazz.isPrimitive() || clazz1.isInterface() || void.class.equals(clazz1)) {
break;
}
RuntimeReflection.register(clazz1.getDeclaredMethods());
RuntimeReflection.register(finalIsWritable, clazz1.getDeclaredFields());
RuntimeReflection.register(clazz1);
RuntimeReflection.registerForReflectiveInstantiation(clazz1);
}
} catch (Exception e) {
} finally {
cache.add(clazz);
}
}
public static List<Class<?>> extractParents(Class<?> clazz) {
List<Class<?>> result = new ArrayList<>();
for (Class<?> clazz1 = clazz; !Object.class.equals(clazz1); clazz1 = clazz1.getSuperclass()) {
if (clazz.isPrimitive() || clazz1.isInterface() || void.class.equals(clazz1)) {
break;
}
result.add(clazz1);
}
return result;
}
/**
* Annotation that can be used to determine additional classes for reflection
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public static @interface AdditionalClassesForReflection {
/**
* Alternative classes that should actually be registered for reflection.
*
* This allows for classes in 3rd party libraries to be registered without modification.
*/
Class<?>[] targets() default {};
}
}