-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathReflector.java
More file actions
96 lines (82 loc) · 4.15 KB
/
Reflector.java
File metadata and controls
96 lines (82 loc) · 4.15 KB
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
package com.github.nylle.javafixture;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InaccessibleObjectException;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Stream;
import static java.lang.String.format;
import static java.util.stream.Collectors.groupingBy;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
public class Reflector<T> {
private final T instance;
private final SpecimenType<T> type;
public Reflector(T instance, SpecimenType<T> type) {
this.instance = instance;
this.type = type;
}
public Reflector<T> validateCustomization(CustomizationContext customizationContext) {
var declaredFields = getDeclaredFields(type.asClass()).map(field -> field.getName()).collect(toList());
var missingDeclaredField = customizationContext.findAllCustomizedFieldNames()
.map(entry -> entry.replaceAll("\\..+", ""))
.filter(entry -> !declaredFields.contains(entry))
.findFirst();
if (missingDeclaredField.isPresent()) {
throw new CustomizationException(format("Cannot customize field '%s': Field not found in class '%s'.", missingDeclaredField.get(), type.getName()));
}
var duplicateField = getDeclaredFields(type.asClass())
.collect(groupingBy(field -> field.getName()))
.entrySet()
.stream()
.filter(x -> x.getValue().size() > 1)
.filter(x -> customizationContext.findAllCustomizedFieldNames()
.map(entry -> entry.replaceAll("\\..+", ""))
.anyMatch(y -> y.equals(x.getKey())))
.findFirst();
if (duplicateField.isPresent()) {
throw new CustomizationException(format("Cannot customize field '%s'. Duplicate field names found: %n%s",
duplicateField.get().getKey(),
duplicateField.get().getValue().stream().map(x -> x.toString()).collect(joining("\n"))));
}
return this;
}
public Stream<Field> getDeclaredFields() {
return getDeclaredFields(type.asClass());
}
public Annotation[] getFieldAnnotations(Field field) {
try {
return Stream.concat(Arrays.stream(Introspector.getBeanInfo(type.asClass()).getPropertyDescriptors())
.filter(property -> !Modifier.isStatic(field.getModifiers()))
.filter(property -> property.getName().equals(field.getName()))
.flatMap(propertyDescriptor -> Stream.of(propertyDescriptor.getReadMethod(), propertyDescriptor.getWriteMethod())
.filter(x -> Objects.nonNull(x))
.flatMap(method -> Stream.of(method.getAnnotations()))), Arrays.stream(field.getAnnotations())).toArray(x -> new Annotation[x]);
} catch (IntrospectionException e) {
return field.getAnnotations();
}
}
public void setField(Field field, Object value) {
try {
if (!field.getType().isPrimitive() || value != null) {
field.setAccessible(true);
field.set(instance, value);
}
} catch (SecurityException e) {
throw new SpecimenException(format("Unable to access field %s on object of type %s", field.getName(), type.getName()), e);
} catch (IllegalAccessException | InaccessibleObjectException e) {
throw new SpecimenException(format("Unable to set field %s on object of type %s", field.getName(), type.getName()), e);
}
}
private Stream<Field> getDeclaredFields(Class<?> type) {
return Stream.concat(
Stream.of(type.getDeclaredFields()).filter(field -> !Modifier.isStatic(field.getModifiers())),
Optional.ofNullable(type.getSuperclass())
.map(superclass -> getDeclaredFields(superclass))
.orElse(Stream.of()));
}
}