towi
towi

Reputation: 22267

How to get from a Method to the Annotations of its Parameters during Annotation-processing?

During annotation processing I am currently processing the annotation of a method:

@Override
public boolean process(Set<? extends TypeElement> elements, RoundEnvironment env) {
  Messager msg = processingEnv.getMessager();
  for (TypeElement te : elements) {
    for (Element e : env.getElementsAnnotatedWith(te)) {
      processAnnotation(e, msg);
    }
  }
  return true;
}

private void processAnnotation(Element method, Messager msg) {
  final Info ann = method.getAnnotation(Info.class);
  assert method.getKind() == ElementKind.METHOD;
  ....

I can get to the types (or its mirrors) of the parameters with

  final ExecutableType emeth = (ExecutableType)method.asType();
  final List<? extends TypeMirror> parameterTypes = emeth.getParameterTypes();

but how to I get to its annotations? I would like to check, if the method under consideration has any parameter with the annotation @Input. For example the processed source could be:

@Info
void myMethodOk(@Input String input) {  }

@Info
void myMethodNotOk(@Input String input) { }

Upvotes: 2

Views: 1006

Answers (1)

jbunting
jbunting

Reputation: 901

If you cast your method Element to ExecutableElement, then you can invoke executableElement.getParamerers(). This returns a list of VariableElements, which you can get annotations from.

Upvotes: 3

Related Questions