Recursively searches for enterprise annotations matching the filter. - Java java.lang.annotation

Java examples for java.lang.annotation:Enterprise Annotation

Description

Recursively searches for enterprise annotations matching the filter.

Demo Code

/*// w  w w.jav 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{
    /**
     * 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