Skip to content

Commit b78da85

Browse files
init commit for annotation processor
1 parent 79e7950 commit b78da85

File tree

3 files changed

+88
-0
lines changed

3 files changed

+88
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package anthonisen.felix.annotationProcessing;
2+
3+
import java.lang.annotation.ElementType;
4+
import java.lang.annotation.Retention;
5+
import java.lang.annotation.RetentionPolicy;
6+
import java.lang.annotation.Target;
7+
8+
@Retention(RetentionPolicy.SOURCE)
9+
@Target(ElementType.TYPE_PARAMETER)
10+
public @interface MyVariance {
11+
public VarianceType variance() default VarianceType.INVARIANT;
12+
13+
public int depth() default Integer.MAX_VALUE;
14+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package anthonisen.felix.annotationProcessing;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
import java.util.Set;
6+
7+
import javax.annotation.processing.AbstractProcessor;
8+
import javax.annotation.processing.Messager;
9+
import javax.annotation.processing.Processor;
10+
import javax.annotation.processing.RoundEnvironment;
11+
import javax.annotation.processing.SupportedAnnotationTypes;
12+
import javax.annotation.processing.SupportedSourceVersion;
13+
import javax.lang.model.SourceVersion;
14+
import javax.lang.model.element.Element;
15+
import javax.lang.model.element.TypeElement;
16+
import javax.lang.model.element.TypeParameterElement;
17+
import javax.tools.Diagnostic.Kind;
18+
import com.google.auto.service.AutoService;
19+
20+
import anthonisen.felix.astParsing.Covariancer;
21+
22+
@AutoService(Processor.class)
23+
@SupportedSourceVersion(SourceVersion.RELEASE_17)
24+
@SupportedAnnotationTypes("anthonisen.felix.annotationProcessing.MyVariance")
25+
public class VarianceProcessor extends AbstractProcessor {
26+
27+
Map<String, String> classHierarchy = new HashMap<>();
28+
29+
@Override
30+
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
31+
Messager messager = processingEnv.getMessager();
32+
Covariancer covariancer = new Covariancer(messager,
33+
System.getProperty("user.dir") + "/src/main/java/anthonisen/felix");
34+
messager.printMessage(Kind.NOTE, "Processing annotations:\n");
35+
for (Element e : roundEnv.getElementsAnnotatedWith(MyVariance.class)) {
36+
MyVariance annotation = e.getAnnotation(MyVariance.class);
37+
messager.printMessage(Kind.NOTE, e.getEnclosingElement().getKind().name());
38+
if (isClassParameter(e)) {
39+
messager.printMessage(Kind.NOTE, "Is part of a class declaration");
40+
} else
41+
messager.printMessage(Kind.NOTE, "Is part of a method declaration");
42+
messager.printMessage(Kind.NOTE, e.getEnclosingElement().getKind().name());
43+
switch (annotation.variance()) {
44+
case INVARIANT:
45+
messager.printMessage(Kind.NOTE, "Invariant class detected");
46+
break;
47+
case CONTRAVARIANT:
48+
messager.printMessage(Kind.NOTE, "Contravariant class detected");
49+
break;
50+
case COVARIANT:
51+
messager.printMessage(Kind.NOTE, "Covariant class detected");
52+
break;
53+
}
54+
TypeParameterElement tE = (TypeParameterElement) e;
55+
covariancer.makeCovariant(tE.getEnclosingElement().getSimpleName() + ".java");
56+
// Get the enclosing element (this could be a class, method, or constructor)
57+
58+
}
59+
covariancer.applyChanges();
60+
return true;
61+
}
62+
63+
// Check whether the type parameter is for a class or a method
64+
private static boolean isClassParameter(Element e) {
65+
return e.getEnclosingElement().getKind().name().equals("CLASS");
66+
}
67+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package anthonisen.felix.annotationProcessing;
2+
3+
public enum VarianceType {
4+
INVARIANT,
5+
COVARIANT,
6+
CONTRAVARIANT
7+
}

0 commit comments

Comments
 (0)