This question already has an answer here:
Hello, I have a class:
package com.example.test;
public class Test {
public static void doSomething(Object[] objects) {
return;
}
}
what I want to do is to get that class (and invoke later) with reflection. The problem is that I have the name of method and type of param given as Strings. Here is the code that I tried to solve this issue with:
String methodName = "doSomething";
String paramType = "java.lang.Object";
try {
Class<?> paramTypeClass = Class.forName(paramType);
com.example.test.Test.class.getDeclaredMethod(methodName, paramTypeClass);
} catch (ClassNotFoundException | NoSuchMethodException e) {
e.printStackTrace();
}
it throws a NoSuchMethodException:
java.lang.NoSuchMethodException: com.example.test.Test.doSomething(java.lang.Object)
I also tried with paramType set to "java.lang.Object...", but it throws this on the 4th line of my code above:
java.lang.ClassNotFoundException: java/lang/Object///
also with "java.lang.Object[]":
java.lang.ClassNotFoundException: java/lang/Object[]
What should I put as the methodName, or how can I change my code in a different way to solve this problem?
Aucun commentaire:
Enregistrer un commentaire