|
| 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 | +} |
0 commit comments