Skip to content
Advertisement

Reflection generic get field value

I am trying to obtain a field’s value via reflection. The problem is I don’t know the field’s type and have to decide it while getting the value.

This code results with this exception:

Can not set java.lang.String field com….fieldName to java.lang.String

JavaScript

I tried to cast, but I get compilation errors:

JavaScript

or

JavaScript

How can I do this?

Advertisement

Answer

Like answered before, you should use:

JavaScript

Another way, which is sometimes prefered, is calling the getter dynamically. example code:

JavaScript

Also be aware that when your class inherits from another class, you need to recursively determine the Field. for instance, to fetch all Fields of a given class;

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement