-
Notifications
You must be signed in to change notification settings - Fork 5
/
AnnotationsManager.java
205 lines (185 loc) · 8.4 KB
/
AnnotationsManager.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
package org.genericsystem.reactor;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import org.genericsystem.reactor.annotations.Attribute;
import org.genericsystem.reactor.annotations.BindAction;
import org.genericsystem.reactor.annotations.BindSelection;
import org.genericsystem.reactor.annotations.BindText;
import org.genericsystem.reactor.annotations.Children;
import org.genericsystem.reactor.annotations.DirectSelect;
import org.genericsystem.reactor.annotations.ForEach;
import org.genericsystem.reactor.annotations.Switch;
import org.genericsystem.reactor.annotations.Process;
import org.genericsystem.reactor.annotations.Select;
import org.genericsystem.reactor.annotations.SelectModel;
import org.genericsystem.reactor.annotations.SetStringExtractor;
import org.genericsystem.reactor.annotations.SetText;
import org.genericsystem.reactor.annotations.Stepper;
import org.genericsystem.reactor.annotations.Style;
import org.genericsystem.reactor.annotations.Style.FlexDirectionStyle;
import org.genericsystem.reactor.annotations.Style.GenericValueBackgroundColor;
import org.genericsystem.reactor.annotations.Style.KeepFlexDirection;
import org.genericsystem.reactor.annotations.Style.ReverseFlexDirection;
import org.genericsystem.reactor.annotations.StyleClass;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class AnnotationsManager {
private final Set<AnnotationProcessor> processors = new LinkedHashSet<>();
public static final Logger log = LoggerFactory.getLogger(AnnotationsManager.class);
public void registerAnnotation(Class<? extends Annotation> annotationClass) {
Annotation processAnnotation = annotationClass.getAnnotation(Process.class);
if (processAnnotation != null) {
try {
processors.add(new AnnotationProcessor(annotationClass, ((Process) processAnnotation).value().newInstance(), ((Process) processAnnotation).repeatable()));
} catch (IllegalAccessException | InstantiationException e) {
throw new IllegalStateException(e);
}
} else
log.warn("Unable to find a processor on annotation : " + annotationClass.getSimpleName());
}
public AnnotationsManager() {
registerAnnotation(Children.class);
registerAnnotation(DirectSelect.class);
registerAnnotation(Select.class);
registerAnnotation(SelectModel.class);
registerAnnotation(ForEach.class);
registerAnnotation(Stepper.class);
registerAnnotation(BindSelection.class);
registerAnnotation(SetStringExtractor.class);
registerAnnotation(StyleClass.class);
registerAnnotation(FlexDirectionStyle.class);
registerAnnotation(KeepFlexDirection.class);
registerAnnotation(ReverseFlexDirection.class);
registerAnnotation(SetText.class);
registerAnnotation(BindText.class);
registerAnnotation(BindAction.class);
registerAnnotation(Style.class);
registerAnnotation(GenericValueBackgroundColor.class);
registerAnnotation(Attribute.class);
registerAnnotation(Switch.class);
}
public void processAnnotations(Tag tag) {
for (AnnotationProcessor processor : processors)
processAnnotation(processor, tag);
}
static <T extends Tag> void processAnnotation(AnnotationProcessor processor, Tag tag) {
if (!processor.isRepeatable()) {
List<Class<?>> classesToResult = new ArrayList<>();
Tag current = tag;
Annotation applyingAnnotation = null;
while (current != null) {
List<Annotation> annotationsFound = selectAnnotations(current.getClass(), processor.getAnnotationClass(), classesToResult, tag);
if (!DirectSelect.class.equals(processor.getAnnotationClass())) {
Class<?> superClass = current.getClass().getSuperclass();
while (annotationsFound.isEmpty() && superClass != null) {
annotationsFound = selectAnnotations(superClass, processor.getAnnotationClass(), classesToResult, tag);
superClass = superClass.getSuperclass();
}
}
if (!annotationsFound.isEmpty())
applyingAnnotation = annotationsFound.get(0);
classesToResult.add(0, current.getClass());
current = current.getParent();
}
if (applyingAnnotation != null)
processor.getProcess().accept(applyingAnnotation, tag);
} else {
List<Class<?>> classesToResult = new ArrayList<>();
Tag current = tag;
List<Annotation> applyingAnnotations = new ArrayList<>();
while (current != null) {
Class<?> superClass = current.getClass();
List<Annotation> annotationsFound = new ArrayList<>();
while (superClass != null) {
annotationsFound.addAll(selectAnnotations(superClass, processor.getAnnotationClass(), classesToResult, tag));
superClass = superClass.getSuperclass();
}
Collections.reverse(annotationsFound);
applyingAnnotations.addAll(annotationsFound);
classesToResult.add(0, current.getClass());
current = current.getParent();
}
for (Annotation applyingAnnotation : applyingAnnotations)
processor.getProcess().accept(applyingAnnotation, tag);
}
}
static boolean posMatches(int[] posAnnotation, Class<?>[] pathAnnotation, Tag testedTag) {
if (posAnnotation.length == 0)
return true;
Tag tag = testedTag;
for (int i = pathAnnotation.length - 1; i >= 0; i--) {
if (posAnnotation[i] != -1 && position(tag, pathAnnotation[i]) != posAnnotation[i])
return false;
tag = tag.getParent();
}
return true;
}
// Assumes that tag is of a class extending tagClass.
public static int position(Tag tag, Class<?> tagClass) {
int result = 0;
for (Tag sibling : tag.getParent().getObservableChildren()) {
if (sibling.equals(tag))
break;
if (tagClass.isAssignableFrom(sibling.getClass()))
result++;
}
return result;
}
static List<Annotation> selectAnnotations(Class<?> annotatedClass, Class<? extends Annotation> annotationClass, List<Class<?>> classesToResult, Tag tag) {
List<Annotation> annotationsFound = new ArrayList<>();
Annotation[] annotations = annotatedClass.getAnnotationsByType(annotationClass);
for (Annotation annotation : annotations)
try {
Class<?>[] path = (Class<?>[]) annotation.annotationType().getDeclaredMethod("path").invoke(annotation);
int[] pos = (int[]) annotation.annotationType().getDeclaredMethod("pos").invoke(annotation);
if (pos.length != 0 && pos.length != path.length)
throw new IllegalStateException("The annotation " + annotationClass.getSimpleName() + " contains a path and an array of class positions of different lengths. path: "
+ Arrays.asList(path).stream().map(c -> c.getSimpleName()).collect(Collectors.toList()) + ", positions: " + IntStream.of(pos).boxed().collect(Collectors.toList()) + " found on class " + annotatedClass.getSimpleName());
if (isAssignableFrom(Arrays.asList(path), classesToResult) && posMatches(pos, path, tag)) {
if (!annotationsFound.isEmpty() && !(Style.class.equals(annotationClass) || Attribute.class.equals(annotationClass)))
throw new IllegalStateException("Multiple annotations applicable to same tag defined at same level. Annotation: " + annotationClass.getSimpleName() + ", path to tag: "
+ Arrays.asList(path).stream().map(c -> c.getSimpleName()).collect(Collectors.toList()));
annotationsFound.add(annotation);
}
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | NoSuchMethodException | SecurityException e) {
throw new IllegalStateException(e);
}
return annotationsFound;
}
static boolean isAssignableFrom(List<Class<?>> list1, List<Class<?>> list2) {
if (list1.size() != list2.size())
return false;
for (int i = 0; i < list1.size(); i++)
if (!list1.get(i).isAssignableFrom(list2.get(i)))
return false;
return true;
}
public class AnnotationProcessor {
private final Class<? extends Annotation> annotationClass;
private final BiConsumer<Annotation, Tag> process;
private final boolean repeatable;
public AnnotationProcessor(Class<? extends Annotation> annotationClass, BiConsumer<Annotation, Tag> process, boolean repeatable) {
this.annotationClass = annotationClass;
this.process = process;
this.repeatable = repeatable;
}
public Class<? extends Annotation> getAnnotationClass() {
return annotationClass;
}
public BiConsumer<Annotation, Tag> getProcess() {
return process;
}
public boolean isRepeatable() {
return repeatable;
}
}
}