jeudi 11 juin 2020

Java reflect method invocation dynamic args

Can you invoke a java reflect method with a dynamic number of arguments? Say you have the method updateJsonNode

public static void updateJsonNode(String arg1, JsonNode arg2, JsonNode arg3, String arg4){
// Do stuff here
}

What i need to know is if you can pass an object array in as arguments or if you are forced to actually invoke the method using the correct types? Or are you forced to define the method taking the argument (Object[]... objects) and simply cast the objects in the method?

Object[] objects = new Object[5];
objects = {"arg1", *JsonNode*, *JsonNode*, "arg4"}
try{
method.invoke(null, objects);
}catch(...){
...
}




Aucun commentaire:

Enregistrer un commentaire