lundi 13 avril 2015

Argument type mismatch when attempting to invoke method

Say I have a parameters array:



Object[] parameters;


I also have a types list, that stores parameter types of a method.



List<Class<?>> types = Arrays.asList(Taxi.class, Bus.class);


Now, I fill the parameters array, with the given types in the types list:



int index = 0;
for (Class<?> type : types) {
if (Taxi.class.isAssignableFrom(type)) {
parameters[index] = new Taxi();
} else if (Bus.class.isAssignableFrom(type)) {
parameters[index] = new Bus();
}
index ++;
}


Now I try to invoke the method that has the exactly same parameters as the types list:



someMethod.invoke(someObject, parameters);


And Java gives me an IllegalArgumentException: argument type mismatch. I understand why would this happen, as parameters is an Object array; are there any workarounds, though? Maybe like casting? But how?






Aucun commentaire:

Enregistrer un commentaire