|
2 | 2 |
|
3 | 3 | import com.github.javaparser.ast.CompilationUnit; |
4 | 4 | import com.github.javaparser.ast.type.Type; |
| 5 | + |
| 6 | +import java.lang.annotation.Annotation; |
| 7 | + |
| 8 | +import io.leangen.geantyref.AnnotationFormatException; |
| 9 | +import io.leangen.geantyref.TypeFactory; |
5 | 10 | import java.util.HashSet; |
| 11 | +import java.util.Map; |
6 | 12 | import java.util.Set; |
7 | 13 | import javax.annotation.processing.AbstractProcessor; |
8 | 14 | import javax.annotation.processing.Messager; |
|
16 | 22 | import javax.lang.model.element.TypeParameterElement; |
17 | 23 | import javax.tools.Diagnostic.Kind; |
18 | 24 | import com.google.auto.service.AutoService; |
| 25 | +import com.google.common.collect.ImmutableList; |
| 26 | + |
19 | 27 | import anthonisen.felix.astParsing.AstManipulator; |
20 | 28 | import anthonisen.felix.astParsing.util.TypeHandler; |
21 | 29 | import anthonisen.felix.astParsing.visitors.ParameterTypeCollector; |
22 | 30 | import anthonisen.felix.astParsing.visitors.ReturnTypeCollector; |
23 | 31 |
|
24 | 32 | @AutoService(Processor.class) |
25 | 33 | @SupportedSourceVersion(SourceVersion.RELEASE_17) |
26 | | -@SupportedAnnotationTypes("anthonisen.felix.annotationProcessing.MyVariance") |
| 34 | +@SupportedAnnotationTypes({ |
| 35 | + "anthonisen.felix.annotationProcessing.MyVariance", |
| 36 | + "anthonisen.felix.annotationProcessing.Covariant", |
| 37 | + "anthonisen.felix.annotationProcessing.Contravariant", |
| 38 | +}) |
27 | 39 | public class VarianceProcessor extends AbstractProcessor { |
28 | 40 | private Messager messager; |
29 | 41 | private AstManipulator astManipulator; |
| 42 | + private final ImmutableList<Class<? extends Annotation>> supportedAnnotations = ImmutableList.of(MyVariance.class, |
| 43 | + Covariant.class, Contravariant.class); |
30 | 44 |
|
31 | 45 | @Override |
32 | 46 | public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { |
33 | 47 | messager = processingEnv.getMessager(); |
34 | 48 | astManipulator = new AstManipulator(messager, |
35 | 49 | System.getProperty("user.dir") + "/src/main/java"); |
36 | 50 | messager.printMessage(Kind.NOTE, "Processing annotations:\n"); |
37 | | - for (Element e : roundEnv.getElementsAnnotatedWith(MyVariance.class)) { |
38 | | - MyVariance annotation = e.getAnnotation(MyVariance.class); |
39 | | - // should not process method declarations |
40 | | - if (!isClassParameter(e)) |
41 | | - continue; |
| 51 | + for (Class<? extends Annotation> annotationType : supportedAnnotations) { |
| 52 | + for (Element e : roundEnv.getElementsAnnotatedWith(annotationType)) { |
| 53 | + MyVariance annotation = e.getAnnotation(MyVariance.class); |
| 54 | + try { |
| 55 | + if (annotationType.equals(Covariant.class)) |
| 56 | + annotation = TypeFactory.annotation(MyVariance.class, |
| 57 | + Map.of("variance", VarianceType.COVARIANT, "strict", true)); |
| 58 | + else if (annotationType.equals(Contravariant.class)) |
| 59 | + annotation = TypeFactory.annotation(MyVariance.class, |
| 60 | + Map.of("variance", VarianceType.CONTRAVARIANT, "strict", true)); |
42 | 61 |
|
43 | | - TypeParameterElement tE = (TypeParameterElement) e; |
44 | | - String className = tE.getEnclosingElement().getSimpleName().toString(); |
45 | | - String packageName = processingEnv.getElementUtils().getPackageOf(tE.getEnclosingElement()).toString(); |
| 62 | + } catch (AnnotationFormatException ex) { |
| 63 | + // catch this later |
| 64 | + } |
| 65 | + if (annotation != null) |
| 66 | + processElement(annotation, e); |
| 67 | + else |
| 68 | + messager.printMessage(Kind.WARNING, "Could not parse annotation for element: " + e); |
| 69 | + } |
| 70 | + } |
| 71 | + astManipulator.applyChanges(); |
| 72 | + return true; |
| 73 | + } |
46 | 74 |
|
47 | | - if (packageName.contains("output")) |
48 | | - continue; |
| 75 | + private void processElement(MyVariance annotation, Element e) { |
| 76 | + // should not process method declarations |
| 77 | + if (!isClassParameter(e)) |
| 78 | + return; |
49 | 79 |
|
50 | | - if (annotation.variance() == VarianceType.INVARIANT) { |
51 | | - messager.printMessage(Kind.NOTE, |
52 | | - String.format( |
53 | | - "Invariant type parameter detected in class: %s\nWill not proceed with AST manipulation", |
54 | | - className)); |
55 | | - } |
| 80 | + TypeParameterElement tE = (TypeParameterElement) e; |
| 81 | + String className = tE.getEnclosingElement().getSimpleName().toString(); |
| 82 | + String packageName = processingEnv.getElementUtils().getPackageOf(tE.getEnclosingElement()).toString(); |
56 | 83 |
|
57 | | - checkVariance(className, annotation.variance(), packageName, tE.getSimpleName().toString()); |
58 | | - astManipulator.eraseTypesAndInsertCasts(className + ".java", packageName, tE.getSimpleName().toString()); |
| 84 | + if (packageName.contains("output")) |
| 85 | + return; |
59 | 86 |
|
| 87 | + if (annotation.variance() == VarianceType.INVARIANT) { |
| 88 | + messager.printMessage(Kind.NOTE, |
| 89 | + String.format( |
| 90 | + "Invariant type parameter detected in class: %s\nWill not proceed with AST manipulation", |
| 91 | + className)); |
60 | 92 | } |
61 | | - astManipulator.applyChanges(); |
62 | | - return true; |
| 93 | + |
| 94 | + checkVariance(className, annotation, packageName, tE.getSimpleName().toString()); |
| 95 | + astManipulator.eraseTypesAndInsertCasts(className + ".java", packageName, |
| 96 | + tE.getSimpleName().toString()); |
63 | 97 | } |
64 | 98 |
|
65 | | - private void checkVariance(String className, VarianceType variance, String packageName, String typeOfInterest) { |
| 99 | + private void checkVariance(String className, MyVariance annotation, String packageName, String typeOfInterest) { |
66 | 100 | Set<Type> types = new HashSet<>(); |
67 | 101 | CompilationUnit cu = astManipulator.getSourceRoot().parse(packageName, className |
68 | 102 | + ".java"); |
69 | | - if (variance == VarianceType.CONTRAVARIANT) |
| 103 | + if (annotation.variance() == VarianceType.CONTRAVARIANT) |
70 | 104 | cu.accept(new ReturnTypeCollector(), types); |
71 | 105 | else |
72 | 106 | cu.accept(new ParameterTypeCollector(), types); |
73 | 107 |
|
74 | 108 | for (Type type : types) { |
75 | 109 | if (TypeHandler.containsType(type, typeOfInterest)) { |
76 | 110 | messager.printMessage( |
77 | | - Kind.WARNING, |
| 111 | + annotation.strict() ? Kind.ERROR : Kind.WARNING, |
78 | 112 | String.format( |
79 | 113 | "%s is declared as %s, but does not conform to constraints: contains T in %s position", |
80 | 114 | className, |
81 | | - variance, |
82 | | - variance == VarianceType.COVARIANT ? "IN" : "OUT")); |
| 115 | + annotation.variance(), |
| 116 | + annotation.variance() == VarianceType.COVARIANT ? "IN" : "OUT")); |
83 | 117 | break; |
84 | 118 | } |
85 | 119 | } |
|
0 commit comments