Get all fields (even private and inherited) from c

2020-02-17 04:40发布

I am making university project.
I need to get all fields from class. Even private and inherited. I tried to get all declared fields and then cast to super class and repeat. Fragment of my code:

private void listAllFields(Object obj) {
    List<Field> fieldList = new ArrayList<Field>();
    while (obj != null) {
        fieldList.addAll(Arrays.asList(obj.getClass().getDeclaredFields()));
        obj = obj.getClass().getSuperclass().cast(obj);
    }
    // rest of code

But it does not work. tmpObj after casting is still the same class (not superclass).
I will appreciate any help how to fix casting problem, or how to retrieve these fields in different way.

Problem is not to gain access to fields, but to get names of fields!
I manages it that way:

private void listAllFields(Object obj) {
    List<Field> fieldList = new ArrayList<Field>();
    Class tmpClass = obj.getClass();
    while (tmpClass != null) {
        fieldList.addAll(Arrays.asList(tmpClass .getDeclaredFields()));
        tmpClass = tmpClass .getSuperclass();
    }
    // rest of code

9条回答
三岁会撩人
2楼-- · 2020-02-17 05:07

Here is the method I use to get all the fields of an object

private <T> List<Field> getFields(T t) {
        List<Field> fields = new ArrayList<>();
        Class clazz = t.getClass();
        while (clazz != Object.class) {
            fields.addAll(Arrays.asList(clazz.getDeclaredFields()));
            clazz = clazz.getSuperclass();
        }
        return fields;
    }
查看更多
乱世女痞
3楼-- · 2020-02-17 05:08

Previously asked.

Access to private inherited fields via reflection in Java

In your code, set field.setAccessible(true);

查看更多
老娘就宠你
4楼-- · 2020-02-17 05:11

try these

How do I read a private field in Java?

Is it possible in Java to access private fields via reflection

Field.setAccessible(true) is what you have to do to make it accessible via reflection

import java.lang.reflect.Field;

class B 
{
    private int i = 5;
}

public class A extends B 
{
    public static void main(String[] args) throws Exception 
    {
        A a = new A();
        Field[] fs = a.getClass().getSuperclass().getDeclaredFields();
        for (Field field : fs)
        {
            field.setAccessible(true);
            System.out.println( field.get( a ) );
        }
    }
}
查看更多
Deceive 欺骗
5楼-- · 2020-02-17 05:16
ClassName obj  =  new ClassName();
Field field  =   ClassName.class.getDeclaredField  ("name of the field");
field.setAccessible   (true);
DataType value  =  (DataType) field.get  (obj);

You have to use setAccessible to true before extracting the value. Sometime JVM does not allow to extract the private variable, it may give Security Exception.

查看更多
爷、活的狠高调
6楼-- · 2020-02-17 05:17
obj = obj.getClass().getSuperclass().cast(obj);

This line does not do what you expect it to do. Casting an Object does not actually change it, it just tells the compiler to treat it as something else.

E.g. you can cast a List to a Collection, but it will still remain a List.

However, looping up through the super classes to access fields works fine without casting:

Class<?> current = yourClass;
while(current.getSuperclass()!=null){ // we don't want to process Object.class
    // do something with current's fields
    current = current.getSuperclass();
}

BTW, if you have access to the Spring Framework, there is a handy method for looping through the fields of a class and all super classes:
ReflectionUtils.doWithFields(baseClass, FieldCallback)
(also see this previous answer of mine: Access to private inherited fields via reflection in Java)

查看更多
我命由我不由天
7楼-- · 2020-02-17 05:17

To get superclass fields use getSuperclass(). From there you can get fields of superclass.

查看更多
登录 后发表回答