-
Notifications
You must be signed in to change notification settings - Fork 5
/
ExtendedRootTag.java
230 lines (197 loc) · 9.13 KB
/
ExtendedRootTag.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
package org.genericsystem.reactor.gscomponents;
import java.io.Serializable;
import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import org.genericsystem.api.core.AxedPropertyClass;
import org.genericsystem.api.core.annotations.Components;
import org.genericsystem.api.core.annotations.Dependencies;
import org.genericsystem.api.core.annotations.InstanceClass;
import org.genericsystem.api.core.annotations.Meta;
import org.genericsystem.api.core.annotations.SystemGeneric;
import org.genericsystem.api.core.annotations.constraints.InstanceValueClassConstraint;
import org.genericsystem.api.core.annotations.constraints.NoInheritance;
import org.genericsystem.api.core.annotations.constraints.PropertyConstraint;
import org.genericsystem.api.core.annotations.value.ClassGenericValue;
import org.genericsystem.common.Generic;
import org.genericsystem.common.Root;
import org.genericsystem.reactor.AnnotationsManager;
import org.genericsystem.reactor.Tag;
import org.genericsystem.reactor.TagNode;
import org.genericsystem.reactor.annotations.Attribute;
import org.genericsystem.reactor.annotations.Children;
import org.genericsystem.reactor.annotations.Style;
import org.genericsystem.reactor.gscomponents.ExtendedRootTag.GTagType.AnnotationParameter;
import org.genericsystem.reactor.gscomponents.ExtendedRootTag.GTagType.AnnotationParameterValue;
import org.genericsystem.reactor.gscomponents.ExtendedRootTag.GTagType.TagAnnotation;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.collections.ObservableMap;
public class ExtendedRootTag extends RootTagImpl {
private final Root engine;
private final Generic tagAnnotationType;
private final Generic annotationParameter;
private final Generic parameterValue;
// To avoid duplicate work.
private final Map<Class<?>, GTag> storedClasses = new HashMap<>();
public ExtendedRootTag(Root engine) {
this.engine = engine;
storedClasses.put(TagImpl.class, engine.find(GTag.class));
tagAnnotationType = engine.find(TagAnnotation.class);
annotationParameter = engine.find(AnnotationParameter.class);
parameterValue = engine.find(AnnotationParameterValue.class);
super.initRoot();
}
@Override
protected void initRoot() {
}
public Root getEngine() {
return engine;
}
@Override
public TagNode buildTagNode(Tag tag) {
storeClass(tag.getClass());
return new GenericTagNode(tag).init();
}
private GTag storeClass(Class<?> clazz) {
if (!TagImpl.class.isAssignableFrom(clazz))
return getEngine().find(GTag.class);
if (storedClasses.containsKey(clazz))
return storedClasses.get(clazz);
Generic parentGeneric = storeClass(clazz.getSuperclass());
GTag classGeneric = (GTag) parentGeneric.getMeta().setInstance(parentGeneric, clazz);
Children[] annotations = clazz.getAnnotationsByType(Children.class);
for (int i = 0; i < annotations.length; i++) {
Children childrenAnnotation = annotations[i];
Generic annotationGeneric = classGeneric.setHolder(tagAnnotationType, new AxedPropertyClass(Children.class, i));
storeAnnotationParameter(annotationGeneric, "path", childrenAnnotation.path());
storeAnnotationParameter(annotationGeneric, "value", childrenAnnotation.value());
storeAnnotationParameter(annotationGeneric, "pos", childrenAnnotation.pos());
}
Style[] styleAnnotations = clazz.getAnnotationsByType(Style.class);
for (int i = 0; i < styleAnnotations.length; i++) {
Style styleAnnotation = styleAnnotations[i];
Generic styleAnnotationGeneric = classGeneric.setHolder(tagAnnotationType, new AxedPropertyClass(Style.class, i));
storeAnnotationParameter(styleAnnotationGeneric, "path", styleAnnotation.path());
storeAnnotationParameter(styleAnnotationGeneric, "value", styleAnnotation.value());
storeAnnotationParameter(styleAnnotationGeneric, "pos", styleAnnotation.pos());
storeAnnotationParameter(styleAnnotationGeneric, "name", styleAnnotation.name());
}
storedClasses.put(clazz, classGeneric);
getEngine().getCurrentCache().flush();
return classGeneric;
}
private void storeAnnotationParameter(Generic annotationGeneric, String parameterName, Serializable value) {
Generic paramName = annotationGeneric.getComposites(parameterName).filter(g -> annotationParameter.equals(g.getMeta())).first();
if (paramName == null)
paramName = annotationGeneric.addHolder(annotationParameter, parameterName);
Generic paramValue = paramName.getComposites().filter(g -> parameterValue.equals(g.getMeta())).first();
if (paramValue != null) {
if (!Objects.deepEquals(value, paramValue.getValue()))
paramName.setHolder(parameterValue, value);
} else
paramName.addHolder(parameterValue, value);
}
@Override
public void processChildren(Tag tag, Class<? extends TagImpl>[] classes) {
for (Class<?> clazz : classes)
storeClass(clazz);
}
public class GenericTagNode implements TagNode {
private ObservableList<Tag> children = FXCollections.observableArrayList();
private Tag tag;
public GenericTagNode(Tag tag) {
this.tag = tag;
}
public GenericTagNode init() {
List<Class<?>> classesToResult = new ArrayList<>();
Tag current = tag;
Generic applyingAnnotation = null;
while (current != null) {
Generic annotationFound = selectAnnotation(current.getClass(), Children.class, classesToResult, tag);
Class<?> superClass = current.getClass().getSuperclass();
while (annotationFound == null && superClass != null) {
annotationFound = selectAnnotation(superClass, Children.class, classesToResult, tag);
superClass = superClass.getSuperclass();
}
if (annotationFound != null)
applyingAnnotation = annotationFound;
classesToResult.add(0, current.getClass());
current = current.getParent();
}
if (applyingAnnotation != null) {
Class<? extends TagImpl>[] childrenClasses = (Class<? extends TagImpl>[]) applyingAnnotation.getHolder(annotationParameter, "value").getHolder(parameterValue).getValue();
for (Class<? extends TagImpl> childClass : childrenClasses) {
TagImpl result = createChild(tag, childClass);
GenericTagNode tagNode = new GenericTagNode(result);
result.setTagNode(tagNode);
tagNode.init();
processAnnotations(result);
result.init();
children.add(result);
}
}
return this;
}
private Generic selectAnnotation(Class<?> annotatedClass, Class<? extends Annotation> annotationClass, List<Class<?>> classesToResult, Tag tag) {
Generic annotationFound = null;
Generic tagClass = storeClass(annotatedClass);
List<Generic> annotations = tagClass.getObservableHolders(tagAnnotationType).filtered(g -> annotationClass.equals(((AxedPropertyClass) g.getValue()).getClazz()));
for (Generic annotation : annotations) {
Class<?>[] path = (Class<?>[]) annotation.getHolder(annotationParameter, "path").getHolder(parameterValue).getValue();
int[] pos = (int[]) annotation.getHolder(annotationParameter, "pos").getHolder(parameterValue).getValue();
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: " + Arrays.stream(pos).boxed().collect(Collectors.toList()) + " found on class " + annotatedClass.getSimpleName());
if (AnnotationsManager.isAssignableFrom(Arrays.asList(path), classesToResult) && AnnotationsManager.posMatches(pos, path, tag)) {
if (annotationFound != null && !(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()));
annotationFound = annotation;
}
}
return annotationFound;
}
@Override
public ObservableList<Tag> getObservableChildren() {
return children;
}
@Override
public ObservableMap<String, String> getObservableStyles() {
return FXCollections.observableHashMap();
}
}
@SystemGeneric
@InstanceClass(GTag.class)
@Dependencies({ TagAnnotation.class })
@InstanceValueClassConstraint(Class.class)
public static interface GTagType extends Generic {
@SystemGeneric
@Components(GTagType.class)
@InstanceValueClassConstraint(AxedPropertyClass.class)
@Dependencies({ AnnotationParameter.class, AnnotationParameterValue.class })
@NoInheritance
public static interface TagAnnotation extends Generic {
}
@SystemGeneric
@Components(TagAnnotation.class)
@InstanceValueClassConstraint(String.class)
public static interface AnnotationParameter extends Generic {
}
@SystemGeneric
@Components(AnnotationParameter.class)
@PropertyConstraint
public static interface AnnotationParameterValue extends Generic {
}
}
@SystemGeneric
@Meta(GTagType.class)
@ClassGenericValue(TagImpl.class)
public static interface GTag extends Generic {
}
}