Skip to content

Commit 885b201

Browse files
committed
feature(gradle-test): provide ablity for indexes provides by third party lib.
1 parent 71bd392 commit 885b201

File tree

2 files changed

+65
-2
lines changed

2 files changed

+65
-2
lines changed

gestalt-inject-java/src/main/java/org/terasology/gestalt/annotation/processing/ClassIndexProcessor.java

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
package org.terasology.gestalt.annotation.processing;
22

3+
import com.google.common.collect.HashMultimap;
4+
import com.google.common.collect.Multimap;
35
import com.google.common.collect.Queues;
6+
import org.terasology.context.annotation.BindAnnotationFor;
47
import org.terasology.context.annotation.Index;
58
import org.terasology.context.annotation.IndexInherited;
69

710
import javax.annotation.processing.AbstractProcessor;
811
import javax.annotation.processing.Filer;
912
import javax.annotation.processing.ProcessingEnvironment;
1013
import javax.annotation.processing.RoundEnvironment;
14+
import javax.lang.model.element.AnnotationValue;
1115
import javax.lang.model.element.Element;
1216
import javax.lang.model.element.ElementKind;
1317
import javax.lang.model.element.PackageElement;
@@ -19,8 +23,11 @@
1923
import javax.tools.StandardLocation;
2024
import java.io.IOException;
2125
import java.io.Writer;
26+
import java.lang.annotation.Annotation;
2227
import java.util.Arrays;
2328
import java.util.Collections;
29+
import java.util.Map;
30+
import java.util.Optional;
2431
import java.util.Queue;
2532
import java.util.Set;
2633

@@ -31,17 +38,34 @@ public class ClassIndexProcessor extends AbstractProcessor {
3138
private SubtypesTypeWriter subtypesTypeWriter;
3239
private ElementUtility elementUtility;
3340

41+
private Multimap<TypeMirror, Class<? extends Annotation>> boundAnnotations;
42+
3443
@Override
3544
public synchronized void init(ProcessingEnvironment processingEnv) {
3645
super.init(processingEnv);
3746
filer = processingEnv.getFiler();
3847
annotationTypeWriter = new AnnotationTypeWriter(filer);
3948
subtypesTypeWriter = new SubtypesTypeWriter(filer);
4049
elementUtility = new ElementUtility(processingEnv.getElementUtils(), processingEnv.getTypeUtils());
50+
boundAnnotations = HashMultimap.create();
4151
}
4252

4353
@Override
4454
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
55+
for (TypeElement annotation : annotations) {
56+
if (annotation.asType().toString().equals(BindAnnotationFor.class.getName())) {
57+
for (Element type : roundEnv.getElementsAnnotatedWith(annotation)) {
58+
TypeMirror foreighElement = getBindAnnotationFor(type);
59+
if (elementUtility.hasStereotype(type, Collections.singletonList(IndexInherited.class.getName()))) {
60+
boundAnnotations.put(foreighElement, IndexInherited.class);
61+
}
62+
if (elementUtility.hasStereotype(type, Collections.singletonList(Index.class.getName()))) {
63+
boundAnnotations.put(foreighElement, Index.class);
64+
}
65+
}
66+
}
67+
}
68+
4569
for (TypeElement annotation : annotations) {
4670
// Annotation Index
4771
processAnnotationIndex(roundEnv, annotation);
@@ -66,7 +90,7 @@ private void processSubtypeIndexInReverseWay(RoundEnvironment roundEnv) {
6690
TypeMirror candidate = supers.poll();
6791
if (candidate.getKind() != TypeKind.NONE) {
6892
if (elementUtility.hasStereotype(elementUtility.getTypes().asElement(candidate),
69-
Collections.singletonList(IndexInherited.class.getName())))
93+
Collections.singletonList(IndexInherited.class.getName())) || boundAnnotations.containsEntry(candidate, IndexInherited.class))
7094
subtypesTypeWriter.writeSubType(elementUtility.getTypes().erasure(candidate).toString(), elementUtility.getTypes().erasure(type.asType()).toString());
7195
supers.addAll(elementUtility.getTypes().directSupertypes(candidate));
7296
}
@@ -86,7 +110,7 @@ private void processSubtypeIndexByDirectMarked(RoundEnvironment roundEnv, TypeEl
86110
TypeMirror candidate = supers.poll();
87111
if (candidate.getKind() != TypeKind.NONE) {
88112
if (elementUtility.hasStereotype(elementUtility.getTypes().asElement(candidate),
89-
Collections.singletonList(IndexInherited.class.getName())))
113+
Collections.singletonList(IndexInherited.class.getName())) || boundAnnotations.containsEntry(candidate, IndexInherited.class))
90114
subtypesTypeWriter.writeSubType(elementUtility.getTypes().erasure(candidate).toString(), elementUtility.getTypes().erasure(type.asType()).toString());
91115
supers.addAll(elementUtility.getTypes().directSupertypes(candidate));
92116
}
@@ -109,6 +133,27 @@ private void processAnnotationIndex(RoundEnvironment roundEnv, TypeElement annot
109133
}
110134
}
111135

136+
private TypeMirror getBindAnnotationFor(Element element) {
137+
return (TypeMirror) getAnnotationValue(element, BindAnnotationFor.class, "value").orElse(null);
138+
}
139+
140+
private Optional<Object> getAnnotationValue(Element element, Class<?> annotation, String fieldName) {
141+
return element
142+
.getAnnotationMirrors()
143+
.stream()
144+
.filter(am -> am.getAnnotationType().toString().equals(annotation.getName()))
145+
.map(am -> am.getElementValues()
146+
.entrySet()
147+
.stream()
148+
.filter(kv -> kv.getKey().getSimpleName().toString().equals(fieldName))
149+
.map(Map.Entry::getValue)
150+
.findFirst())
151+
.filter(Optional::isPresent)
152+
.map(Optional::get)
153+
.map(AnnotationValue::getValue)
154+
.findFirst();
155+
}
156+
112157
private void writeIndexes() {
113158
try {
114159
annotationTypeWriter.finish();
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.terasology.context.annotation;
2+
3+
import java.lang.annotation.Documented;
4+
import java.lang.annotation.ElementType;
5+
import java.lang.annotation.Retention;
6+
import java.lang.annotation.RetentionPolicy;
7+
import java.lang.annotation.Target;
8+
9+
/**
10+
* Provides binding gestalt-di's annotation for foreigh classes notated at `value`.
11+
*/
12+
@Retention(RetentionPolicy.RUNTIME)
13+
@Target(ElementType.TYPE)
14+
@Documented
15+
public @interface BindAnnotationFor {
16+
Class value();
17+
}
18+

0 commit comments

Comments
 (0)