I am getting "java.lang.NoSuchMethodException" even though i can see my method in classType.getMethods(); Please help
ShortestPathAlgorithm.java
public class ShortestPathAlgorithm {
public void show(int[][] graph, int from, int des){
// some complex code :D
}
}
SPATest.java
public class SPATest {
public static void main(String[] args) {
int graph[][] = {
{0, 1, 5, 4, 0},
{1, 0, 0, 2, 4},
{5, 0, 0, 1, 0},
{4, 2, 1, 0, 1},
{0, 4, 0, 1, 0},
};
Integer from = 1;
Integer des = 2;
RunWithTimeTrace.run(ShortestPathAlgorithm.class, "show", graph, from, des);
}
}
RunWithTimeTrace.java
public class RunWithTimeTrace {
public static void run(Class<?> classType, String methodName, Object... paramValues ){
try{
Object o = classType.newInstance();
int n = paramValues.length;
Class<?>[] arr = new Class[n];
for(int i=0; i < n; i++){
arr[i] = paramValues[i].getClass();
}
Method[] declaredMethods = classType.getMethods();
System.out.println(Arrays.toString(declaredMethods));
System.out.println("------------------------");
Method method = classType.getDeclaredMethod(methodName, arr);
long s = System.currentTimeMillis();
method.invoke(o, paramValues);
long t = System.currentTimeMillis();
System.out.println("Time Taken : " + (t - s));
}catch(Exception e){
e.printStackTrace();
}
}
}
I am able to see my method in getMethods but not in reflection. Why ??
Output :
[public void demo.ds.graph.main.ShortestPathAlgorithm.show(int[][],int,int), public final void java.lang.Object.wait() throws java.lang.InterruptedException, public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException, public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException, public boolean java.lang.Object.equals(java.lang.Object), public java.lang.String java.lang.Object.toString(), public native int java.lang.Object.hashCode(), public final native java.lang.Class java.lang.Object.getClass(), public final native void java.lang.Object.notify(), public final native void java.lang.Object.notifyAll()]
------------------------
java.lang.NoSuchMethodException: demo.ds.graph.main.ShortestPathAlgorithm.show([[I, java.lang.Integer, java.lang.Integer)
at java.lang.Class.getDeclaredMethod(Class.java:2130)
at demo.ds.graph.test.RunWithTimeTrace.run(RunWithTimeTrace.java:23)
at demo.ds.graph.test.SPATest.main(SPATest.java:20)
Aucun commentaire:
Enregistrer un commentaire