get All Fields Include Super Class - Java Reflection

Java examples for Reflection:Field Get

Description

get All Fields Include Super Class

Demo Code


//package com.java2s;
import java.lang.reflect.Field;

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

public class Main {
    public static void main(String[] argv) throws Exception {
        Class clazz = String.class;
        System.out.println(getAllFieldsIncludeSuperClass(clazz));
    }/*from  w  w  w. j a  v a2 s.co m*/

    public static List<Field> getAllFieldsIncludeSuperClass(
            final Class<?> clazz) {
        List<Field> lf = new ArrayList<Field>();
        for (Class<?> superClass = clazz; superClass != Object.class; superClass = superClass
                .getSuperclass()) {
            Field[] f = superClass.getDeclaredFields();
            for (int j = 0; j < f.length; j++) {
                lf.add(f[j]);
            }
        }
        return lf;
    }
}

Related Tutorials