Check if there is no superclass in annotation processor


G_H:

TypeElementWhen obtained in an annotation processor, TypeMirrora method can be used to request its superclass (or more specifically, its superclass ) getSuperClass(). According to the JavaDoc , a type does not explicitly extend anything (in other words, Objectis a superclass), or an interface will return an interface NoTypewith NONEas TypeKind.

Ignoring the fact that the whole model/mirror API stuff seems to confuse you at any point, how would I check this reliably? The following is a partial code excerpt:

//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 right way? It seems clumsy. Since the equalsTypeMirror method should not be used for semantic equality checks, I would like to know the safety of using it TypeKind. My intuition is yes since it's an enum, but I doubt it instanceof.

Is there a better way? The entire javax.lang.model package and its children are cross-referenced throughout the store, and for me never really figured out which best approach was what. There are some useful utility methods for certain jobs, and then there seem to be simple tasks that require dubious acrobatics like the one above.

G_H:

I just did a test and realized I misunderstood the documentation. It returns NoType of type NONE for java.lang.Object and interfaces, not for things that implicitly extend Object. Silly me. I don't need to do the check, because earlier versions strictly check if the annotated element is a class (not an enum or interface), and if it is, it will be returned. In other words, checking if the canonical name is java.lang.Object or using Types.isSameTypeshould do the trick.

Feel sorry! I've decided not to delete because others may ask the same question. Feel free to vote for removal if you think it's appropriate.

EDIT: This is where I ended up going...

boolean superTypeValid = true;
final TypeMirror parent = el.getSuperClass();
if(!parent.getKind().equals(TypeKind.DECLARED)) {
    messager.printMessage(Kind.ERROR, "May only inherit from a class; not enums, annotations or other declared kinds.", annotatedElement, mirror);
    superTypeValid = false;
} else {
    final DeclaredType parentType = (DeclaredType)parent;
    final Element parentEl = parentType.asElement();
    if(!parentEl.getKind().equals(ElementKind.CLASS)) {
        messager.printMessage(Kind.ERROR, "May only inherit from a class; not enums, annotations or other declared kinds.", annotatedElement, mirror);
        superTypeValid = false;
    }
}
...
if(superTypeValid) {
    if(typeUtils.isSameType(parent, elUtils.getTypeElement("java.lang.Object").asType())) {
        messager.printMessage(Kind.ERROR, "Inherit must not be set to true if the class doesn't extend.", annotatedElement, mirror);
        valid = false;
    } else {
        ...
    }
}

If some of these error messages look weird, it's because I'm missing something specific to the project.

Related


Check if there is no superclass in annotation processor

G_H: TypeElementWhen obtained in an annotation processor, TypeMirrora method can be used to request its superclass (or more specifically, its superclass ) getSuperClass(). According to the JavaDoc , a type does not explicitly extend anything (in other words, O

Check if there is no superclass in annotation processor

G_H: TypeElementWhen obtained in an annotation processor, TypeMirrora method can be used to request its superclass (or more specifically, its superclass ) getSuperClass(). According to the JavaDoc , a type does not explicitly extend anything (in other words, O

Java annotation processor check for full rebuild

thedevilqwe: I'm creating an annotation processor in Java and would like to be able to check if the user triggered a full rebuild. I'd like to be able to differentiate between a full rebuild and just building a few files. is it possible? or any workaround for

How to check method parameter type in annotation processor?

Tovey This would be easy if pure reflection was used, but the annotation processor world seems to be different. How do I TypeMirrorreturn from getParameterTypes()to String.class? In my annotation processor, I want to check if the currently accessed method has

Check class hierarchy when compiling on annotation processor

Chilo I am writing an annotation processor to perform the following checks at compile time: has an interfaceE There is an @Applyannotation for annotating methods. Annotated methods @Applyshould be called applyand take only one parameter of the implementing cla

How to check method parameter type in annotation processor?

Tovey This would be easy if pure reflection was used, but the annotation processor world seems to be different. How do I TypeMirrorreturn from getParameterTypes()to String.class? In my annotation processor, I want to check if the currently accessed method has

Java annotation processor check for full rebuild

thedevilqwe: I'm creating an annotation processor in Java and would like to be able to check if the user triggered a full rebuild. I'd like to be able to differentiate between a full rebuild and just building a few files. is it possible? or any workaround for

Java annotation processor check for full rebuild

thedevilqwe: I'm creating an annotation processor in Java and would like to be able to check if the user triggered a full rebuild. I'd like to be able to differentiate between a full rebuild and just building a few files. is it possible? or any workaround for

Java annotation processor check for full rebuild

thedevilqwe: I'm creating an annotation processor in Java and would like to be able to check if the user triggered a full rebuild. I'd like to be able to differentiate between a full rebuild and just building a few files. is it possible? or any workaround for

How to check method parameter type in annotation processor?

Tovey This would be easy if pure reflection was used, but the annotation processor world seems to be different. How do I TypeMirrorreturn from getParameterTypes()to String.class? In my annotation processor, I want to check if the currently accessed method has

Check class hierarchy when compiling on annotation processor

Chilo I am writing an annotation processor to perform the following checks at compile time: has an interfaceE There is an @Applyannotation for annotating methods. Annotated methods @Applyshould be called applyand take only one parameter of the implementing cla

How to check method parameter type in annotation processor?

Tovey This would be easy if pure reflection was used, but the annotation processor world seems to be different. How do I TypeMirrorreturn from getParameterTypes()to String.class? In my annotation processor, I want to check if the currently accessed method has

Java annotation processor check for full rebuild

thedevilqwe: I'm creating an annotation processor in Java and would like to be able to check if the user triggered a full rebuild. I'd like to be able to differentiate between a full rebuild and just building a few files. is it possible? or any workaround for

Java annotation processor check for full rebuild

thedevilqwe: I'm creating an annotation processor in Java and would like to be able to check if the user triggered a full rebuild. I'd like to be able to differentiate between a full rebuild and just building a few files. is it possible? or any workaround for

How to check method parameter type in annotation processor?

Tovey This would be easy if pure reflection was used, but the annotation processor world seems to be different. How do I TypeMirrorreturn from getParameterTypes()to String.class? In my annotation processor, I want to check if the currently accessed method has

Annotation processor output in Maven

iGili I use JSR 269 as a way to analyze the code during compilation and fail the code if needed. I'm having trouble displaying the output of an annotation processor with maven (Ant does display the output) I'm using javax.annotation.processing.Messager to disp

Using ServiceLoader in annotation processor

specification Is it possible to use ServiceLoaderin a method of init(ProcessingEnvironment)annotation processor ? interface Service {} class AnnotationProcessor extends AbstractProcessor { public static void main(String[] args) { ServiceLoade

Empty annotation processor in Eclipse

David Ten Hoff I'm using the Eclipse JDT Null annotation processor and I'm getting some weird behavior when using java.lang.Class. package test; import org.eclipse.jdt.annotation.Nullable; public class AnnotationSpul { @Nullable public <V> V get1(Cl

Annotation processor output in Maven

iGili I use JSR 269 as a way to analyze the code during compilation and fail the code if needed. I'm having trouble displaying the output of an annotation processor with maven (Ant does display the output) I'm using javax.annotation.processing.Messager to disp

Using ServiceLoader in annotation processor

specification Is it possible to use ServiceLoaderin a method of init(ProcessingEnvironment)annotation processor ? interface Service {} class AnnotationProcessor extends AbstractProcessor { public static void main(String[] args) { ServiceLoade

Annotation processor for Jersey annotations

Sabir Khan I'm just wondering about annotation processors like NJ annotations @Path, @Consumesetc. I mean, is this provided by or Jersey APIby javax.ws.rs-api-xxx.jar? I can't seem to figure out how these REST annotations are handled, I can find out by explori