I am trying to use reflection to invoke a method which takes a varg as an argument.
This is the method I am trying to invoke
public void serve(Object... args)
{
System.out.println("Hi!");
}
This is my code to invoke this method through reflection
Object[] params = new Object[] {new Object(), new Object()};
Class<?> cls = Class.forName("com.reflec.Serve");
Service service = (Service) cls.newInstance();
for(Method method : cls.getMethods())
{
if(method.getName().equals("serve"))
{
method.invoke(service, params);
}
}
However I keep getting the following error java.lang.IllegalArgumentException: wrong number of arguments
Anyone got any ideas?
Aucun commentaire:
Enregistrer un commentaire