Get all declared fields from Class Note : getDeclaredFields() may return private attributes, but does not return Parents fields - Java Reflection

Java examples for Reflection:Field Get

Description

Get all declared fields from Class Note : getDeclaredFields() may return private attributes, but does not return Parents fields

Demo Code


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

import java.util.Arrays;
import java.util.List;

public class Main {
    /**//www.j a v a2s .  c o m
     * Get all declared fields from Class<?>
     * Note : getDeclaredFields() may return private attributes, but does not return Parents fields
     *
     * @param fields
     * @param type
     * @return
     */
    public static List<Field> getAllDeclaredFields(List<Field> fields,
            Class<?> type) {
        fields.addAll(Arrays.asList(type.getDeclaredFields()));

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

        return fields;
    }
}

Related Tutorials