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 classE

So far I have identified all the annotated methods applyand extracted the names of the classes they take as parameters. So I am left with:

 Element method  // this is the Element that represents the `apply` method
 TypeMirror mirror //method's TypeMirror
 String paramClass // the parameter's class Name.

The question is: how (if at all) how to get rid of the class hierarchy representation of these parameters so that their implementation can be checked E. This won't work since the class doesn't ClassLoader.loadClassexist yet , but I just need the class hierarchy.

Chilo

The way to use the javax.lang.model.util.Types.isSubtype()method is simple :

 TypeMirror parameterTypes = mirror.getParameterTypes();
 TypeMirror typeOfE = processingEnv.getElementUtils().getTypeElement(E.class.getName()).asType();
 boolean isSubTypeOfE = processingEnv.getTypeUtils().isSubtype(parameterType, eventType)

This way I can check if the parameter class of the represented method mirroris a subtype of the desired typeE

Related


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

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

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

Get field class in annotation processor

Emil Sjölander: I'm writing my first annotation processor and I'm having some trivial issues, but can't find any info about it. I have an element with an annotation @MyAnnotation String property; When I get this property as an element in the processor, I can'

Get field class in annotation processor

Emil Sjölander: I'm writing my first annotation processor and I'm having some trivial issues, but can't find any info about it. I have an element with an annotation @MyAnnotation String property; When I get this property as an element in the processor, I can'

Get field class in annotation processor

Emil Sjölander: I'm writing my first annotation processor and I'm having some trivial issues, but can't find any info about it. I have an element with an annotation @MyAnnotation String property; When I get this property as an element in the processor, I can'

Get field class in annotation processor

Emil Sjölander: I'm writing my first annotation processor and I'm having some trivial issues, but can't find any info about it. I have an element with an annotation @MyAnnotation String property; When I get this property as an element in the processor, I can'

Get field class in annotation processor

Emil Sjölander: I'm writing my first annotation processor and I'm having some trivial issues, but can't find any info about it. I have an element with an annotation @MyAnnotation String property; When I get this property as an element in the processor, I can'

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

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

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

Get fields of class from annotation processor annotation object

Luke: I am working on my first annotation processor. I'm trying to get through a field (which is a method field) of the declared object on the annotated class. E.g: public class Person { private String name; private String surname; } ... public void m