19

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?
        }
    }
}

1 Answer 1

18

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().

Sign up to request clarification or add additional context in comments.

1 Comment

It worked, though I can't help but feel like this is one of the least intuitive APIs I've worked with.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.