get Fields Annotated - Java java.lang.annotation

Java examples for java.lang.annotation:Field Annotation

Description

get Fields Annotated

Demo Code


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

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Main {


    public static List<Field> getFieldsAnnotated(Class<?> clazz,
            Class<? extends Annotation> annotationClass) {
        Field[] fields = clazz.getDeclaredFields();
        if (fields.length == 0)
            return Collections.emptyList();

        List<Field> result = new ArrayList<>();
        ;/*  ww  w.  j a va2  s .c om*/
        for (Field f : fields) {
            if (f.isAnnotationPresent(annotationClass)) {
                result.add(f);
            }
        }
        return result;
    }
}

Related Tutorials