Olla estou tentando fazer o seguinte exemplo, para fazer uma classe mais nobre e parar de replicar código:
public void gerarPedidosTeste(){
System.out.println("Teste via reflection");
}
public static void main(String[] args) {
try {
System.out.println("OPS");
Class c = Class.forName("br.com.pedrosa.Teste");
Teste a = (Teste) c.newInstance();
for (Method method : a.getClass().getMethods()) {
String m = method.getName();
System.out.println(m);
if (m.equals("gerarPedidosTeste")) {
method.invoke(a,m);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
E causa o seguinte erro:
java.lang.IllegalArgumentException: wrong number of arguments
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at br.com.pedrosa.Teste.main(Teste.java:15)
Estou passando os 2 objetos no invoke e causa a seguinte o exception.
java.lang.NoSuchMethodException: br.com.pedrosa.Teste.gerarArquivosTeste()
at java.lang.Class.getMethod(Unknown Source)
at br.com.pedrosa.Teste.testeReflection(Teste.java:637)
at br.com.pedrosa.Teste.service(Teste.java:87)