有没有办法使用 Java 反射递归地查找属性描述符?
想象一个场景,其中 User 类有一个名为 profile 的字段,它本身是另一个具有 email 属性的类。
通过拥有用户对象,我需要访问 profile.email,因此所需的方法签名应该与此类似:
public PropertyDescriptor findPropertyDescriptor(Class<?> clazz, String path)
{
// Code!
}
调用类似于:
findPropertyDescriptor(User.class, "profile.email")
我还认为像下面这样的调用也应该是可能的:
findPropertyDescriptor(User.class, "addresses[2].postCode")
最佳答案
由于没有人提出解决方案,所以我必须在这里回答我的问题。
感谢 Spring,这已经实现了:
public static <T> PropertyDescriptor getPropertyDescriptor(T rootObject, String path)
{
BeanWrapperImpl wrapper = new BeanWrapperImpl(rootObject);
return wrapper.getPropertyDescriptor(path);
}
除了问题中提到的要求外,它还支持 map 。
关于java - 在 Java 中使用 Reflection 递归查找 PropertyDescriptor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12940979/