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 the form:

public boolean someName(String input)

I can check the original return type, but the Stringparameter creates a problem:

private void processAnnotation(Element method, Messager msg) {
    final MyAnnotation ann = method.getAnnotation(MyAnnotation.class);
    if (method.getKind() != ElementKind.METHOD) {
      error("annotation only for methods", method);
    }
    Set<Modifier> modifiers = method.getModifiers();
    if(!modifiers.contains(Modifier.PUBLIC)) {
      error("annotated Element must be public", method);
    }
    if(modifiers.contains(Modifier.STATIC)) {
      error("annotated Element must not be static", method);
    }
    ExecutableType emeth = (ExecutableType)method.asType();
    if(!emeth.getReturnType().getKind().equals(TypeKind.BOOLEAN)) {
      error("annotated Element must have return type boolean", method);
    }
    if(emeth.getParameterTypes().size() != 1) {
      error("annotated Element must have exactly one parameter", method);
    } else {
      TypeMirror param0 = emeth.getParameterTypes().get(0);
      // TODO: check if param.get(0) is String
    }
}
Radiodef

Elements.getTypeElementDuring processing, we can retrieve it by canonical name.

Types    types = processingEnv.getTypeUtils();
Elements elems = processingEnv.getElementUtils();

TypeMirror param0 = m.getParameterTypes.get(0);
TypeMirror string = elems.getTypeElement("java.lang.String").asType();

boolean isSame = types.isSameType(param0, string);

Such a "class" cannot be obtained because annotation processing operates on partial compilation and we cannot load the class that is being compiled.

Related


How to get annotation parameter in annotation processor

Hiosdra : I'm writing my own annotation processor and I'm trying to make my annotation's parameter like the code below in the procedure method: roundEnv.getElementsAnnotatedWith(annotation).forEach { val annotation = it.getAnnotation(annotation)

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

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 get annotation parameter in annotation processor

Hiosdra : I'm writing my own annotation processor and I'm trying to make the parameters of my annotations like the code below in the procedure method: roundEnv.getElementsAnnotatedWith(annotation).forEach { val annotation = it.getAnnotation(annotation)

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 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

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 the generic type of a method parameter

AnAmuser I don't know if this can be done in Java. I can't find a solution. I want to check all methods in a class and find out the parameters of that method with generic information. For example, if you have an interface and an implementer similar to the foll

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 get annotation parameter in annotation processor

Hiosdra : I'm writing my own annotation processor and I'm trying to make my annotation's parameter like the code below in the procedure method: roundEnv.getElementsAnnotatedWith(annotation).forEach { val annotation = it.getAnnotation(annotation)

How to get annotation parameter in annotation processor

Hiosdra : I'm writing my own annotation processor and I'm trying to make my annotation's parameter like the code below in the procedure method: roundEnv.getElementsAnnotatedWith(annotation).forEach { val annotation = it.getAnnotation(annotation)

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