Iterating an annotated class in compile-time Annotation Processing

2 hours ago 1
ARTICLE AD BOX

I'm working on a compile-time Annotation-Processor.

I have annotated a class with MyAnnotation and want to iterate the attributes of the annotated class. The process method looks like this

@SupportedAnnotationTypes("mypackage.MyAnnotation") public class MyAnnotationProcessor extends AbstractProcessor { // ... @Override public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) { Messager messager = processingEnv.getMessager(); try { TypeElement te = null; for (Element element : roundEnv.getElementsAnnotatedWith(MyAnnotation.class)) { if (element.getKind() == ElementKind.CLASS) { messager.printError(element.toString()); messager.printError(element.getClass().toString()); te = (TypeElement)element; } } // ...

which is like all examples I have seen. The output it generates is

mypackage.MyAnnotatedClass class com.sun.tools.javac.code.Symbol$ClassSymbol

The mypackage.MyAnnotatedClass is what I would expect, but the type ClassSymbol seems to be a node of the abstract syntax tree instead of a TypeElement(?) and after the loop, the TypeElement te is null.

Can anyone tell me what is wrong here? This makes no sense to me, because element has type Element, but ClassSymbol is not derived from Element, so it should not even be possible that element points to an object of type ClassSymbol.

Read Entire Article