I’m working with Java 6’s annotation processing, i.e. what can be found within javax.annotation.processing (not Java 5’s APT).
I wonder what the conceptional difference between the various Element, Type, and Mirror classes is. As I don’t really understand this, it’s hard to efficiently program an annotation processor. There are various methods that ‘convert’ between these notions but I’m not really sure what I’m doing when using them.
So, for example, let me have an instance of AnnotationMirror.
When I call getAnnotationType() I get an instance of DeclaredType (which implements TypeMirror for whatever reason).
Then I can call asElement() on this one and obtain an instance of Element.
What has happened?
The object of type
javax.lang.model.element.AnnotationMirrorrepresents an annotation in your code.The declared type represents the annotation class.
Its element is the generic class (see http://java.sun.com/javase/6/docs/api/javax/lang/model/element/TypeElement.html for more information on that matter). The element might be the generic version of a class, like
List, where as the declared type is the parametrized version, for instanceList<String>. However I’m not sure it is possible to have annotations classes use generics and thus the distinction might be irrelevant in that context.For instance lets say you have the following JUnit4 method:
The AnnotationMirror represents
@Test(expected = NullPointerException.class). The declared type is theorg.junit.Testclass. The element is more or less the same as there are no generics involved.