Searches the given enterprise annotated type for matching annotations according to the given filter implementation. - Java java.lang.annotation

Java examples for java.lang.annotation:Enterprise Annotation

Description

Searches the given enterprise annotated type for matching annotations according to the given filter implementation.

Demo Code

/*/* w  ww. j av a 2  s  .  c o  m*/
 * ---LICENSE_BEGIN---
 * cdi-ext - Some extensions for CDI
 * ---
 * Copyright (C) 2013 Roland Bachlechner
 * ---
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ---LICENSE_END---
 */
import java.lang.annotation.Annotation;
import java.util.HashSet;
import java.util.Set;
import javax.enterprise.context.Dependent;
import javax.enterprise.inject.spi.AnnotatedType;
import javax.enterprise.inject.spi.BeanManager;
import javax.enterprise.util.AnnotationLiteral;

public class Main{
    /**
     * Searches the given annotated type for matching annotations according to the given filter implementation.
     *
     * @param annotatedType annotated type to search for matching annotations
     * @param beanManager bean manager
     * @param filter filter implementation to search for annotations
     * @param unique there should only be returned one annotation
     * @return list of matching annotations
     */
    private static Set<Annotation> findAnnotations(
            final AnnotatedType<?> annotatedType,
            final BeanManager beanManager, final AnnotationFilter filter,
            final Boolean unique) {
        Set<Annotation> matchingAnnotations = new HashSet<>();

        Set<Annotation> annotations = annotatedType.getAnnotations();
        for (Annotation annotation : annotations) {
            matchingAnnotations.addAll(findMatchingAnnotations(annotation,
                    beanManager, filter, unique));

            if (unique && !matchingAnnotations.isEmpty()) {
                break;
            }
        }

        return matchingAnnotations;
    }
    /**
     * Recursively searches for annotations matching the filter. Recursion is necessary because of
     * stereotypes.
     *
     * @param annotation annotation to check for stereotype definition
     * @param beanManager bean manager
     * @param filter filter implementation to search for annotations
     * @return list of matching annotations
     */
    private static Set<Annotation> findMatchingAnnotations(
            final Annotation annotation, final BeanManager beanManager,
            final AnnotationFilter filter, final Boolean unique) {
        Set<Annotation> matchingAnnotations = new HashSet<>();

        Class<? extends Annotation> annotationType = annotation
                .annotationType();

        if (filter.matches(annotation)) {
            matchingAnnotations.add(annotation);
        }

        if ((!unique || matchingAnnotations.isEmpty())
                && beanManager.isStereotype(annotationType)) {
            for (Annotation stereotype : beanManager
                    .getStereotypeDefinition(annotationType)) {
                matchingAnnotations.addAll(findMatchingAnnotations(
                        stereotype, beanManager, filter, unique));

                if (unique && !matchingAnnotations.isEmpty()) {
                    break;
                }
            }
        }

        return matchingAnnotations;
    }
}

Related Tutorials