Reflection - Dynamic parameter types
I have a class that invokes a method via reflection based on a few parameters. Here is some pseudocode representing my current class.
Code:
public runMethod(String className, String methodName) {
Class<?>[] parameterTypes = new Class<?>{Map.class}; // Predefined now but I want to be dynamic
Object[] parameters = new Object[] {}; // Defined elsewhere.
Class clazz = Class.forName(className);
Method method = clazz.getMethod(methodName, parameterTypes);
method.invoke(obj, parameters); // obj defined elsewhere
}
In order for this class to work the method it invokes must have the same parameter types in every case. This is working just fine but I want to make it a bit more useful by making the method parameters dynamic, sort of like how Spring MVC request methods work. The problem is the Java reflection API requires me to know the parameter types when asking for the method. I am wondering if there is a way of getting a Method object without having to provide a fixed set of parameter types? Is there a better way of achieving my stated objective that I am not thinking of? I am open to suggestions. Thanks in advance.
Re: Reflection - Dynamic parameter types
Any help would be greatly appreciated.
Re: Reflection - Dynamic parameter types
Looking at the API (Class (Java Platform SE 7 ))
You should be able to get an array of all the methods for the class.
Re: Reflection - Dynamic parameter types
I am aware of the getMethods() call in the Class object. I was hoping to avoid the potentially slow looping to find the methods with a given name. It seems like a oversight to not have a way of getting all methods of a given name. Overloading a method is such a simple thing in Java that I would figure someone would have created a better way to do this.