Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get parameter type from javax.lang.model.VariableElement

Tags:

java

apt

I'm trying to find the types of the parameters of a method using the Java 6 metamodel API. If the type is an enum, I'd also like to know all of it's type's enum constant names. Here's what I've got so far:

for (Element member : members) {
    if(member.getKind() == ElementKind.METHOD) {
        ExecutableElement methodElement = (ExecutableElement) member;
        List<? extends VariableElement> parameters = methodElement.getParameters();
        for (VariableElement parameter : parameters) {
            //How do I get the type of the parameter here?
        }
    }
}
like image 792
Daniel Avatar asked Oct 14 '11 05:10

Daniel


1 Answers

Element#asType() gets you the DeclaredType.

For enums, use Types#asElement() with the DeclaredType to get the enum type's element, and then iterate over the members using either an ElementVisitor or by using getEnclosedElements().

like image 150
prunge Avatar answered Nov 03 '22 03:11

prunge