集册 Java实例教程 检索带注释的字段

检索带注释的字段

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

496
检索带注释的字段
/*n  o  w  j  a  v  a . c o m 提 供*/

//package com.nowjava;

import java.lang.annotation.Annotation;

import java.lang.reflect.Field;


import java.util.ArrayList;

import java.util.List;


public class Main {

    public static List<Field> retrieveAnnotatedFields(

            final Class<?> objectClass,

            final Class<? extends Annotation> annotationClass) {

        final List<Field> fields = new ArrayList<>();/**时代Java - N o w  J a v a . c o m**/

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

            final Annotation annotation = field

                    .getAnnotation(annotationClass);

            if (annotation != null

                    && annotation.annotationType().equals(annotationClass)) {

                fields.add(field);

            }

        }

        return fields;

    }


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

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

        Class<?> currentClass = clazz;

        while (!Object.
展开阅读全文