集册 Java实例教程 检索常量字段

检索常量字段

欢马劈雪     最近更新时间:2020-01-02 10:19:05

459
检索常量字段
/**N  o w  J a v a . c o m**/


//package com.nowjava;


import java.lang.reflect.Field;

import java.lang.reflect.Modifier;


import java.util.ArrayList;

import java.util.List;


public class Main {

    public static List<Field> retrieveConstantFields(final Class<?> clazz) {

        final List<Field> fields = new ArrayList<Field>();

        for (final Field field : retrieveAllFields(clazz)) {

            if (!isFieldToBeProvided(field)) {

                fields.add(field);

            }/*N o  w  J a v a . c o m - 时  代  Java 提供*/

        }

        return fields;

    }


    public static List<Field> retrieveAllFields(final Class<?> clazz) {

        final List<Field> fields = new ArrayList<Field>();

        Class<?> currentClass = clazz;

        while (!Object.class.equals(currentClass)) {

            for (final Field field : currentClass.getDeclaredFields()) {

                fields.add(field);

            }

            currentClass = currentClass.getSuperclass();

        }

        return fields;

    }


    private static 
展开阅读全文