Method Class | getParameterTypes() Method in Java
Prerequisite : Java.lang.Class class in Java | Set 1, Java.lang.Class class in Java | Set 2
java.lang.reflectMethod class help us to get information of a single method on a class or interface. This class also provides access to the methods of classes and invoke them at runtime.
getParameterTypes() method of Method class:
For creating methods, many times parameters are needed for those method to work properly. The getParameterTypes() method of Method class returns an array of Class objects that represents the parameter types, declared in method at time of coding. The getParameterTypes() returns an array of length 0 if the method object takes no parameters.
Syntax:
public Class[] getParameterTypes()
Parameters: The method does not take any parameters.
Return Value: The method returns an array of Class object that represents the formal parameter types of the method object, in declaration order.
Below programs illustrate the getParameterTypes() method of Method class:
Program 1: Below program is going to print the details for array of Class objects that represent the formal parameter types of the method object defined in program.
/* * Program Demonstrate how to apply getParameterTypes() method * of Method Class. */ import java.lang.reflect.Method; public class GFG { // Main method public static void main(String[] args) { try { // Create class object Class classobj = GFG. class ; // Get Method Object Method[] methods = classobj.getMethods(); // Iterate through methods for (Method method : methods) { // We are only taking method defined in the demo class // We are not taking other methods of the object class if (method.getName().equals( "setValue" ) || method.getName().equals( "getValue" ) || method.getName().equals( "setManyValues" )) { // Apply getGenericParameterTypes() method Class[] parameters = method.getParameterTypes(); // Print parameter Types of method Object System.out.println( "\nMethod Name : " + method.getName()); System.out.println( "No of Parameters : " + parameters.length); System.out.println( "Parameter object details:" ); for (Class classobject : parameters) { System.out.println(classobject.getName()); } } } } catch (Exception e) { e.printStackTrace(); } } // Method containing two parameter public void setValue(String value1, String value2) { System.out.println( "setValue" ); } // Method containing no parameter public String getValue() { System.out.println( "getValue" ); return "getValue" ; } // Method containing many parameter public void setManyValues( int value1, double value2, String value3) { System.out.println( "setManyValues" ); } } |
Method Name : setManyValues No of Parameters : 3 Parameter object details: int double java.lang.String Method Name : getValue No of Parameters : 0 Parameter object details: Method Name : setValue No of Parameters : 2 Parameter object details: java.lang.String java.lang.String
Program 2: We are giving a parameter class object as input and below program going to count number of those type parameter if the Method object contains parameter otherwise program returns 0.
/* * Program Demonstrate how to apply getParameterTypes() method * of Method Class. */ import java.lang.reflect.Method; import java.lang.reflect.Type; public class GFG3 { // Main method public static void main(String[] args) { try { // Create class object Class classobj = sample. class ; Method[] methods = classobj.getMethods(); /* Check whether setManyValues() method contains int parameter or not and print no of string parameter it contains*/ for (Method method : methods) { if (method.getName().equals( "setValue" )) { int count = containsParameter(method, (Class)java.lang.String. class ); System.out.println( "No of String " + "Parameters in setValue(): " + count); } } /* Check whether setManyValues() method contains int parameter or not and print no of string parameter it contains */ for (Method method : methods) { if (method.getName().equals( "setManyValues" )) { int count = containsParameter(method, (Class) int . class ); System.out.println( "No of int Parameters" + " in setManyValues(): " + count); } } } catch (Exception e) { e.printStackTrace(); } } // Count no of parameters contain by method same as passed to method private static int containsParameter(Method method, Class parameterName) { int count = 0 ; // Get all parameter class objects using getParameterTypes() Class parameters[] = method.getParameterTypes(); for ( int i = 0 ; i < parameters.length; i++) { // Check contains parameter or not if (parameters[i] == parameterName) { count++; } } return count; } } // A simple class class sample { // Method containing two parameter public void setValue(String value1, String value2) { System.out.println( "setValue" ); } // Method containing many parameter public void setManyValues( int value1, double value2, String value3) { System.out.println( "setManyValues" ); } } |
No of String Parameters in setValue(): 2 No of int Parameters in setManyValues(): 1
Reference:
Oracle Doc for getParameterTypes()
Please Login to comment...