Skip to content

Commit c13507d

Browse files
DarkWeirdsoloturn
authored andcommitted
feature(gradle-test): provide ablity for indexes provides by third party lib.
1 parent 0c6e948 commit c13507d

2 files changed

Lines changed: 65 additions & 2 deletions

File tree

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,6 +1,9 @@
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

@@ -9,6 +12,7 @@
912
import javax.annotation.processing.ProcessingEnvironment;
1013
import javax.annotation.processing.RoundEnvironment;
1114
import javax.lang.model.SourceVersion;
15+
import javax.lang.model.element.AnnotationValue;
1216
import javax.lang.model.element.Element;
1317
import javax.lang.model.element.ElementKind;
1418
import javax.lang.model.element.PackageElement;
@@ -20,8 +24,11 @@
2024
import javax.tools.StandardLocation;
2125
import java.io.IOException;
2226
import java.io.Writer;
27+
import java.lang.annotation.Annotation;
2328
import java.util.Arrays;
2429
import java.util.Collections;
30+
import java.util.Map;
31+
import java.util.Optional;
2532
import java.util.Queue;
2633
import java.util.Set;
2734

@@ -35,17 +42,34 @@ public class ClassIndexProcessor extends AbstractProcessor {
3542
private SubtypesTypeWriter subtypesTypeWriter;
3643
private ElementUtility elementUtility;
3744

45+
private Multimap<TypeMirror, Class<? extends Annotation>> boundAnnotations;
46+
3847
@Override
3948
public synchronized void init(ProcessingEnvironment processingEnv) {
4049
super.init(processingEnv);
4150
filer = processingEnv.getFiler();
4251
annotationTypeWriter = new AnnotationTypeWriter(filer);
4352
subtypesTypeWriter = new SubtypesTypeWriter(filer);
4453
elementUtility = new ElementUtility(processingEnv.getElementUtils(), processingEnv.getTypeUtils());
54+
boundAnnotations = HashMultimap.create();
4555
}
4656

4757
@Override
4858
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
59+
for (TypeElement annotation : annotations) {
60+
if (annotation.asType().toString().equals(BindAnnotationFor.class.getName())) {
61+
for (Element type : roundEnv.getElementsAnnotatedWith(annotation)) {
62+
TypeMirror foreighElement = getBindAnnotationFor(type);
63+
if (elementUtility.hasStereotype(type, Collections.singletonList(IndexInherited.class.getName()))) {
64+
boundAnnotations.put(foreighElement, IndexInherited.class);
65+
}
66+
if (elementUtility.hasStereotype(type, Collections.singletonList(Index.class.getName()))) {
67+
boundAnnotations.put(foreighElement, Index.class);
68+
}
69+
}
70+
}
71+
}
72+
4973
for (TypeElement annotation : annotations) {
5074
// Annotation Index
5175
processAnnotationIndex(roundEnv, annotation);
@@ -75,7 +99,7 @@ private void processSubtypeIndexInReverseWay(Element type) {
7599
TypeMirror candidate = supers.poll();
76100
if (candidate.getKind() != TypeKind.NONE) {
77101
if (elementUtility.hasStereotype(elementUtility.getTypes().asElement(candidate),
78-
Collections.singletonList(IndexInherited.class.getName()))) {
102+
Collections.singletonList(IndexInherited.class.getName())) || boundAnnotations.containsEntry(candidate, IndexInherited.class)) {
79103
TypeElement candidateElement = (TypeElement) elementUtility.getTypes().asElement(elementUtility.getTypes().erasure(candidate));
80104
TypeElement erasedType = (TypeElement) elementUtility.getTypes().asElement(elementUtility.getTypes().erasure(type.asType()));
81105
subtypesTypeWriter.writeSubType(elementUtility.getElements().getBinaryName(candidateElement).toString(),
@@ -98,7 +122,7 @@ private void processSubtypeIndexByDirectMarked(RoundEnvironment roundEnv, TypeEl
98122
TypeMirror candidate = supers.poll();
99123
if (candidate.getKind() != TypeKind.NONE) {
100124
if (elementUtility.hasStereotype(elementUtility.getTypes().asElement(candidate),
101-
Collections.singletonList(IndexInherited.class.getName()))) {
125+
Collections.singletonList(IndexInherited.class.getName())) || boundAnnotations.containsEntry(candidate, IndexInherited.class)) {
102126
TypeElement candidateElement = (TypeElement) elementUtility.getTypes().asElement(elementUtility.getTypes().erasure(candidate));
103127
TypeElement erasedType = (TypeElement) elementUtility.getTypes().asElement(elementUtility.getTypes().erasure(type.asType()));
104128
subtypesTypeWriter.writeSubType(elementUtility.getElements().getBinaryName(candidateElement).toString(),
@@ -126,6 +150,27 @@ private void processAnnotationIndex(RoundEnvironment roundEnv, TypeElement annot
126150
}
127151
}
128152

153+
private TypeMirror getBindAnnotationFor(Element element) {
154+
return (TypeMirror) getAnnotationValue(element, BindAnnotationFor.class, "value").orElse(null);
155+
}
156+
157+
private Optional<Object> getAnnotationValue(Element element, Class<?> annotation, String fieldName) {
158+
return element
159+
.getAnnotationMirrors()
160+
.stream()
161+
.filter(am -> am.getAnnotationType().toString().equals(annotation.getName()))
162+
.map(am -> am.getElementValues()
163+
.entrySet()
164+
.stream()
165+
.filter(kv -> kv.getKey().getSimpleName().toString().equals(fieldName))
166+
.map(Map.Entry::getValue)
167+
.findFirst())
168+
.filter(Optional::isPresent)
169+
.map(Optional::get)
170+
.map(AnnotationValue::getValue)
171+
.findFirst();
172+
}
173+
129174
private void writeIndexes() {
130175
try {
131176
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)