shubham pandey
shubham pandey

Reputation: 103

How can I get the parameter name in java at run time

I am using Java 8. In Java 8 there is a method name as get parameter but it gives the output like arg0, arg1. However I want the exact parameter name. Can anybody tell me how to achieve it? I have seen some response like we can use paranamer. But I couldn't find the solution. I am trying to build an automation framework so I have this requirement.

for example if my function is

public void Login(String sUserName, String sPassword)
{
}

So in a different class file I want the output as sUserName.

Upvotes: 4

Views: 3754

Answers (4)

shubham pandey
shubham pandey

Reputation: 103

Regarding the answers given by Eng.Fouad. You have to use jre 1.8, as the getParameters method is available in Java 8 only.

I've 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 can be replaced with the actual class whose methods' parameters' names must be read.

To enable it in Eclipse, you need to set up the following settings:

Window > Preference > Compiler > check option "Add Variable attribute to generated class file"(Usable through debugger)

Window Preference > Compiler > Store information about method parameters (Usable through reflection)

Upvotes: 0

Eng.Fouad
Eng.Fouad

Reputation: 117597

Starting from Java 8, you can use Reflection API to retrieve parameters names:

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

Also, see JavaDoc of Parameter#getName():

Returns the name of the parameter. If the parameter's name is present, then this method returns the name provided by the class file. Otherwise, this method synthesizes a name of the form argN, where N is the index of the parameter in the descriptor of the method which declares the parameter.

Upvotes: 6

ring bearer
ring bearer

Reputation: 20783

Either you try

Eclipse -> project properties -> Java compiler -> Add variable attributes to generated class files.

Or compile your classes with debugging information as javac -g:vars

Above applies only for classes and not for interfaces.

If you absolutely must do this, there is an overkill that you can indulge yourself in. For that first, you must define proper java doc with parameter information in your class files.

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

Once this is done, you can use a bit of Doclet code to read documentation and get the parameter names from documentation.

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

Well I am just suggesting a possibility; Do not do this because documentations can get really out dated real fast.

Upvotes: 0

Lilith Daemon
Lilith Daemon

Reputation: 1473

Java does not support giving the variable names at run-time. (The variables's assigned "name" does not actually save when compiled.) One thing to try might be something along the lines of:

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

Just worth a thought. Not sure if this would be what your wanting.

Upvotes: 0

Related Questions