Exorcismus
Exorcismus

Reputation: 2482

generic Caster for method reflection

I have a Map of methods(getter methods) , I invoke them on some object , the getter method returns different types of data i.e. int string etc... am trying to do something like

String col1= <method name>; //getId()
Class c = colGetterMap.get(col1).getReturnType();
(c) colGetterMap.get(col1).invoke(object);

but am getting

cannot find symbol Class c

Upvotes: 0

Views: 253

Answers (1)

user902383
user902383

Reputation: 8640

When you do casting, you put class name in brackets, ie String string = (String) obj

in your case, you are trying cast to instance. c is just instance of class Class<?>, so to do this properly, invoke cast method on your class instance c.cast(colGetterMap.get(col1).invoke(object)) now it should work

Upvotes: 2

Related Questions