How to get field type annotation in Java annotation processing?

For example, I have this code:

@Retention(RetentionPolicy.SOURCE)
public @interface ClassAnnotation {
}

@ClassAnnotation
public class AnnotatedClass {
}

@ClassAnnotation
public class AnotherAnnotatedClass {

    private AnnotatedClass someField;
    private int intIsNotAnnotated;
}

And this processor processes it at compile time:

@SupportedAnnotationTypes({ "com.example.ClassAnnotation" })
@SupportedSourceVersion(SourceVersion.RELEASE_6)
public class AwesomeProcessor extends AbstractProcessor {

    public boolean process(Set<? extends TypeElement> annotations,
            RoundEnvironment roundEnv) {
        // Skipped for brevity...
        // For each annotated class
        for (Element e : roundEnv.getElementsAnnotatedWith(ClassAnnotation.class)) {
            // Skipped for brevity...
            // For each field
            for (Element ee : classElement.getEnclosedElements()) {
                // Skipped for brevity... (of course there kind checking)
                TypeMirror fieldType = fieldElement.asType();
                TypeElement fieldTypeElement = (TypeElement) processingEnv.
                    getTypeUtils().asElement(fieldType);
            }
        }
        // Skipped for brevity
    }
}

I need to check if a field type is a class that annotates with my annotation. Somehow I have TypeElementa name fieldTypeElement, it can represent AnnotatedClassfrom someFieldor intfrom intIsNotAnnotatedin the example. How to get @ClassAnnotationfrom AnnotatedClassof someField? I tried fieldTypeElement.getAnnotation(ClassAnnotation.class)and fieldTypeElement.getAnnotationMirrors(), but it returns a null and empty list respectively.

+4
source share
2 answers

, - TypeElement, , , .

, :

processingEnv.getElementUtils().getTypeElement("AnnotatedClass").getAnnotationMirrors()

@ClassAnnotation.

TypeElement ,

  • DeclaredType
  • TypeElement

, :

Element field = // ... 
List<? extends AnnotationMirror> annotationMirrors =
    ((DeclaredType) field.asType()).asElement().getAnnotationMirrors();

, , . . :

  • , , , .
  • RetentionPolicy . , CLASS RUNTIME , SOURCE
+3

Field Class , . Field, # getType. , , Field s.

Processor.java

public class Processor {

    public static void main(String[] args) {

        Class<?> clazz = AnotherAnnotatedClass.class;
        Field[] fields = clazz.getDeclaredFields();

        for(Field f:fields){
            if(f.getType().isAnnotationPresent(ClassAnnotation.class)){
                System.out.println(f.getName());
            }
        }
    }
}

ClassAnnotation.java

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface ClassAnnotation {

}

AnnotatedClass.java

@ClassAnnotation
public class AnnotatedClass {

}

AnotherAnnotatedClass.java

@ClassAnnotation
public class AnotherAnnotatedClass {

    private AnnotatedClass annotatedClass;
    private int intIsNotAnnotated;
}
-1

Source: https://habr.com/ru/post/1547512/


All Articles