【发布时间】:2021-05-31 21:37:17
【问题描述】:
从标题中我需要使用反射来迭代一个对象。我有什么:
@Getter
@Setter
public static class ObjectA {
public ObjectB obj;
}
@Getter
@Setter
public static class ObjectB {
public String value;
}
String pathValueRetrived = "ObjectA.obj.value";
我必须以通用方式检索 value 属性。
类似:
String[] pathValue = StringUtils.split(pathValueRetrived, ".");
ObjectA objectA = //retrive the object with values
Object iterator;
for(String idxPath : pathValue){
if(iterator != null)
iterator = iterator.getClass().getDeclaredField(path);
else
iterator = objectA.getClass().getDeclaredField(path);
}
非常感谢您的帮助!
【问题讨论】:
-
另外,请仅使用您实际使用的 java 版本标记您的问题。 不提供确切的 java 版本也可以,因为您不会将自己限制为特定于版本的语言功能
-
我刚刚编辑过。谢谢!
标签: java reflection