-
Notifications
You must be signed in to change notification settings - Fork 5
/
AnnotationsManager.java
229 lines (207 loc) · 9.37 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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package org.genericsystem.reactor;
import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Deque;
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.CustomAnnotations;
import org.genericsystem.reactor.annotations.DirectSelect;
import org.genericsystem.reactor.annotations.ForEach;
import org.genericsystem.reactor.annotations.ForEachContext;
import org.genericsystem.reactor.annotations.InheritStyle;
import org.genericsystem.reactor.annotations.Process;
import org.genericsystem.reactor.annotations.Select;
import org.genericsystem.reactor.annotations.SelectContext;
import org.genericsystem.reactor.annotations.SetStringExtractor;
import org.genericsystem.reactor.annotations.SetText;
import org.genericsystem.reactor.annotations.Step;
import org.genericsystem.reactor.annotations.Stepper;
import org.genericsystem.reactor.annotations.Stepper2;
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.genericsystem.reactor.annotations.Switch;
import org.genericsystem.reactor.annotations.TagName;
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 AnnotationsManager(Class<? extends RootTag> clazz) {
initManager(clazz);
}
protected void registerCustomAnnotations(Class<? extends RootTag> clazz) {
Annotation annotations = clazz.getAnnotation(CustomAnnotations.class);
if (annotations != null)
for (Class<? extends Annotation> annotation : ((CustomAnnotations) annotations).value())
registerAnnotation(annotation);
}
public void initManager(Class<? extends RootTag> clazz) {
registerAnnotation(Children.class);
registerAnnotation(DirectSelect.class);
registerAnnotation(Select.class);
registerAnnotation(SelectContext.class);
registerAnnotation(ForEach.class);
registerAnnotation(ForEachContext.class);
registerAnnotation(Stepper.class);
registerAnnotation(Step.class);
registerAnnotation(Stepper2.class);
registerAnnotation(BindSelection.class);
registerAnnotation(SetStringExtractor.class);
registerAnnotation(TagName.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(InheritStyle.class);
registerAnnotation(Attribute.class);
registerAnnotation(Switch.class);
registerCustomAnnotations(clazz);
}
public void registerAnnotation(Class<? extends Annotation> annotationClass) {
Process processAnnotation = annotationClass.getAnnotation(Process.class);
if (processAnnotation != null) {
try {
processors.add(new AnnotationProcessor(annotationClass, processAnnotation.value().newInstance(), processAnnotation.repeatable()));
} catch (IllegalAccessException | InstantiationException e) {
throw new IllegalStateException(e);
}
} else
log.warn("Unable to find a processor on annotation: {}.", annotationClass.getSimpleName());
}
public void processAnnotations(Tag tag) {
for (AnnotationProcessor processor : processors)
processAnnotation(processor, tag);
}
static <T extends Tag> void processAnnotation(AnnotationProcessor processor, Tag tag) {
Deque<Class<?>> classesToResult = new ArrayDeque<>();
Tag current = tag;
if (!processor.isRepeatable()) {
Annotation applyingAnnotation = null;
while (current != null) {
List<Annotation> annotationsFound = selectAnnotations(current.getClass(), processor, classesToResult, tag);
if (!DirectSelect.class.equals(processor.getAnnotationClass())) {
Class<?> superClass = current.getClass().getSuperclass();
while (annotationsFound.isEmpty() && Tag.class.isAssignableFrom(superClass)) {
annotationsFound = selectAnnotations(superClass, processor, classesToResult, tag);
superClass = superClass.getSuperclass();
}
}
if (!annotationsFound.isEmpty())
applyingAnnotation = annotationsFound.get(0);
classesToResult.push(current.getClass());
current = current.getParent();
}
if (applyingAnnotation != null)
processor.getProcess().accept(applyingAnnotation, tag);
} else {
List<Annotation> applyingAnnotations = new ArrayList<>();
while (current != null) {
Class<?> superClass = current.getClass();
List<Annotation> annotationsFound = new ArrayList<>();
while (Tag.class.isAssignableFrom(superClass)) {
annotationsFound.addAll(selectAnnotations(superClass, processor, classesToResult, tag));
superClass = superClass.getSuperclass();
}
Collections.reverse(annotationsFound);
applyingAnnotations.addAll(annotationsFound);
classesToResult.push(current.getClass());
current = current.getParent();
}
for (Annotation applyingAnnotation : applyingAnnotations)
processor.getProcess().accept(applyingAnnotation, tag);
}
}
public 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 (tag.getParent() == null || 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, AnnotationProcessor processor, Deque<Class<?>> classesToResult, Tag tag) {
Class<? extends Annotation> annotationClass = processor.getAnnotationClass();
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), new ArrayList<>(classesToResult)) && posMatches(pos, path, tag)) {
if (!annotationsFound.isEmpty() && !processor.isRepeatable())
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;
}
public 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;
}
}
}