java 获取类层次结构的所有字段

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13685311/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 13:36:05  来源:igfitidea点击:

Get all Fields of class hierarchy

javaclassreflection

提问by Dmitry Nelepov

I have classes:

我有课:

ClassA{
 public String filedA;
}

ClassB extends ClassA{
 public String filedB;
}

ClassC extends ClassB{
 public String filedC;
}

Then I create object:

然后我创建对象:

ClassC c=new ClassC();
c.fieldC="TestC";
c.fieldA="TestA";
c.fieldB="TestB";

After I try get all fields, I call

在我尝试获取所有字段后,我打电话

Field[] fields=c.getClass().getDeclaredFields();

But I get array with only one item

但我得到的数组只有一项

fields[fieldC]

How to get all fields from all classes include extends?

如何从所有类中获取所有字段包括扩展?

回答by tjg184

Try the following:

请尝试以下操作:

Field[] fields = c.getClass().getFields();

If you want all superclass fields, see the following:

如果您想要所有超类字段,请参阅以下内容:

Retrieving the inherited attribute names/values using Java Reflection

使用 Java 反射检索继承的属性名称/值

回答by logoff

Your C class does not extend any class. Then, getDeclaredFields()only returns String filedCas you have seen. You cannot do c.fieldA="TestA"and c.fieldB="TestB"because your class does not declare this fields. Anyway, in case of C extends B and B extends A, method getFields()returns only public fields (including inherited):

您的 C 类不扩展任何类。然后,getDeclaredFields()只返回String filedC如您所见。你不能这样做c.fieldA="TestA"c.fieldB="TestB"因为你的班级没有声明这个领域。无论如何,在 C 扩展 B 和 B 扩展 A 的情况下,方法getFields()仅返回公共字段(包括继承的):

Returns an array containing Field objects reflecting all the accessible public fields of the class or interface represented by this Class object.

返回一个包含 Field 对象的数组,这些对象反映了此 Class 对象表示的类或接口的所有可访问公共字段。

And getDeclaredFields()returns all fields declared in the class (not including inherited):

并且getDeclaredFields()返回类中声明的所有字段(不包括继承的):

Returns an array of Field objects reflecting all the fields declared by the class or interface represented by this Class object. This includes public, protected, default (package) access, and private fields, but excludes inherited fields.

返回一个 Field 对象数组,这些对象反映了由此 Class 对象表示的类或接口声明的所有字段。这包括公共、受保护、默认(包)访问和私有字段,但不包括继承的字段。

回答by madx

If you don't want to reinvent the wheel you could rely upon Apache Commons Langversion 3.2+ which provides FieldUtils.getAllFieldsList:

如果您不想重新发明轮子,您可以依赖Apache Commons Lang3.2+ 版,它提供FieldUtils.getAllFieldsList

import java.lang.reflect.Field;
import java.util.AbstractCollection;
import java.util.AbstractList;
import java.util.AbstractSequentialList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

import org.apache.commons.lang3.reflect.FieldUtils;
import org.junit.Assert;
import org.junit.Test;

public class FieldUtilsTest {

    @Test
    public void testGetAllFieldsList() {

        // Get all fields in this class and all of its parents
        final List<Field> allFields = FieldUtils.getAllFieldsList(LinkedList.class);

        // Get the fields form each individual class in the type's hierarchy
        final List<Field> allFieldsClass = Arrays.asList(LinkedList.class.getFields());
        final List<Field> allFieldsParent = Arrays.asList(AbstractSequentialList.class.getFields());
        final List<Field> allFieldsParentsParent = Arrays.asList(AbstractList.class.getFields());
        final List<Field> allFieldsParentsParentsParent = Arrays.asList(AbstractCollection.class.getFields());

        // Test that `getAllFieldsList` did truly get all of the fields of the the class and all its parents 
        Assert.assertTrue(allFields.containsAll(allFieldsClass));
        Assert.assertTrue(allFields.containsAll(allFieldsParent));
        Assert.assertTrue(allFields.containsAll(allFieldsParentsParent));
        Assert.assertTrue(allFields.containsAll(allFieldsParentsParentsParent));
    }
}

回答by Brian Agnew

This would work with reflection if ClassCderived from ClassB(and presumably from ClassAetc..). I assume this is a typo ? Then this:

如果ClassC源自ClassB(并且可能来自ClassA等),这将与反射一起使用。我认为这是一个错字?然后这个:

Field[] fields = c.getClass().getFields();

would work as expected.

会按预期工作。

回答by user902383

getDeclaredFields()which you are using, are not contain inherited flields from superclass.

getDeclaredFields()您正在使用的不包含从超类继承的字段。

if you want all clields just use getFields()method

如果你想要所有的clields只是使用getFields()方法

回答by sjngm

You should be able to get them with

你应该能够得到它们

Field[] fields = c.getClass().getFields();

This returns all accessible fields.

这将返回所有可访问的字段。