Searches for the given enterprise annotation and returns if found. - Java java.lang.annotation

Java examples for java.lang.annotation:Enterprise Annotation

Description

Searches for the given enterprise annotation and returns if found.

Demo Code

/*/* w  w  w  . ja  va 2s . c om*/
 * ---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 for the given annotation and returns if found.
     *
     * @param annotatedType annotated type instance
     * @param beanManager the bean manager
     * @param annotationType class of the annotation to query for
     * @return true/false
     */
    public static boolean isAnnotationPresent(
            final AnnotatedType<?> annotatedType,
            final BeanManager beanManager,
            final Class<? extends Annotation> annotationType) {
        return getAnnotation(annotatedType, beanManager, annotationType) != null;
    }
    /**
     * Returns a given annotation on an annotated type instance. If the annotation is found
     * more than once (e.g. because it's also contained in a stereotype) the returned annoation is
     * not deterministic. So make sure to prevent such a situation.
     *
     * @param <T> type of the annotated type
     * @param annotatedType annotated type instance
     * @param beanManager the bean manager
     * @param annotationType class of the annotation to query for
     * @return annotation or null if not found
     */
    @SuppressWarnings("unchecked")
    public static <T extends Annotation> T getAnnotation(
            final AnnotatedType<?> annotatedType,
            final BeanManager beanManager, final Class<T> annotationType) {
        Set<Annotation> annotations = findAnnotations(annotatedType,
                beanManager, new AnnotationFilter() {
                    @Override
                    public boolean matches(final Annotation annotation) {
                        return annotation.annotationType().equals(
                                annotationType);
                    }
                }, true);

        return (T) (annotations.isEmpty() ? null : annotations.toArray()[0]);
    }
    /**
     * 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