如何在Java运行时获取参数名称

3
我正在使用Java 8。在Java 8中,有一个叫做get parameter的方法,但它返回的输出是arg0arg1等。然而,我想要确切的参数名称。有人能告诉我如何实现吗?我看到一些回答说我们可以使用paranamer。但我找不到解决方案。我正在尝试构建一个自动化框架,所以我有这个要求。
例如,如果我的函数是:
public void Login(String sUserName, String sPassword)
{
}

在另一个类文件中,我希望输出为sUserName。

与某些语言不同,Java中的参数本身没有名称,它们只是变量的占位符,这些变量在您的代码中可以被命名为任何名称。Java使用参数类型来区分不同的方法。 - MadProgrammer
2
你是如何编译程序的?默认情况下,编译器会删除参数名称。 - user253751
你能展示一下你正在做什么吗? - Devesh
我直接通过运行为 -> TestNG设置来运行它。 - shubham pandey
Eclipse -> 项目属性 -> Java编译器 -> 在生成的类文件中添加变量属性。 - ring bearer
4个回答

6

从Java 8开始,您可以使用反射API获取参数名称:

Method someMethod = Main.class.getDeclaredMethod("someMethod");
Parameter[] parameters = someMethod.getParameters();
for(Parameter parameter : parameters)
{
    System.out.println(parameter.getName());
}

此外,请参阅Parameter#getName()的JavaDoc:
返回参数的名称。如果参数的名称存在,则此方法返回类文件提供的名称。否则,此方法会合成一个名称,形式为argN,其中N是声明该参数的方法描述符中参数的索引。

4
在使用javac编译源代码时,请使用选项-parameters,它会将构造函数和方法的形式参数名称存储在生成的类文件中。 - Eng.Fouad
我该如何在Eclipse中实现它?因为我正在使用TestNg框架在Eclipse中运行代码。Eclipse中有任何设置可以做到这一点吗? - shubham pandey
1
@shubhampandey 窗口 > 首选项 > Java > 编译器,然后勾选“存储有关方法参数的信息(可通过反射使用)”选项。 - Eng.Fouad
仍然输出相同的结果:我正在使用以下代码:for (Method method : CommonFunctions.class.getDeclaredMethods()) { String name = method.getName(); System.out.println("方法是:" + name); Parameter[] param = method.getParameters(); for (Parameter parameter : param) { System.out.println("参数名称为:" + parameter.getName()); } - shubham pandey
2
@Eng.Fouad 请将编译器选项/ Eclipse 选项的信息添加到您的答案中,而不是评论中。 - Erwin Bolwidt
显示剩余2条评论

0

Java不支持在运行时指定变量名称。(编译后,变量的分配“名称”实际上并没有保存。)尝试的一件事可能是类似于以下内容:

class NamedInteger{
    public String name;
    public Integer value;
}

值得一想。不确定这是否是你想要的。


2
你是否注意到了这个答案 - PM 77-1

0

你可以尝试以下两种方法:

Eclipse -> 项目属性 -> Java编译器 -> 添加变量属性到生成的类文件。

或者使用 javac -g:vars 编译你的类并加入调试信息。

上述方法仅适用于类,不适用于接口。

如果你确实需要这么做,有一种过度解决方案可供使用。首先,在你的类文件中定义适当的 Java 文档和参数信息。

/**
 * Print permutations of given string "aString".
 * @param fixed
 * @param aString
 */

完成这个步骤后,您可以使用一些Doclet代码来读取文档并从中获取参数名称。

 for (ClassDoc classDoc : root.classes()) {
   for (MethodDoc methodDoc : classDoc.methods()) {
     if (methodDoc.parameters() != null) {
       for (Parameter p : methodDoc.parameters()) {
         System.out.println(p.name());
       }
     }
   }
 }

我只是在提出一种可能性;不要这样做,因为文档很容易过时。


0
Hi I got the answers given by Eng.Fouad.
You'll have to use jre1.8 
getParameters function is available in java8 only.

I just used the following piece of code:


for (Method method : CommonFunctions.class.getDeclaredMethods()) {
            String name = method.getName(); 
            System.out.println("method is :"+name);
        Parameter[] param =   method.getParameters();
        for (Parameter parameter : param) {
            System.out.println("name of parameter is :" +parameter.getName());
        }

Here CommonFunctions is the class of which methods parameters you want.

You need to do the folowing settings in the Eclipse:
Window->Preference->Compiler->check option "Add Variable attribute to generated class file"(Usable through debugger)
Window Preference ->Compiler-> Store information about method parameters(Usabe through reflection)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接