Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find Method Arguments of annotated Method using Java Annotation Processor?

I have the following annotation:

@Target(ElementType.METHOD)
public @interface MyAnn {
}

and a method annotated with @MyAnn:

  @MyAnn
  Object myMehtod(Object x) {
  ...
  }

Using a Java annotation processor I get the annotated element as:

Element annotatedElement // = myMehtod 
  1. How do I get the return type of this method?
  2. How do I get the arguments of this method?
  3. How do I get the name of the arguments of this method?
like image 858
Sven Bauer Avatar asked Jun 07 '26 20:06

Sven Bauer


1 Answers

Here is my solution:

ExecutableType executableType = (ExecutableType)annotatedElement.asType();
List<? extends TypeMirror> parameters = executableType.getParameterTypes();
TypeMirror param1 = parameters.get(0);
DeclaredType declaredType = (DeclaredType)param1;
List<? extends AnnotationMirror> anns = ((TypeElement)declaredType.asElement()).getAnnotationMirrors( );
like image 158
Sven Bauer Avatar answered Jun 10 '26 18:06

Sven Bauer