getDeclaredMethod não funciona, NoSuchMethodException
Eu tenho tentado usarReflection
em Java, mas não acaba muito bem. Aqui está meu código:
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);
}
}
Acabei de receber ojava.lang.NoSuchMethodException
erro, e eu tentei praticamente tudo. Gosta de usargetMethod
ao invés degetDeclaredMethod
, adicionartest.getClass()
depois de"Test"
emgetDeclaredMethod
e mais.
Aqui está o rastreamento de pilha:
java.lang.NoSuchMethodException: ReflectionTest.Test()
at java.lang.Class.getDeclaredMethod(Unknown Source)
at ReflectionTest.main(ReflectionTest.java:10)
Eu tenho pesquisado por muitos dias agora, mas sem sorte. Então, alguém sabe como devo consertar isso?