What is the best way to get a strongly typed value of a property, using the property name as string, of an object in Java.
For eg: A Person class with age field as an integer, say 21. If the following statement has to return integer 21 for age field, then what should be the implementation of get method? [Note: 21 is to be returned as integer]
ObjectUtils.get(person, "age");
Sample method header
public static <E> E get(Object object, String fieldName);
One way is to get the type of the field and type cast explicitly.
Other way is to use getProperty method in BeanUtils class (Apache commons library). But, the limitation is that it returns only string, but not the strongly typed value.
Is there any better approach or library utility available to achieve this?