filter annotations by given class. - Java java.lang.annotation

Java examples for java.lang.annotation:Class Annotation

Description

filter annotations by given class.

Demo Code


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

import java.util.LinkedList;
import java.util.List;

public class Main {
    /**/*from   ww  w.  j a v a2 s .  c o m*/
     * 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 Tutorials