Chamly Idunil
Chamly Idunil

Reputation: 1872

java reflection library which support to get nested object field values.

Is there knowing java reflection library which can get value of fields easily. Ex . If I have Address Object inside User Object. And Address object have city attribute.

public class Address {
    private String city;
}

public class User {
    private String name;
    private Address address;
}

Then i want to pass address.city as parameter with User Object and i want to get city of user.

Is there any library which support my requirement.

Upvotes: 3

Views: 2966

Answers (1)

juvenislux
juvenislux

Reputation: 300

Apache commons beanutils has PropertyUtils.getNestedProperty(user, "address.city").

You can also do PropertyUtils.setNestedProperty(user, "address.city", "new city") but you need to make sure that address is not null.

Upvotes: 6

Related Questions