get All Fields from a Class - Android java.lang.reflect

Android examples for java.lang.reflect:Field Name

Description

get All Fields from a Class

Demo Code


//package com.java2s;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Main {
    public static List<Field> getAllFields(Class clazz) {
        List<Field> results = new ArrayList<Field>();

        Class superClass = clazz;
        while (superClass != Object.class) {
            results.addAll(Arrays.asList(superClass.getFields()));
            results.addAll(Arrays.asList(superClass.getDeclaredFields()));
            superClass = superClass.getSuperclass();
        }//ww  w.ja  v  a  2 s . c o  m

        return results;
    }
}

Related Tutorials