Cha*_*nil 3 java reflection core shared-libraries
是否有知道可以轻松获取字段值的Java反射库。前任 。如果我在用户对象中有地址对象。并且 Address 对象具有城市属性。
public class Address {
private String city;
}
public class User {
private String name;
private Address address;
}
Run Code Online (Sandbox Code Playgroud)
然后我想将 address.city 作为参数传递给用户对象,我想获取用户所在的城市。
是否有任何图书馆支持我的要求。
Apache commons beanutils有
PropertyUtils.getNestedProperty(user, "address.city").
您也可以这样做,PropertyUtils.setNestedProperty(user, "address.city", "new city")但您需要确保该地址不为空。