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

org.jboss.weld.util.Interceptors Maven / Gradle / Ivy

/*
 * JBoss, Home of Professional Open Source
 * Copyright 2010, 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.util;

import java.lang.annotation.Annotation;
import java.lang.annotation.Repeatable;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import jakarta.enterprise.inject.spi.AnnotatedType;
import jakarta.interceptor.InterceptorBinding;

import org.jboss.weld.annotated.enhanced.EnhancedAnnotatedType;
import org.jboss.weld.exceptions.DefinitionException;
import org.jboss.weld.exceptions.DeploymentException;
import org.jboss.weld.logging.BeanLogger;
import org.jboss.weld.logging.BeanManagerLogger;
import org.jboss.weld.manager.BeanManagerImpl;
import org.jboss.weld.metadata.cache.MetaAnnotationStore;
import org.jboss.weld.util.collections.Multimap;
import org.jboss.weld.util.collections.SetMultimap;

/**
 * Helper class for working with interceptors and interceptor bindings.
 *
 * @author Jozef Hartinger
 *
 */
public class Interceptors {

    private Interceptors() {
    }

    /**
     * Extracts a set of interceptor bindings from a collection of annotations.
     *
     * @param beanManager
     * @param annotations
     * @return
     */
    public static Set filterInterceptorBindings(BeanManagerImpl beanManager, Collection annotations) {
        Set interceptorBindings = new InterceptorBindingSet(beanManager);
        for (Annotation annotation : annotations) {
            if (beanManager.isInterceptorBinding(annotation.annotationType())) {
                interceptorBindings.add(annotation);
            }
        }
        return interceptorBindings;
    }

    /**
     * Extracts a flat set of interception bindings from a given set of interceptor bindings.
     *
     * @param addTopLevelInterceptorBindings add top level interceptor bindings to the result set.
     * @param addInheritedInterceptorBindings add inherited level interceptor bindings to the result set.
     * @return
     */
    public static Set flattenInterceptorBindings(EnhancedAnnotatedType clazz, BeanManagerImpl beanManager,
            Collection annotations, boolean addTopLevelInterceptorBindings,
            boolean addInheritedInterceptorBindings) {
        Set flattenInterceptorBindings = new InterceptorBindingSet(beanManager);
        MetaAnnotationStore metaAnnotationStore = beanManager.getServices().get(MetaAnnotationStore.class);

        if (addTopLevelInterceptorBindings) {
            addInterceptorBindings(clazz, annotations, flattenInterceptorBindings, metaAnnotationStore);
        }
        if (addInheritedInterceptorBindings) {
            for (Annotation annotation : annotations) {
                addInheritedInterceptorBindings(clazz, annotation.annotationType(), metaAnnotationStore,
                        flattenInterceptorBindings);
            }
        }
        return flattenInterceptorBindings;
    }

    private static void addInheritedInterceptorBindings(EnhancedAnnotatedType clazz, Class bindingType,
            MetaAnnotationStore metaAnnotationStore, Set flattenInterceptorBindings) {
        Set metaBindings = metaAnnotationStore.getInterceptorBindingModel(bindingType)
                .getInheritedInterceptionBindingTypes();
        addInterceptorBindings(clazz, metaBindings, flattenInterceptorBindings, metaAnnotationStore);
        for (Annotation metaBinding : metaBindings) {
            addInheritedInterceptorBindings(clazz, metaBinding.annotationType(), metaAnnotationStore,
                    flattenInterceptorBindings);
        }
    }

    private static void addInterceptorBindings(EnhancedAnnotatedType clazz, Collection interceptorBindings,
            Set flattenInterceptorBindings,
            MetaAnnotationStore metaAnnotationStore) {
        for (Annotation annotation : interceptorBindings) {
            Class annotationType = annotation.annotationType();
            if (!annotation.annotationType().isAnnotationPresent(Repeatable.class)) {
                for (Annotation binding : flattenInterceptorBindings) {
                    if (binding.annotationType().equals(annotationType)
                            && !metaAnnotationStore.getInterceptorBindingModel(annotationType).isEqual(annotation, binding,
                                    false)) {
                        if (clazz != null) {
                            throw new DefinitionException(BeanLogger.LOG.conflictingInterceptorBindings(clazz));
                        } else {
                            throw BeanManagerLogger.LOG.duplicateInterceptorBinding(annotation);
                        }

                    }
                }
            }
            flattenInterceptorBindings.add(annotation);
        }
    }

    /**
     * Merge class-level interceptor bindings with interceptor bindings inherited from interceptor bindings and stereotypes.
     */
    public static Multimap, Annotation> mergeBeanInterceptorBindings(BeanManagerImpl beanManager,
            EnhancedAnnotatedType clazz, Collection> stereotypes) {
        Set rawBindings = clazz.getMetaAnnotations(InterceptorBinding.class);
        Set classBindingAnnotations = flattenInterceptorBindings(clazz, beanManager,
                filterInterceptorBindings(beanManager, rawBindings), true, false);
        Set inheritedBindingAnnotations = new HashSet();
        inheritedBindingAnnotations.addAll(flattenInterceptorBindings(clazz, beanManager,
                filterInterceptorBindings(beanManager, rawBindings), false, true));
        for (Class annotation : stereotypes) {
            inheritedBindingAnnotations.addAll(flattenInterceptorBindings(clazz, beanManager,
                    filterInterceptorBindings(beanManager, beanManager.getStereotypeDefinition(annotation)), true, true));
        }
        try {
            return mergeBeanInterceptorBindings(beanManager, clazz, classBindingAnnotations, inheritedBindingAnnotations);
        } catch (DeploymentException e) {
            throw new DefinitionException(BeanLogger.LOG.conflictingInterceptorBindings(clazz.getJavaClass()));
        }
    }

    /**
     * Merge class-level interceptor bindings with interceptor bindings inherited from interceptor bindings and stereotypes.
     */
    public static Multimap, Annotation> mergeBeanInterceptorBindings(BeanManagerImpl beanManager,
            AnnotatedType clazz,
            Collection classBindingAnnotations, Collection inheritedBindingAnnotations) {

        SetMultimap, Annotation> mergedBeanBindings = SetMultimap.newSetMultimap();
        SetMultimap, Annotation> acceptedInheritedBindings = SetMultimap.newSetMultimap();
        MetaAnnotationStore metaAnnotationStore = beanManager.getServices().get(MetaAnnotationStore.class);

        // add all class-level interceptor bindings (these have precedence)
        for (Annotation binding : classBindingAnnotations) {
            Class annotationType = binding.annotationType();
            if (!annotationType.isAnnotationPresent(Repeatable.class)) {
                // Detec conflicts for non repeating bindings
                for (Annotation mergedBinding : mergedBeanBindings.get(annotationType)) {
                    if (!metaAnnotationStore.getInterceptorBindingModel(annotationType).isEqual(binding, mergedBinding,
                            false)) {
                        throw new DeploymentException(BeanLogger.LOG.conflictingInterceptorBindings(clazz.getJavaClass()));
                    }
                }
            }
            mergedBeanBindings.put(binding.annotationType(), binding);
        }
        // add inherited interceptor bindings
        for (Annotation binding : inheritedBindingAnnotations) {
            Class annotationType = binding.annotationType();

            if (!mergedBeanBindings.containsKey(annotationType) || annotationType.isAnnotationPresent(Repeatable.class)) {
                mergedBeanBindings.put(annotationType, binding);
                acceptedInheritedBindings.put(annotationType, binding);
            } else {
                Set inheritedBindings = acceptedInheritedBindings.get(annotationType);
                for (Annotation inheritedBinding : inheritedBindings) {
                    if (!metaAnnotationStore.getInterceptorBindingModel(annotationType).isEqual(binding, inheritedBinding,
                            false)) {
                        throw new DeploymentException(BeanLogger.LOG.conflictingInterceptorBindings(clazz.getJavaClass()));
                    }
                }
            }
        }
        return mergedBeanBindings;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy