Java Reflection Annotation getAnnotations(Set fields, Set methods)

Here you can find the source of getAnnotations(Set fields, Set methods)

Description

get annotations by given fields and methods

License

Apache License

Parameter

Parameter Description
fields a parameter
methods a parameter

Declaration

public static Annotation[] getAnnotations(Set<Field> fields, Set<Method> methods) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

public class Main {
    /**/* www .j a v  a 2 s. c o  m*/
     * get annotations by given fields and methods
     * 
     * @param fields
     * @param methods
     * @return
     */
    public static Annotation[] getAnnotations(Set<Field> fields, Set<Method> methods) {
        List<Annotation> annotations = new LinkedList<Annotation>();
        if (fields != null) {
            for (Field field : fields) {
                Annotation[] annos = field.getAnnotations();
                if (annos != null) {
                    for (Annotation annotation : annos) {
                        annotations.add(annotation);
                    }
                }
            }
        }
        if (methods != null) {
            for (Method method : methods) {
                Annotation[] annos = method.getAnnotations();
                if (annos != null) {
                    for (Annotation annotation : annos) {
                        annotations.add(annotation);
                    }
                }
            }
        }
        return annotations.toArray(new Annotation[annotations.size()]);
    }

    /**
     * get annotations by given fields and methods, and filter by given
     * annotationClass. annotation need to equals or presented by given
     * annotation class.
     * 
     * @param fields
     * @param methods
     * @param annotationClass
     * @return
     */
    public static Annotation[] getAnnotations(Set<Field> fields, Set<Method> methods,
            Class<? extends Annotation> annotationClass) {
        return filterAnnotations(getAnnotations(fields, methods), annotationClass);
    }

    /**
     * filter annotations by given class. annotation need to equals or presented
     * by given annotation class.
     * 
     * @param annotations
     * @param annotationClass
     * @return
     */
    public static Annotation[] filterAnnotations(Annotation[] annotations,
            Class<? extends Annotation> annotationClass) {
        List<Annotation> list = new LinkedList<Annotation>();
        if (annotations != null) {
            for (Annotation annotation : annotations) {
                Class<?> annotationType = annotation.annotationType();
                if (annotationType == annotationClass) {
                    list.add(annotation);
                } else if (annotationType.isAnnotationPresent(annotationClass)) {
                    list.add(annotation);
                }
            }
        }
        return list.toArray(new Annotation[list.size()]);
    }
}

Related

  1. getAnnotations(Method method)
  2. getAnnotations(Method method)
  3. getAnnotations(Method method)
  4. getAnnotations(Method method)
  5. getAnnotations(Object instance)
  6. getAnnotationsOf(Class annoClass)
  7. getAnnotationsRecursive(Class clazz)
  8. getAnnotationsRecursive(final Class type, final Class annotationType)
  9. getAnnotationValue(@Nonnull Class cls, @Nonnull Class annotation, V def)