get All Fields from class hierarchy - Android java.lang.reflect

Android examples for java.lang.reflect:Field

Description

get All Fields from class hierarchy

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 Field[] getAllFields(Class<?> klass) {
        List<Field> fields = new ArrayList<Field>();
        fields.addAll(Arrays.asList(klass.getDeclaredFields()));

        if (klass.getSuperclass() != null) {
            fields.addAll(Arrays.asList(getAllFields(klass.getSuperclass())));
        }/*  w  w  w .  java2 s  .  co  m*/

        return fields.toArray(new Field[] {});
    }
}

Related Tutorials