get fields list of a type, and all the fields returned contains the given annotation - Java java.lang.annotation

Java examples for java.lang.annotation:Field Annotation

Description

get fields list of a type, and all the fields returned contains the given annotation

Demo Code


//package com.java2s;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class Main {


    /**/*  ww w. j  a v a 2  s . c  o m*/
     * get fields list of a type, and all the fields returned contains the given
     * annotation
     * 
     * @param claz
     * @param annotationType
     * @return
     */
    @SuppressWarnings({ "unchecked", "rawtypes" })
    public static List<Field> getAnnotationFields(Class claz,
            Class annotationType) {
        List<Field> result = new ArrayList<Field>();
        if (claz != null) {
            Field[] fields = claz.getDeclaredFields();
            for (Field field : fields) {
                if (field.getAnnotation(annotationType) != null) {
                    result.add(field);
                }
            }
            result.addAll(getAnnotationFields(claz.getSuperclass(),
                    annotationType));
        }
        return result;
    }
}

Related Tutorials