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.
Java
import java.lang.reflect.Method;
public class GFG {
public static void main(String[] args)
{
try {
Class classobj = GFG. class ;
Method[] methods = classobj.getMethods();
for (Method method : methods) {
if (method.getName().equals("setValue")
|| method.getName().equals("getValue")
|| method.getName().equals("setManyValues")) {
Class[] parameters = method.getParameterTypes();
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();
}
}
public void setValue(String value1, String value2)
{
System.out.println("setValue");
}
public String getValue()
{
System.out.println("getValue");
return "getValue";
}
public void setManyValues( int value1, double value2, String value3)
{
System.out.println("setManyValues");
}
}
|
Output:
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.
Java
import java.lang.reflect.Method;
import java.lang.reflect.Type;
public class GFG3 {
public static void main(String[] args)
{
try {
Class classobj = sample. class ;
Method[] methods = classobj.getMethods();
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);
}
}
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();
}
}
private static int containsParameter(Method method, Class parameterName)
{
int count = 0 ;
Class parameters[] = method.getParameterTypes();
for ( int i = 0 ; i < parameters.length; i++) {
if (parameters[i] == parameterName) {
count++;
}
}
return count;
}
}
class sample {
public void setValue(String value1, String value2)
{
System.out.println("setValue");
}
public void setManyValues( int value1, double value2, String value3)
{
System.out.println("setManyValues");
}
}
|
Output:
No of String Parameters in setValue(): 2
No of int Parameters in setManyValues(): 1
Reference: Oracle Doc for getParameterTypes()
Last Updated :
18 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...