Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the value of a field, given the hierarchical path

Tags:

java

recursion

I have an object that contains sub properties, which also have sub properties and so on.

I basically need to find the best way to retrieve the value of a particular field on the object, given it's full hierarchical path as a string.

For example, if the object has the field company (Object) which has the field client (Object) which has the field id (String), this path will be represented as company.client.id. Therefore, given a path to the field i'm trying to get the value of on an object, how would I go about doing this?

Cheers.

like image 544
Rob Harris Avatar asked Oct 21 '12 15:10

Rob Harris


1 Answers

You can use Apache Commons BeanUtils PropertyUtilsBean.

Example of use :

PropertyUtilsBean pub = new PropertyUtilsBean();
Object property = pub.getProperty(yourObject, "company.client.id");
like image 56
Radouane ROUFID Avatar answered Sep 22 '22 16:09

Radouane ROUFID