Using Class.getMethod(), we can check whether a method with given name exists or not.
public Method getMethod(String name, Class<?>... parameterTypes) throws NoSuchMethodException, SecurityException
Return a Method object, if the method with given name and respective parameters exists, else it throws NoSuchMethodException.
Below snippet return true, if the method name exists with respective arguments, else false.
public static boolean isMethodExist(Class clazz, String methodName, Class<?>... parameterTypes) {
try {
clazz.getMethod(methodName, parameterTypes);
} catch (NoSuchMethodException e) {
return false;
}
return true;
}
If you are not sure about the method arguments and interested only in method name, you can use getDeclaredMethods method. Class.getDeclaredMethods return all the methods declared in this class.
Below snippet return true, if the method with given name exists, else false.
public static boolean isMethodExist(Class clazz, String methodName) {
try {
Method[] declaredMethods = clazz.getDeclaredMethods();
for (Method method : declaredMethods) {
if (method.getName().equals(methodName)) {
return true;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
Find the below working application.
ArithmeticUtil.java
package com.sample.app.util;
public class ArithmeticUtil {
public int add(int a, int b) {
return a + b;
}
public int add(int a, int b, int c) {
return a + b + c;
}
public int sub(int a, int b) {
return a - b;
}
}
ReflectionUtil.java
package com.sample.app.util;
import java.lang.reflect.Method;
public class ReflectionUtil {
public static boolean isMethodExist(Class clazz, String methodName, Class<?>... parameterTypes) {
try {
clazz.getMethod(methodName, parameterTypes);
} catch (NoSuchMethodException e) {
return false;
}
return true;
}
public static boolean isMethodExist(Class clazz, String methodName) {
try {
Method[] declaredMethods = clazz.getDeclaredMethods();
for (Method method : declaredMethods) {
if (method.getName().equals(methodName)) {
return true;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
}
App.java
package com.sample.app;
import com.sample.app.util.ArithmeticUtil;
import com.sample.app.util.ReflectionUtil;
public class App {
public static void main(String[] args) {
boolean exists = ReflectionUtil.isMethodExist(ArithmeticUtil.class, "add", int.class, int.class);
System.out.println("Is add method with two integer arguments exists : " + exists);
exists = ReflectionUtil.isMethodExist(ArithmeticUtil.class, "sub", int.class, int.class);
System.out.println("Is sub method with two integer arguments exists : " + exists);
exists = ReflectionUtil.isMethodExist(ArithmeticUtil.class, "mul", int.class, int.class);
System.out.println("Is mul method with two integer arguments exists : " + exists);
exists = ReflectionUtil.isMethodExist(ArithmeticUtil.class, "add");
System.out.println("\nIs add method exists : " + exists);
exists = ReflectionUtil.isMethodExist(ArithmeticUtil.class, "sub");
System.out.println("Is sub method exists : " + exists);
exists = ReflectionUtil.isMethodExist(ArithmeticUtil.class, "mul");
System.out.println("Is mul method exists : " + exists);
}
}
Output
Is add method with two integer arguments exists : true Is sub method with two integer arguments exists : true Is mul method with two integer arguments exists : false Is add method exists : true Is sub method exists : true Is mul method exists : false
No comments:
Post a Comment