[英]How can I get the names of annotated method parameters in an Annotation Processor in Java 11
In Java 8, I used to be able to do在 Java 8 中,我曾经能够做到
Symbol.MethodSymbol annotatedMethod = (Symbol.MethodSymbol) annotatedElement;
and then接着
annotatedMethod.params
And I would get both the types of the method's parameters, AND the names of the parameters in the method declaration.我会得到方法参数的类型和方法声明中的参数名称。
Now that import com.sun.tools.javac.code.Symbol is internal in Java 11, I can get the parameters' types by using:现在 import com.sun.tools.javac.code.Symbol 在 Java 11 中是内部的,我可以使用以下方法获取参数的类型:
ExecutableType executableType = (ExecutableType)annotatedElement.asType();
List<? extends TypeMirror> parameters = executableType.getParameterTypes();
But how can I get the parameters' names?但是我怎样才能得到参数的名字呢?
ExecutableType
is a part of the "Mirror API", which is generally about types. ExecutableType
是“Mirror API”的一部分,一般是关于类型的。 For inspecting elements, use the "Element API".要检查元素,请使用“元素 API”。
In your case, parameter is modeled by javax.lang.model.element.VariableElement
, and the name can be obtained with element.getSimpleName()
.在您的情况下,参数由javax.lang.model.element.VariableElement
建模,并且可以使用element.getSimpleName()
获得名称。
I guess, annotatedElement
in your code is an ExecutableElement
.我猜,您的代码中的annotatedElement
是一个ExecutableElement
。 If so, parameters' elements can be obtained via annotatedElement.getParameters()
.如果是这样,参数的元素可以通过annotatedElement.getParameters()
获得。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.