我一直在尝试在Java中使用Reflection,但它并没有很好地结束.这是我的代码:
public class ReflectionTest {
public static void main(String[] args) {
ReflectionTest test = new ReflectionTest();
try {
Method m = test.getClass().getDeclaredMethod("Test");
m.invoke(test.getClass(), "Cool story bro");
} catch (NoSuchMethodException | SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void Test(String someawesometext) {
System.out.println(someawesometext);
}
}
我刚刚收到java.lang.NoSuchMethodException错误,我已经尝试了很多东西.
就像使用getMethod而不是getDeclaredMethod一样,在getDeclaredMethod中的“Test”之后添加test.getClass()等等.
这是堆栈跟踪:
java.lang.NoSuchMethodException: ReflectionTest.Test()
at java.lang.Class.getDeclaredMethod(Unknown Source)
at ReflectionTest.main(ReflectionTest.java:10)
我已经谷歌搜索了很多天但没有运气.所以我知道我应该怎么解决这个问题?