I would like to call method(using reflection) which has parameter Interface - i.e: List but with implementation of List. For example:
public class Test {
public static void main(String[] args) throws NoSuchMethodException {
Method method = Test1.class.getMethod("method", new Class[]{ArrayList.class});
}
public class Test1 {
public void method(List list) {
System.out.println("method");
}
}
}
I get NoSuchMethodException. In this case i know which params i get, problem is that I want to use this in general when i don't "statically" know param types.
Is possible that getMethod returns also method which has interface as parameter? Or i have to write my own "methodsearcher"
Thank you.
EDIT: It's much more complicated. I'm trying to write something like "dynamic modular architecture" in my program. I have Core, which should comunicate with other modules. So i don't know params classes in programming time but in runtime.
public Object processMessage(String target, String methodName, List<Object> params, Object returnNonExist) {
Module m = modules.get(target);
if (m == null) {
return returnNonExist;
} else {
Class[] paramsTypes = new Class[params.size()];
for (int i = 0; i < params.size(); i++) {
paramsTypes[i] = params.get(i).getClass();
}
}
try {
Method method = m.getClass().getMethod(methodName, paramsTypes);
Object result = method.invoke(m, params.toArray());
return result;
}
Is it better?