When obtaining a TypeElement
in an annotation processor, you can ask for its super class (or more specifically, the TypeMirror
of it) using method getSuperClass()
. According to the JavaDoc, a type that doesn't explicity extend anything (in other words, Object
is the super class) or that is an interface will return a NoType
with NONE
as TypeKind
.
Disregarding the fact that the whole model/mirror API thing seems to go out of its way to confuse you at every opportunity for a moment, how would I reliably check for this? Here's some code extracts:
//Cast is safe since TypeKind is checked before this
final TypeElement el = (TypeElement)annotatedElement;
...
final TypeMirror parent = el.getSuperclass();
//Checking whether "nothing" is extended
if(parent.getKind().equals(TypeKind.NONE) && parent instanceof NoType) {
...
}
Is this the proper manner? It seems rather clunky. Since the equals
method of TypeMirror should not be used for semantic equality checks, I'm wondering whether it is safe to use it for TypeKind
. My gut feeling says yes since it's an enum, but then I'm having doubts about that instanceof
.
Is there a better way of doing this? The whole javax.lang.model package and its children have cross-references all over the shop and it's never really clear to me what the best method for something is. There's useful utility methods for some stuff, and then there are seemingly simple tasks that require dubious acrobatics like the above.