简体   繁体   English

java反射库,它支持获取嵌套的对象字段值。

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

Is there knowing java reflection library which can get value of fields easily. 是否有已知的Java反射库,可以轻松获取字段的值。 Ex . 例如 If I have Address Object inside User Object. 如果我在用户对象中有地址对象。 And Address object have city attribute. 并且Address对象具有城市属性。

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. 然后我想将address.city作为参数传递给User Object,我想获得用户所在的城市。

Is there any library which support my requirement. 有没有支持我的要求的图书馆。

Apache commons beanutils has PropertyUtils.getNestedProperty(user, "address.city") . Apache commons beanutils具有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. 您也可以执行PropertyUtils.setNestedProperty(user, "address.city", "new city")但需要确保该地址不为null。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM