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 to preprocess it in 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's kind checking)
TypeMirror fieldType = fieldElement.asType();
TypeElement fieldTypeElement = (TypeElement) processingEnv.
getTypeUtils().asElement(fieldType);
}
}
// Skipped for brevity
}
}
I need to check whether a field's type is a class that is annotated with my annotation. Somehow I've got a TypeElement named fieldTypeElement, it may represent AnnotatedClass from someField or int from intIsNotAnnotated in the example. How to get @ClassAnnotation of AnnotatedClass of someField? I've tried fieldTypeElement.getAnnotation(ClassAnnotation.class) and fieldTypeElement.getAnnotationMirrors(), but it returns null and empty list respectively.
I think you are somehow getting the wrong TypeElement but unfortunatly that part of the code is missing.
This simple example works as expected:
processingEnv.getElementUtils().getTypeElement("AnnotatedClass").getAnnotationMirrors()
contains @ClassAnnotation.
To get the TypeElement of a field, you'll have to
DeclaredType
TypeElement
The last step isn't necessary though to get an element's annotations:
Element field = // ...
List<? extends AnnotationMirror> annotationMirrors =
((DeclaredType) field.asType()).asElement().getAnnotationMirrors();
The updated code should work, I've tested it and it runs fine. The error must be elsewhere. Some other things to check for:
RetentionPolicy of the annotation. None, CLASS or RUNTIME are fine but SOURCE won't workIf you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With