towi towi - 4 months ago 10
Java Question

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:

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
. For example the processed source could be:

void myMethodOk(@Input String input) { }

void myMethodNotOk(@Input String input) { }


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.