All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jboss.weld.bootstrap.events.RequiredAnnotationDiscovery Maven / Gradle / Ivy

There is a newer version: 3.0.0.Alpha1
Show newest version
/*
 * 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;

import java.lang.annotation.Annotation;
import java.lang.annotation.Inherited;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

import org.jboss.weld.bootstrap.api.Service;
import org.jboss.weld.resources.ReflectionCache;

/**
 * Wrapper over {@link 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 Jozef Hartinger
 *
 */
public class RequiredAnnotationDiscovery implements Service {

    private final ReflectionCache cache;

    public RequiredAnnotationDiscovery(ReflectionCache cache) {
        this.cache = 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 {@link 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
  • *
* * @param javaClass the given class * @param annotation the given annotation type * @return */ public boolean containsAnnotation(Class javaClass, Class requiredAnnotation) { for (Class clazz = javaClass; clazz != null && clazz != Object.class; clazz = clazz.getSuperclass()) { // class level annotations if (clazz == javaClass || requiredAnnotation.isAnnotationPresent(Inherited.class)) { if (containsAnnotations(cache.getAnnotations(clazz), requiredAnnotation)) { return true; } } // fields for (Field field : clazz.getDeclaredFields()) { if (containsAnnotations(cache.getAnnotations(field), requiredAnnotation)) { return true; } } // constructors for (Constructor constructor : clazz.getConstructors()) { if (containsAnnotations(cache.getAnnotations(constructor), requiredAnnotation)) { return true; } for (Annotation[] parameterAnnotations : constructor.getParameterAnnotations()) { if (containsAnnotations(parameterAnnotations, requiredAnnotation)) { return true; } } } // methods for (Method method : clazz.getDeclaredMethods()) { if (containsAnnotations(cache.getAnnotations(method), requiredAnnotation)) { return true; } for (Annotation[] parameterAnnotations : method.getParameterAnnotations()) { if (containsAnnotations(parameterAnnotations, requiredAnnotation)) { return true; } } } } return false; } private boolean containsAnnotations(Annotation[] annotations, Class requiredAnnotation) { return containsAnnotation(annotations, requiredAnnotation, true); } private boolean containsAnnotation(Annotation[] annotations, Class requiredAnnotation, boolean checkMetaAnnotations) { for (Annotation annotation : annotations) { Class annotationType = annotation.annotationType(); if (requiredAnnotation.equals(annotationType)) { return true; } if (checkMetaAnnotations && containsAnnotation(cache.getAnnotations(annotationType), requiredAnnotation, false)) { return true; } } return false; } @Override public void cleanup() { } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy