get All Fields from class - Java Reflection

Java examples for Reflection:Field Get

Description

get All Fields from class

Demo Code


//package com.java2s;

import java.lang.reflect.Field;

import java.util.ArrayList;
import java.util.List;

public class Main {
 

    public static List<Field> getAllFields(Class<?> type) {
        return getAllFields(null, type);
    }//from w w  w  . j  a  va 2 s  . co m

    private static List<Field> getAllFields(List<Field> fields,
            Class<?> type) {
        if (fields == null)
            fields = new ArrayList<Field>();
        for (Field field : type.getDeclaredFields()) {
            fields.add(field);
        }

        if (type.getSuperclass() != null) {
            fields = getAllFields(fields, type.getSuperclass());
        }

        return fields;
    }
}

Related Tutorials