-
Notifications
You must be signed in to change notification settings - Fork 63
/
JqwikReflectionSupport.java
452 lines (404 loc) · 15.3 KB
/
JqwikReflectionSupport.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
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
package net.jqwik.engine.support;
import java.io.*;
import java.lang.annotation.*;
import java.lang.reflect.*;
import java.nio.file.*;
import java.util.*;
import java.util.function.*;
import java.util.logging.*;
import java.util.stream.*;
import org.junit.platform.commons.support.*;
import net.jqwik.api.*;
import net.jqwik.api.providers.*;
import net.jqwik.api.support.*;
import static java.util.stream.Collectors.*;
import static net.jqwik.engine.support.OverriddenMethodAnnotationSupport.*;
public class JqwikReflectionSupport {
private static final Logger LOG = Logger.getLogger(JqwikReflectionSupport.class.getName());
@SuppressWarnings("deprecation") // Deprecated as of Java 9
private static <T extends AccessibleObject> T makeAccessible(T object) {
if (!object.isAccessible()) {
object.setAccessible(true);
}
return object;
}
/**
* Create instance of a class that can potentially be a non static inner class
*
* @param <T> The type of the instance to create
* @param clazz The class to instantiate
* @return the newly created instance
*/
public static <T> T newInstanceWithDefaultConstructor(Class<T> clazz) {
if (isInnerClass(clazz)) {
Object parentInstance = newInstanceWithDefaultConstructor(clazz.getDeclaringClass());
return ReflectionSupport.newInstance(clazz, parentInstance);
} else {
return ReflectionSupport.newInstance(clazz);
}
}
/**
* Create instance of a class that can potentially be a non static inner class
*
* @param clazz The class to instantiate
* @return all newly created instances with from most outer to most inner
*/
public static List<Object> newInstancesWithDefaultConstructor(Class<?> clazz) {
if (isInnerClass(clazz)) {
List<Object> instances = newInstancesWithDefaultConstructor(clazz.getDeclaringClass());
Object inner = ReflectionSupport.newInstance(clazz, instances.get(instances.size() - 1));
instances.add(inner);
return instances;
} else {
List<Object> instances = new ArrayList<>();
instances.add(ReflectionSupport.newInstance(clazz));
return instances;
}
}
/**
* Create instance of a class that can potentially be a non static inner class
* and its outer instance might be {@code context}
*
* @param <T> The type of the instance to create
* @param clazz The class to instantiate
* @param context The potential context instance
* @return the newly created instance
*/
public static <T> T newInstanceInTestContext(Class<T> clazz, Object context) {
if (!isInnerClass(clazz)) {
return ReflectionSupport.newInstance(clazz);
}
Class<?> outerClass = clazz.getDeclaringClass();
Object parentInstance = outerClass.isAssignableFrom(context.getClass()) ?
context : newInstanceWithDefaultConstructor(outerClass);
try {
Constructor<T> constructor = clazz.getDeclaredConstructor(outerClass);
return newInstance(constructor, parentInstance);
} catch (NoSuchMethodException e) {
return JqwikExceptionSupport.throwAsUncheckedException(e);
}
}
public static <T> T newInstance(Constructor<T> constructor, Object... args) {
try {
return makeAccessible(constructor).newInstance(args);
} catch (Throwable t) {
return JqwikExceptionSupport.throwAsUncheckedException(t);
}
}
/**
* Find all {@linkplain Method methods} as in ReflectionSupport.findMethods(..) but also use outer classes to look for
* methods.
*
* <p>
* Duplicate methods (through) inheritance are de-duplicated. The first occurrence of a method is kept.
* </p>
*
* @param clazz The class in which you start the search
* @param predicate The condition to check for all candidate methods
* @param traversalMode Traverse hierarchy up or down. Determines the order in resulting list.
* @return List of found methods
*/
public static List<Method> findMethodsPotentiallyOuter(
Class<?> clazz,
Predicate<Method> predicate,
HierarchyTraversalMode traversalMode
) {
List<Class<?>> searchClasses = getDeclaringClasses(clazz, traversalMode);
Set<Method> foundMethods = new LinkedHashSet<>();
for (Class<?> searchClass : searchClasses) {
foundMethods.addAll(ReflectionSupport.findMethods(searchClass, predicate, traversalMode));
}
return new ArrayList<>(foundMethods);
}
/**
* Find all {@linkplain Field field} but also use outer classes to look for
* methods.
*
* @param clazz The class in which you start the search
* @param predicate The condition to check for all candidate methods
* @param traversalMode Traverse hierarchy up or down. Determines the order in resulting list.
* @return List of found fields
*/
public static List<Field> findFieldsPotentiallyOuter(
Class<?> clazz,
Predicate<Field> predicate,
HierarchyTraversalMode traversalMode
) {
List<Class<?>> searchClasses = getDeclaringClasses(clazz, traversalMode);
List<Field> foundFields = new ArrayList<>();
for (Class<?> searchClass : searchClasses) {
foundFields.addAll(ReflectionSupport.findFields(searchClass, predicate, traversalMode));
}
return foundFields;
}
/**
* Read a field's value as in ReflectionSupport.getField(..) but potentially use outer instances if the field belongs to an inner class.
*
* @param field The field to read
* @param targetInstances The container instances to read the field from, from outermost to innermost
* @return The value of the field
*/
public static Object readFieldOnContainer(Field field, List<Object> targetInstances) {
makeAccessible(field);
return readField(field, new ArrayDeque<>(targetInstances));
}
private static Object readField(Field field, Deque<Object> instances) {
Object target = instances.removeLast();
List<Field> declaredFields = Arrays.stream(target.getClass().getDeclaredFields()).collect(toList());
if (declaredFields.contains(field)) {
try {
return field.get(target);
} catch (Exception exception) {
return JqwikExceptionSupport.throwAsUncheckedException(exception);
}
} else {
if (instances.isEmpty()) {
String message = String.format("Cannot access value of field %s", field);
throw new JqwikException(message);
}
return readField(field, instances);
}
}
/**
* Set a field's value as in ReflectionSupport.setField(..) but potentially use outer instances if the field belongs to an inner class.
*
* @param field The field to set
* @param value The value to set in the field
* @param targetInstances The container instances to set the field on, from outermost to innermost
*/
public static void setFieldOnContainer(Field field, Object value, List<Object> targetInstances) {
makeAccessible(field);
setField(field, value, new ArrayDeque<Object>(targetInstances));
}
private static void setField(Field field, Object value, Deque<Object> instances) {
Object target = instances.removeLast();
List<Field> declaredFields = Arrays.stream(target.getClass().getDeclaredFields()).collect(toList());
if (declaredFields.contains(field)) {
try {
if (isStatic(field)) {
field.set(null, value);
} else {
field.set(target, value);
}
} catch (Exception exception) {
//noinspection ResultOfMethodCallIgnored
JqwikExceptionSupport.throwAsUncheckedException(exception);
}
} else {
if (instances.isEmpty()) {
String message = String.format("Cannot set value of field %s", field);
throw new JqwikException(message);
}
setField(field, value, instances);
}
}
private static List<Class<?>> getDeclaringClasses(Class<?> clazz, HierarchyTraversalMode traversalMode) {
List<Class<?>> declaringClasses = new ArrayList<>();
Class<?> nextClass = clazz;
while (nextClass != null) {
if (traversalMode == HierarchyTraversalMode.BOTTOM_UP) {
declaringClasses.add(nextClass);
} else {
declaringClasses.add(0, nextClass);
}
nextClass = nextClass.getDeclaringClass();
}
return declaringClasses;
}
/**
* Invoke the supplied {@linkplain Method method} as in ReflectionSupport.invokeMethod(..) but potentially use outer
* instances if the method belongs to the outer instances of an object.
*
* @param method The method to invoke
* @param instances The container instances to invoke the method on, from outermost to innermost
* @param args The arguments of the method invocation
* @return Result of method invocation if there is one, otherwise null
*/
public static Object invokeMethodOnContainer(Method method, List<Object> instances, Object... args) {
return invokeMethod(method, new ArrayDeque<>(instances), args);
}
private static Object invokeMethod(Method method, Deque<Object> instances, Object... args) {
Object target = instances.removeLast();
if (method.getDeclaringClass().isAssignableFrom(target.getClass())) {
return ReflectionSupport.invokeMethod(method, target, args);
} else {
if (instances.isEmpty()) {
String message = String.format("Method [%s] cannot be invoked on target [%s].", method, target);
throw new IllegalArgumentException(message);
}
return invokeMethod(method, instances, args);
}
}
public static Set<Path> getAllClasspathRootDirectories() {
// TODO: This is quite a hack, since sometimes the classpath is quite different.
// Especially under Java >=9's module system this will probably no longer work.
String classpath = System.getProperty("java.class.path");
return Arrays.stream(classpath.split(File.pathSeparator))
.map(Paths::get).filter(Files::isDirectory)
.collect(CollectorsSupport.toLinkedHashSet());
}
public static List<MethodParameter> getMethodParameters(Executable method, Class<?> containerClass) {
List<MethodParameter> list = new ArrayList<>();
Parameter[] parameters = method.getParameters();
GenericsClassContext containerClassContext = GenericsSupport.contextFor(containerClass);
for (int i = 0; i < parameters.length; i++) {
Parameter parameter = parameters[i];
TypeResolution resolution = containerClassContext.resolveParameter(parameter);
MethodParameter methodParameter = new MethodParameter(parameter, resolution, i);
list.add(methodParameter);
}
return list;
}
public static MethodParameter getMethodParameter(Parameter parameter, int index, Class<?> containerClass) {
GenericsClassContext containerClassContext = GenericsSupport.contextFor(containerClass);
TypeResolution resolution = containerClassContext.resolveParameter(parameter);
return new MethodParameter(parameter, resolution, index);
}
public static Optional<Method> findGeneratorMethod(
String generatorToFind,
Class<?> containerClass,
Class<? extends Annotation> requiredGeneratorAnnotation,
Function<Method, String> generatorNameSupplier,
TypeUsage expectedReturnType
) {
List<Method> creators = findMethodsPotentiallyOuter(
containerClass,
isGeneratorMethod(expectedReturnType, requiredGeneratorAnnotation),
HierarchyTraversalMode.BOTTOM_UP
);
return creators.stream().filter(generatorMethod -> {
String generatorName = generatorNameSupplier.apply(generatorMethod);
if (generatorName.isEmpty()) {
generatorName = JqwikKotlinSupport.javaOrKotlinName(generatorMethod);
}
return generatorName.equals(generatorToFind);
}).findFirst();
}
public static <T> Constructor<T> findConstructor(Class<T> type, Class<?>... parameterTypes) {
try {
Constructor<T> ctor = type.getDeclaredConstructor(parameterTypes);
// Constructor<T> ctor = type.getConstructor(parameterTypes);
ctor.setAccessible(true);
return ctor;
} catch (Throwable t) {
return JqwikExceptionSupport.throwAsUncheckedException(t);
}
}
private static Predicate<Method> isGeneratorMethod(TypeUsage expectedReturnType, Class<? extends Annotation> requiredAnnotation) {
return method -> {
if (!findDeclaredOrInheritedAnnotation(method, requiredAnnotation).isPresent()) {
return false;
}
TypeUsage generatorReturnType = TypeUsage.forType(method.getAnnotatedReturnType().getType());
return generatorReturnType.canBeAssignedTo(expectedReturnType);
};
}
public static boolean isInnerClass(Class<?> clazz) {
return clazz.isMemberClass() && !ModifierSupport.isStatic(clazz);
}
public static boolean isFunctionalType(Class<?> candidateType) {
if (!candidateType.isInterface()) {
return false;
}
return countInterfaceMethods(candidateType) == 1;
}
private static long countInterfaceMethods(Class<?> candidateType) {
Method[] methods = candidateType.getMethods();
return findInterfaceMethods(methods).size();
}
private static List<Method> findInterfaceMethods(Method[] methods) {
return Arrays
.stream(methods)
.filter(m -> !m.isDefault() && !ModifierSupport.isStatic(m))
.collect(Collectors.toList());
}
public static Optional<Method> getFunctionMethod(Class<?> candidateType) {
Method[] methods = candidateType.getMethods();
List<Method> candidates = findInterfaceMethods(methods);
if (candidates.size() != 1) {
return Optional.empty();
}
return Optional.of(candidates.get(0));
}
public static boolean isEqualsMethod(Method method) {
try {
return method.equals(Object.class.getDeclaredMethod("equals", Object.class));
} catch (NoSuchMethodException shouldNeverHappen) {
return false;
}
}
public static boolean isToStringMethod(Method method) {
try {
return method.equals(Object.class.getDeclaredMethod("toString"));
} catch (NoSuchMethodException shouldNeverHappen) {
return false;
}
}
public static boolean isHashCodeMethod(Method method) {
try {
return method.equals(Object.class.getDeclaredMethod("hashCode"));
} catch (NoSuchMethodException shouldNeverHappen) {
return false;
}
}
public static boolean hasDefaultConstructor(Class<?> aClass) {
return hasConstructor(aClass);
}
public static boolean hasConstructor(Class<?> aClass, Class<?>... parameterTypes) {
try {
aClass.getDeclaredConstructor(parameterTypes);
return true;
} catch (NoSuchMethodException e) {
return false;
}
}
public static boolean isJava9orAbove() {
try {
//noinspection JavaReflectionMemberAccess
Runtime.class.getMethod("version");
return true;
} catch (NoSuchMethodException e) {
return false;
}
}
public static boolean isStatic(Class<?> clazz) {
return Modifier.isStatic(clazz.getModifiers());
}
public static boolean isPrivate(Class<?> clazz) {
return Modifier.isPrivate(clazz.getModifiers());
}
public static boolean isNotStatic(Class<?> clazz) {
return !isStatic(clazz);
}
public static boolean isStatic(Member member) {
return Modifier.isStatic(member.getModifiers());
}
public static boolean isNotStatic(Member member) {
return !isStatic(member);
}
public static boolean returnsVoid(Method method) {
return method.getReturnType().equals(Void.TYPE);
}
public static boolean implementsMethod(
Class<?> aClass,
String methodName,
Class<?>[] parameterTypes,
Class<?> ignoreImplementationClass
) {
Optional<Method> optionalMethod = ReflectionSupport.findMethod(aClass, methodName, parameterTypes);
return optionalMethod.map(method -> !method.getDeclaringClass().equals(ignoreImplementationClass)).orElse(false);
}
public static Class<?> extractRawType(Type parameterizedType) {
if (parameterizedType instanceof Class) {
return (Class<?>) parameterizedType;
}
if (parameterizedType instanceof ParameterizedType) {
return (Class<?>) ((ParameterizedType) parameterizedType).getRawType();
}
if (parameterizedType instanceof GenericArrayType) {
return Object[].class;
}
// Now we have a type variable (java.lang.reflect.TypeVariable)
return Object.class;
}
}