1

无论如何使用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")
4

1 回答 1

0

由于没有人提出解决方案,我必须在这里回答我的问题。

感谢 Spring,这已经实现:

public static <T> PropertyDescriptor getPropertyDescriptor(T rootObject, String path)
{
    BeanWrapperImpl wrapper = new BeanWrapperImpl(rootObject);
    return wrapper.getPropertyDescriptor(path);
}

除了问题中提到的要求外,它还支持地图。

于 2012-11-05T10:49:22.430 回答