Reputation: 11
I am trying to use an annotation processor to validate annotations, and as part of that effort, I am trying to figure out how to use the API to determine if a Parameter of an ExecutableElement is a parameterized type (such as List<Foo>), and if so, what the parameterized type(s) are (Foo).
Is there any way to do this besides just parsing the string given by ve.asType().toString() where VariableElement ve is an element of ExecutableElement e.getParameters()? It would be nice to have a better handle on those types than just simply a string.
Upvotes: 0
Views: 219
Reputation: 1054
The idea is to know when to cast to what, in your case you need to obtain the generic type argument, so you need to cast to DeclaredType
.
for example for a method like the following
@SampleAnno
public void something(List<String> paramx){
}
a code in a processor like this
ExecutableElement method = (ExecutableElement) this.sampleElement;
method.getParameters()
.forEach(parameter -> ((DeclaredType)parameter.asType()).getTypeArguments()
.forEach(typeMirror -> {
messager.printMessage(Diagnostic.Kind.NOTE, "::::::: > [" + typeMirror.toString() + "]");
}));
should print
Information:java: ::::::: > [java.lang.String]
Upvotes: 1