java.lang.reflect.Parameter Class in Java (original) (raw)

Last Updated : 31 Mar, 2022

java.lang.reflect.Parameter class provides Information about method parameters, including their name and modifiers. It also provides an alternate means of obtaining attributes for the parameter.

Some useful methods of Parameter class are:

  1. public int getModifiers(): It returns the modifier flags for the parameter represented by this Parameter object.
  2. public String getName(): It returns the name of the method parameter.
  3. public Type getParameterizedType(): It returns the type of the parameter.

All the methods of java.lang.reflect.Parameter class is listed below as follows:

Method Description
equals(Object obj) Compares based on the executable and the index.
getAnnotatedType() Returns an AnnotatedType object that represents the use of a type to specify the type of the formal parameter represented by this Parameter.
getAnnotation(Class annotationClass) Returns this element’s annotation for the specified type if such an annotation is present, else null.
getAnnotations() Returns annotations that are present on this element.
getAnnotationsByType(Class annotationClass) Returns annotations that are associated with this element.
getDeclaredAnnotations() Returns annotations that are directly present on this element.
getDeclaredAnnotation(Class annotationClass) Returns this element’s annotation for the specified type if such an annotation is directly present, else null.
getDeclaringExecutable() Return the Executable which declares this parameter.
getDeclaredAnnotationsByType(Class annotationClass) Returns this element’s annotation(s) for the specified type if such annotations are either directly present or indirectly present.
getModifiers() Get the modifier flags for this parameter represented by this Parameter object.
getName() Returns the name of the parameter.
getParameterizedType() Returns a Type object that identifies the parameterized type for the parameter represented by this Parameter object.
getType() Returns a Class object that identifies the declared type for the parameter represented by this Parameter object.
hashCode() Returns a hash code based on the executable’s hash code and the index.
isImplicit() Returns true if the parameter is implicitly declared in the source code else returning false
isNamePresent() Returning true if the parameter has the name in accordance with the class file
isVarArgs() Returning true if this parameter represents a variable argument list.
isSynthetic() Returning true if a parameter is not implicitly nor explicitly declared else false
toString() Returns a string describing this parameter

Example:

Java

import java.lang.reflect.*;

class Calculate {

`` int add( int a, int b)

`` {

`` return (a + b);

`` }

`` int mul( int a, int b)

`` {

`` return (b * a);

`` }

`` long subtract( long a, long b)

`` {

`` return (a - b);

`` }

}

public class GFG {

`` public static void main(String[] args)

`` {

`` Class cls = Calculate. class ;

`` Method[] methods = cls.getDeclaredMethods();

`` for (Method method : methods) {

`` System.out.print(

`` "Method Name: " + method.getName() + " " );

`` Parameter parameters[] = method.getParameters();

`` System.out.println( "\nparameters of "

`` + method.getName()

`` + "() methods: " );

`` for (Parameter parameter : parameters) {

`` System.out.print(

`` parameter.getParameterizedType() + " " );

`` System.out.print(parameter.getName() + " " );

`` }

`` System.out.print( "\n\n" );

`` }

`` }

}

Output

Method Name: subtract parameters of subtract() methods: long arg0 long arg1

Method Name: add parameters of add() methods: int arg0 int arg1

Method Name: mul parameters of mul() methods: int arg0 int arg1

Similar Reads