Signup/Sign In

Java Reflection API - Parameter class

In Java 8 version, Java added a new class named Parameter to get information about method parameters. The Parameter class helps to get information about method parameters, including name and modifiers, etc. It also provides an alternate means of obtaining attributes for the parameter.

We can understand it like suppose we have a class at runtime time and we want to know about its methods, parameters and parameters type, etc. In that case, we use reflection API that consists of various classes such as Class, Methods, Constructors, Parameters, etc. For more about Reflection API, you can refer to our post Java Reflection API.

It is used in reflection API and used to deal with parameters during runtime. It is stored in java.lang.reflect package. The following is the declaration of the class.

Declaration

public final class Parameter extends Object implements AnnotatedElement

Parameter Class Methods

The following table contains the methods of Parameter class that are helpful to access parameters at runtime.

Method

Description

boolean equals(Object obj)

It compares based on the executable and the index.

AnnotatedType getAnnotatedType()

It returns an AnnotatedType object that represents the use of a type to specify the type of the formal parameter represented by this Parameter.

<T extends Annotation> T getAnnotation(Class<T> annotationClass)

It returns this element's annotation for the specified type if such an annotation is present, else null.

Annotation[] getAnnotations()

It returns annotations that are present on this element.

<T extends Annotation>
T[] getAnnotationsByType(Class<T> annotationClass)

It returns annotations that are associated with this element.

<T extends Annotation>
T getDeclaredAnnotation(Class<T> annotationClass)

It returns this element's annotation for the specified type if such an annotation is directly present, else null.

Annotation[] getDeclaredAnnotations()

It returns annotations that are directly present on this element.

<T extends Annotation>
T[] getDeclaredAnnotationsByType(Class<T> annotationClass)

It returns this element's annotation(s) for the specified type if such annotations are either directly present or indirectly present.

Executable getDeclaringExecutable()

It return the Executable which declares this parameter.

int getModifiers()

It gets the modifier flags for this the parameter represented by this Parameter object.

String getName()

It returns the name of the parameter.

Type getParameterizedType()

It returns a Type object that identifies the parameterized type for the parameter represented by this Parameter object.

Class<?> getType()

It returns a Class object that identifies the declared type for the parameter represented by this Parameter object.

int hashCode()

It returns a hash code based on the executable's hash code and the index.

boolean isImplicit()

It returns true if this parameter is implicitly declared in source code; returns false otherwise.

boolean isNamePresent()

It returns true if the parameter has a name according to the class file; returns false otherwise.

boolean isSynthetic()

It returns true if this parameter is neither implicitly nor explicitly declared in source code; returns false otherwise.

boolean isVarArgs()

It returns true if this parameter represents a variable argument list; returns false otherwise.

Time for an Example:

Let's take an example to access parameters of a class "Calculate" that contains two methods with parameters. We are getting parameters by using the methods of Parameter class.

import java.lang.reflect.Method;
import java.lang.reflect.Parameter;

class Calculate {  
	int add(int a, int b){  
		return (a+b);  
	}  
	int sub(int a, int b){  
		return (a-b);  
	}  
}


public class STDemo {
	public static void main(String[] args){
		
	  Class<? extends Calculate> cal = new Calculate().getClass();
	  // Get methods
	  Method[] method = cal.getDeclaredMethods();
	  System.out.println("Method Name: "+ method[0].getName());
	  // get parameters
	  System.out.println("Parameters: ");
	  Parameter[] parameters = method[0].getParameters();
	  System.out.println(parameters[0].getType()+" "+parameters[0].getName());
	  System.out.println(parameters[1].getType()+" "+parameters[1].getName());
	  
	  	
	}
}


Method Name: add
Parameters:
int a
int b

Example: Get All Parameters of A Method

In this example, we are getting all the parameters of both methods of our class. Since getParameters() method returns an array so we need to use for loop to access all the parameters.

import java.lang.reflect.Method;
import java.lang.reflect.Parameter;

class Calculate {  
	int add(int a, int b){  
		return (a+b);  
	}  
	int sub(int a, int b){  
		return (a-b);  
	}  
}


public class STDemo {
	public static void main(String[] args){
		
	  Class<? extends Calculate> cal = new Calculate().getClass();
	  // Get methods
	  Method[] methods = cal.getDeclaredMethods();
	  for(Method method : methods) {
		  System.out.println("Method Name: "+ method.getName());
		  Parameter[] parameters = method.getParameters();
		  System.out.println("Parameters: ");
		  for(Parameter parameter : parameters)
			  System.out.println(parameter);
	  } 	
	}
}


Method Name: add
Parameters:
int a
int b
Method Name: sub
Parameters:
int a
int b

Note: To get actual names of the parameters, compile the class using -parameters flag. By default .class file does not store parameters and returns argsN as parameter name, where N is a number of parameters in the method.



About the author:
I am a Java developer by profession and Java content creator by passion. I have over 5 years of experience in Java development and content writing. I like writing about Java, related frameworks, Spring, Springboot, etc.