Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source
   * Copyright 2012, Red Hat, Inc., and individual contributors
   * by the @authors tag. See the copyright.txt in the distribution for a
   * full listing of individual contributors.
   *
   * 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.
  */
 package org.jboss.weld.bootstrap.events;
 
 
Wrapper over org.jboss.weld.resources.ReflectionCache capable of determining whether a given class has a given annotation or not, as defined by the CDI 1.1 specification (11.5.6).

Author(s):
Jozef Hartinger
 
 public class RequiredAnnotationDiscovery implements Service {
 
     private final ReflectionCache cache;
 
     public RequiredAnnotationDiscovery(ReflectionCache cache) {
         this. = cache;
     }

    

Indicates whether the given class contains an annotation of the given annotation type.

The set is referred to as M hereafter

The given class is said to contain the given annotation if any of these applies:

  • The required annotation or an annotation annotated with the required annotation is present on the class
  • The required annotation or an annotation annotated with the required annotation, which is annotated with java.lang.annotation.Inherited, is present on a direct or indirect superclass of the given class
  • The required annotation or an annotation annotated with the required annotation is present on a field or method declared by the given class or any direct or indirect superclass of the given class
  • The required annotation or an annotation annotated with the required annotation is present on a parameter of a method declared by the given class or any direct or indirect superclass of the given class
  • The required annotation or an annotation annotated with the required annotation is present on a constructor declared by the given class
  • The required annotation or an annotation annotated with the required annotation is present on a parameter of a constructor declared by the given class

Parameters:
javaClass the given class
annotation the given annotation type
Returns:
 
     public boolean containsAnnotation(BackedAnnotatedType<?> annotatedTypeClass<? extends AnnotationrequiredAnnotation) {
         // class level annotations
         if (containsAnnotations(annotatedType.getAnnotations(), requiredAnnotationtrue)) {
             return true;
         }
         for (Class<?> clazz = annotatedType.getJavaClass(); clazz != null && clazz != Object.classclazz = clazz.getSuperclass()) {
             // fields
             for (Field field : clazz.getDeclaredFields()) {
                 if (containsAnnotations(.getAnnotations(field), requiredAnnotation)) {
                     return true;
                 }
             }
             // constructors
             for (Constructor<?> constructor : clazz.getDeclaredConstructors()) {
                 if (containsAnnotations(.getAnnotations(constructor), requiredAnnotation)) {
                     return true;
                 }
                 for (Annotation[] parameterAnnotations : constructor.getParameterAnnotations()) {
                     if (containsAnnotations(parameterAnnotationsrequiredAnnotation)) {
                         return true;
                     }
                 }
             }
             // methods
             for (Method method : clazz.getDeclaredMethods()) {
                 if (containsAnnotations(.getAnnotations(method), requiredAnnotation)) {
                    return true;
                }
                for (Annotation[] parameterAnnotations : method.getParameterAnnotations()) {
                    if (containsAnnotations(parameterAnnotationsrequiredAnnotation)) {
                        return true;
                    }
                }
            }
        }
        return false;
    }
    private boolean containsAnnotations(Annotation[] annotationsClass<? extends AnnotationrequiredAnnotation) {
        return containsAnnotation(annotationsrequiredAnnotationtrue);
    }
    private boolean containsAnnotation(Annotation[] annotationsClass<? extends AnnotationrequiredAnnotationboolean checkMetaAnnotations) {
        for (Annotation annotation : annotations) {
            if (containsAnnotation(annotationrequiredAnnotationcheckMetaAnnotations)) {
                return true;
            }
        }
        return false;
    }
    private boolean containsAnnotations(Iterable<? extends AnnotationannotationsClass<? extends AnnotationrequiredAnnotationboolean checkMetaAnnotations) {
        for (Annotation annotation : annotations) {
            if (containsAnnotation(annotationrequiredAnnotationcheckMetaAnnotations)) {
                return true;
            }
        }
        return false;
    }
    private boolean containsAnnotation(Annotation annotationClass<? extends AnnotationrequiredAnnotationboolean checkMetaAnnotations) {
        Class<? extends AnnotationannotationType = annotation.annotationType();
        if (requiredAnnotation.equals(annotationType)) {
            return true;
        }
        if (checkMetaAnnotations && containsAnnotation(.getAnnotations(annotationType), requiredAnnotationfalse)) {
            return true;
        }
        return false;
    }
    @Override
    public void cleanup() {
    }
New to GrepCode? Check out our FAQ X