[英]Access Nested Field using Java Reflection
Suppose that I have several classes in this style:假设我有几个这种风格的类:
public class A {
public B element;
}
public class B {
public C anotherElement; // where C refers to another type which contains types D, E, F, etc.....
}
I want to iterate over all of the contained sub-classes (not inherited) and get a list of all fields in the nested object tree.我想遍历所有包含的子类(未继承)并获取嵌套对象树中所有字段的列表。 However, when I use reflection code like this:
但是,当我使用这样的反射代码时:
Field[] myFields = providedObject.getClass().getDeclaredFields();
for (Field myField : myFields) {
// do something here?? to access the sub-fields of the class
// if I print out myField.getName(), I get the element of class A, but I also want to access the anotherElement of class B (without hard-coding the name 'anotherElement', I want a full traversal of all nested fields)
}
At the 'do something here' step, I want to access the sub-fields of myField, but I don't see anything in the Field api that directly lets me do this.在“在此处执行操作”步骤中,我想访问 myField 的子字段,但在 Field api 中看不到任何直接让我执行此操作的内容。 I've tried:
我试过了:
myField.getDeclaringClass().getDeclaredFields() -> this seems to return the same element that we already saw and not the sub-elements myField.getDeclaringClass().getDeclaredFields() -> 这似乎返回了我们已经看到的相同元素而不是子元素
and和
myField.getClass().getDeclaredFields() -> this seems to return the fields of the 'Field' class and not my class A or B myField.getClass().getDeclaredFields() -> 这似乎返回了“Field”类的字段,而不是我的 A 类或 B 类
So how would I access the nested fields from the reflection api?那么我将如何从反射 api 访问嵌套字段?
Thanks.谢谢。
I've added following statement to quit recursive method.我添加了以下语句来退出递归方法。 You can change it whatever you want.
你可以随心所欲地改变它。
if (clazz == null || clazz.isPrimitive() || !clazz.getPackage().getName().startsWith("com.vvv.stack02")) {
return;
}
The main idea is you need to convert field to a class via typing following line.主要思想是您需要通过键入以下行将字段转换为类。
Class<?> fieldClass = myField.getType();
public class Main {
public static void main(String[] args) {
printFields(A.class);
}
public static void printFields(Class<?> clazz) {
if (clazz == null || clazz.isPrimitive() || !clazz.getPackage().getName().startsWith("com.vvv.stack02")) {
return;
}
Field[] myFields = clazz.getDeclaredFields();
for (Field myField : myFields) {
System.out.println(clazz.getSimpleName() + "->" + myField.getType().getSimpleName() + ":" + myField.getName());
Class<?> fieldClass = myField.getType();
printField(fieldClass);
}
}
public static class A {
public B element;
}
public static class B {
public C anotherElement; // where C refers to another type which contains types D, E, F, etc.....
}
public static class C {
public Integer a;
}
}
It's just a bit of recursion:这只是一点递归:
public static void traverseDepthFirst(Object obj) { // May need throws/catch!
if (obj == null) {
// ... do something for null ...
return;
}
// ... perhaps do something different for arrays, primitives, String, etc.
for (Field field : obj.getClass().getDeclaredFields()) {
field.setAccessible(true);
// ... pre-traversal code ...
traverseDepthFirst(field.get(obj));
// ... post-traversal code ...
}
}
Edit: That will only consider the leaf class.编辑:那只会考虑叶子类。 To include super classes.
包括超级类。
public static void traverseDepthFirst(Object obj) { // May need throws/catch!
if (obj == null) {
// ... do something for null ...
return;
}
// ... perhaps do something different for arrays, primitives, String, etc.
for (
Class<?> clazz = obj.getClass();
clazz != null;
clazz = clazz.getSuperclass()
) {
for (Field field : clazz.getDeclaredFields()) {
field.setAccessible(true);
// ... pre-traversal code ...
traverseDepthFirst(field.get(obj));
// ... post-traversal code ...
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.