本文介绍了如何从javax.lang.model.VariableElement获取参数类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用Java 6元模型API查找方法的参数类型.如果类型是枚举,我也想知道所有类型的枚举常量名称.这是到目前为止我得到的:
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?
}
}
}
推荐答案
Element#asType()获取DeclaredType.
Element#asType() gets you the DeclaredType.
对于枚举,请使用 Types#使用带有DeclaredType的asElement()来获取枚举类型的元素,然后使用ElementVisitor或使用getEnclosedElements()对成员进行迭代.
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().
这篇关于如何从javax.lang.model.VariableElement获取参数类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!