java 有没有办法在Java中获取方法参数的名称?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/381502/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a way to obtain names of method parameters in Java?
提问by ansgri
I'm writing small and very DRYframework, which heavily relies on metadata. I'd like to know if there is a way to obtain method parameter names, i.e. given some method
我正在编写非常依赖元数据的小型且非常DRY 的框架。我想知道是否有一种方法可以获得方法参数名称,即给出一些方法
public void a(int myIntParam, String theString) { ... }
get the strings "myIntParam"and "theString".
获取字符串"myIntParam"和"theString"。
I know I could annotate parameters, but that wouldn't be nice...
我知道我可以注释参数,但这不会很好......
public void a(
@Param("myIntParam") int myIntParam,
@Param("theString") String theString
) { ... }
采纳答案by dstine
We created a custom annotation for the method that holds a String[] of parameter names. This approach felt a little easier to manage than having to annotate each individual parameter. We plan to add build-time checking that the number of annotated parameter names matches the number of arguments, since that it what we require.
我们为保存参数名称的 String[] 的方法创建了一个自定义注释。这种方法感觉比必须注释每个单独的参数更容易管理。我们计划添加构建时检查注释参数名称的数量是否与参数数量匹配,因为这是我们所需要的。
回答by Jonny Heggheim
Here is a dirty solution that needs some tweaking. Maybe someone can make it better.
这是一个需要一些调整的肮脏解决方案。也许有人可以让它变得更好。
Cons:
缺点:
- Requires that you know the location of compiled class file.
- It has to be compiled with the -g flag.
- 要求您知道编译后的类文件的位置。
- 它必须使用 -g 标志编译。
Code:
代码:
import com.sun.org.apache.bcel.internal.classfile.ClassParser;
import com.sun.org.apache.bcel.internal.classfile.JavaClass;
import com.sun.org.apache.bcel.internal.classfile.LocalVariable;
import com.sun.org.apache.bcel.internal.classfile.Method;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
ClassParser parser = new ClassParser("Main.class");
JavaClass clazz = parser.parse();
for (Method m : clazz.getMethods()) {
System.out.println("Method: " + m.getName());
int size = m.getArgumentTypes().length;
if (!m.isStatic()) {
size++;
}
for (int i = 0; i < size; i++) {
LocalVariable variable = m.getLocalVariableTable().getLocalVariable(i);
System.out.println(" - Param: " + variable.getName());
}
}
}
public void a(int myIntParam, String theString) {
}
}
Output:
输出:
$ javac -g Main.java$ java MainMethod: <init>- Param: thisMethod: main
- Param: args
Method: a
- Param: this
- Param: myIntParam
- Param: theString
$ javac -g Main.java$ java MainMethod: <init>- Param: thisMethod: main
- Param: args
Method: a
- Param: this
- Param: myIntParam
- Param: theString
回答by GaryF
Not really, but codehaus have this library that will do for a lot of purposes: http://paranamer.codehaus.org/
不是真的,但是 codehaus 有这个库可以用于很多目的:http://paranamer.codehaus.org/
回答by bobwienholt
I could be wrong about this... but I don't think parameter names appear in a class file so I would guess that there is no way to get them via reflection.
我可能是错的......但我不认为参数名称出现在类文件中,所以我猜想没有办法通过反射来获取它们。
回答by David Tinker
If you are using Spring you are in luck. Just add this to your applicationContext.xml:
如果您使用的是 Spring,那么您很幸运。只需将其添加到您的 applicationContext.xml 中:
<bean class="org.springframework.core.LocalVariableTableParameterNameDiscoverer"/>
Then you can inject this bean where it is needed:
然后你可以在需要的地方注入这个 bean:
@Autowired
private ParameterNameDiscoverer parameterNameDiscoverer;
Method m = ...
String[] names = parameterNameDiscoverer.getParameterNames(m);
As its name suggests, this implementation relies on classes being compiled with debug info.
顾名思义,此实现依赖于使用调试信息编译的类。
回答by ralfs
The name of the parameters are present in the class file, when the java code was compiled with debugging information (via the -g option). The class file then contains a LocalVariableTable attribute (see http://java.sun.com/docs/books/jvms/second_edition/html/ClassFile.doc.html#5956). This contains the names of local variables and parameters are just local variables. The parameters correspond to the variable slots starting at index 1 (or index 0 for static methods).
当使用调试信息(通过 -g 选项)编译 java 代码时,类文件中存在参数的名称。然后类文件包含一个 LocalVariableTable 属性(参见http://java.sun.com/docs/books/jvms/second_edition/html/ClassFile.doc.html#5956)。这包含局部变量的名称,参数只是局部变量。参数对应于从索引 1(或静态方法的索引 0)开始的变量槽。
回答by Dan Vinton
@bobwienholt is correct - parameter names are not compiled into java classes, and so aren't available at runtime.
@bobwienholt 是正确的 - 参数名称未编译到 Java 类中,因此在运行时不可用。

