HomeCore JavaAccess parameter names at runtime in Java 8

Access parameter names at runtime in Java 8

1. Java 8 access parameter names at runtime :

Java 8 introduced an easier way to access parameter names of methods and constructors at run time.

Java 8 provides java.lang.reflect.Parameter class that will give information about parameters name and its modifiers. Before java 8 we cannot directly get the parameters name of a method or constructor.

By default, the names of parameters should not be added to the compatibility burden of a method or constructor to the class. To store parameter names in a particular .class file, and thus enable the Reflection API to retrieve parameter names, compile the source file with the -parameters option to the javac compiler.

To access method or constructor parameters names at run time, the corresponding java file should be compiled with -parameters compiler argument.

Following example demonstrates how to access constructor and method parameters at run time using java  reflection.

2. Example to access parameter names :

/* A simple java class with constructor and setters and getters */
public class Employee {
    
    private long id;
    private String name;
    private String designation;
    
    public Employee(long id, String name, String designation) {
        super();
        this.id = id;
        this.name = name;
        this.designation = designation;
    }
 
    public long getId() {
        return id;
    }
 
    public void setId(long id) {
        this.id = id;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public String getDesignation() {
        return designation;
    }
 
    public void setDesignation(String designation) {
        this.designation = designation;
    } 
}

2.1. Demo java class to access method and constructor parameters :

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
 
public class RuntimeParameterDemo {
    
    public static void main(String[] args) {
        
        Constructor[] constructors = Employee.class.getDeclaredConstructors();
        System.out.println("Total Constructors: "+ constructors.length);
 
        //Print all Constructor params
        for(Constructor c : constructors) {
            System.out.println("Constructor Params: ");
            for(Parameter p : c.getParameters())
                printParam(p);
            System.out.println();
        }
        
            
        Method[] methods = Employee.class.getDeclaredMethods();
        System.out.println("Total methods: "+ methods.length);
        
               for (Method m : methods) {
              System.out.println();
                  System.out.println("Method: "+m.getName());
                 for(Parameter p : m.getParameters())
                      printParam(p);
                }
    }
    
    public static void printParam(Parameter p) {
    System.out.println("------------ Start -------------");
        System.out.println("Parameter class: "+ p.getType());
        System.out.println("Parameter name "+p.getName());
        System.out.println("Modifiers "+ p.getModifiers());
        System.out.println("------------ End -------------");
     
    }
}

2.2. Run demo class from command line :

>javac -parameters Employee.java
>javac RuntimeParameterDemo.java
>java RuntimeParameterDemo

2.3. Output :

Total Constructors: 1
Constructor Params
------------ Start -------------
Parameter class: long
Parameter name id
Modifiers 0
------------ End -------------
------------ Start -------------
Parameter class: class java.lang.String
Parameter name name
Modifiers 0
------------ End -------------
------------ Start -------------
Parameter class: class java.lang.String
Parameter name designation
Modifiers 0
------------ End -------------
 
Total methods: 6
 
Method: getName
 
Method: getId
 
Method: setName
------------ Start -------------
Parameter class: class java.lang.String
Parameter name name
Modifiers 0
------------ End -------------
 
Method: getDesignation
 
Method: setId
------------ Start -------------
Parameter class: long
Parameter name id
Modifiers 0
------------ End -------------
 
Method: setDesignation
------------ Start -------------
Parameter class: class java.lang.String
Parameter name designation
Modifiers 0
------------ End -------------

If you compile your java file with -parameters only you allow to access parameter names at run time, otherwise parameters names would be like arg0arg1 etc.

Conclusion

In this article we have covered access or obtain to method parameters names at run time with examples.

Reference

  1. JEP 118
  2. JDK 8

LEAVE A REPLY

Please enter your comment!
Please enter your name here