简体   繁体   English

如何在 Java 11 的注释处理器中获取带注释的方法参数的名称

[英]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.

相关问题 如何在注释处理器中获取注释方法的返回类型? - How to get return type of annotated method in annotation processor? 如何在注释处理器上获取 Java Class 的声明字段? - How can I get the Declared Fields of a Java Class on an Annotation Processor? 如何只允许在 Java 中的方法参数中使用某些注释进行注释的类? - How to allow only classes annotated with some annotation in method parameters in Java? 使用Java注释处理器查找带注释方法的方法参数? - Find Method Arguments of annotated Method using Java Annotation Processor? Java / Kotlin注释处理器:获取带注释的字段/属性的类型 - Java/Kotlin annotation processor: get type of annotated field/property 如何在springboot/java应用程序中使用给定的注释获取方法注释 - How to get Method annotated with given annotation in springboot / java app Java注释处理器是否能够删除注释的代码 - Is Java annotation processor capable of removing annotated code 如何获取带注释的方法参数及其注释 - How to get annotated method parameter and his annotation 如何在Java中获取带注释的值? - How can I get the annotated value in java? 如何从Java中的Annotation Processor获取项目的工作目录 - How can I get the working directory of a project from an Annotation Processor in Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM