Java Reflection Annotation getAnnotationFields(Class clazz, Class annotationClass)

Here you can find the source of getAnnotationFields(Class clazz, Class annotationClass)

Description

Retrieves all Field of given Class and his superclasses annotated with given Annotation class.

License

Open Source License

Parameter

Parameter Description
clazz Class to look for fields
annotationClass annotation class to be found

Return

of annotated fields

Declaration

public static List<Field> getAnnotationFields(Class<?> clazz, Class<?> annotationClass) 

Method Source Code


//package com.java2s;
/*/*from   w w  w .jav  a 2  s .  co m*/
 * #%L
 * MQNaaS :: Core
 * %%
 * Copyright (C) 2007 - 2015 Fundaci? Privada i2CAT, Internet i Innovaci? a Catalunya
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
 * #L%
 */

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;

public class Main {
    /**
     * Retrieves all {@link Field} of given {@link Class} and his superclasses annotated with given {@ink Annotation} class.
     * 
     * @param clazz
     *            Class to look for fields
     * @param annotationClass
     *            annotation class to be found
     * @return {@link List} of annotated fields
     */
    public static List<Field> getAnnotationFields(Class<?> clazz, Class<?> annotationClass) {
        // check attributes
        if (clazz == null || annotationClass == null) {
            throw new NullPointerException("clazz and annotationClass parameters must be not null.");
        } else if (clazz.isInterface()) {
            throw new IllegalArgumentException("clazz parameter can not be an interface, it must be a class.");
        } else if (!annotationClass.isAnnotation()) {
            throw new IllegalArgumentException("annotationClass parameter must be an annotation.");
        }

        List<Field> fields = new ArrayList<Field>();
        return getAnnotationFields(clazz, annotationClass, fields);
    }

    private static List<Field> getAnnotationFields(Class<?> clazz, Class<?> annotationClass,
            List<Field> currentFields) {
        for (Field field : clazz.getDeclaredFields()) {
            for (Annotation anot : field.getAnnotations()) {
                if (anot.annotationType().equals(annotationClass)) {
                    currentFields.add(field);
                }
            }
        }

        Class<?> superClass = clazz.getSuperclass();
        if (superClass != null && !superClass.equals(Object.class)) {
            getAnnotationFields(superClass, annotationClass, currentFields);
        }

        return currentFields;
    }
}

Related

  1. getAnnotationDefault(Class annotationClass, String element)
  2. getAnnotationDefaultMap(Class annotationClass)
  3. getAnnotationElementValue(AnnotatedElement annotatedElement, String annotationClassName, String annotationElementName, Class annotationElementType)
  4. getAnnotationField(Annotation annotation, String field)
  5. getAnnotationFields(Class claz, Class annotationType)
  6. getAnnotationForMethodOrContainingClass(Method m, Class annotationType)
  7. getAnnotationForProperty(Class cls, String name, Class anno)
  8. getAnnotationFromAnnotation(Annotation annotation, Class annotationClass)
  9. getAnnotationFromEntityFields(Class entity, Class annotation)