Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * 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.util.Map;
 import java.util.Set;
 
 
Helper class for working with interceptors and interceptor bindings.

Author(s):
Jozef Hartinger
 
 public class Interceptors {
 
     private Interceptors() {
     }

    
Extracts a set of interceptor bindings from a collection of annotations.

Parameters:
beanManager
annotations
Returns:
 
     public static Set<AnnotationfilterInterceptorBindings(BeanManagerImpl beanManagerCollection<Annotationannotations) {
         Set<AnnotationinterceptorBindings = 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.

Parameters:
addTopLevelInterceptorBindings add top level interceptor bindings to the result set.
addInheritedInterceptorBindings add inherited level interceptor bindings to the result set.
Returns:
 
     public static Set<AnnotationflattenInterceptorBindings(BeanManagerImpl beanManagerCollection<Annotationannotationsboolean addTopLevelInterceptorBindings,
             boolean addInheritedInterceptorBindings) {
         Set<AnnotationflattenInterceptorBindings = new InterceptorBindingSet(beanManager);
 
         if (addTopLevelInterceptorBindings) {
             for (Annotation annotation : annotations) {
                 boolean added = flattenInterceptorBindings.add(annotation);
                 if (!added) {
                     throw ..duplicateInterceptorBinding(annotations);
                 }
             }
         }
         if (addInheritedInterceptorBindings) {
             for (Annotation annotation : annotations) {
                 addInheritedInterceptorBindings(annotation.annotationType(), beanManager.getServices().get(MetaAnnotationStore.class), flattenInterceptorBindings);
             }
         }
         return flattenInterceptorBindings;
     }
 
     private static void addInheritedInterceptorBindings(Class<? extends AnnotationbindingTypeMetaAnnotationStore storeSet<AnnotationinheritedBindings) {
         Set<AnnotationmetaBindings = store.getInterceptorBindingModel(bindingType).getInheritedInterceptionBindingTypes();
         inheritedBindings.addAll(metaBindings);
         for (Annotation metaBinding : metaBindings) {
             addInheritedInterceptorBindings(metaBinding.annotationType(), storeinheritedBindings);
         }
     }

    
Merge class-level interceptor bindings with interceptor bindings inherited from interceptor bindings and stereotypes.
 
     public static Map<Class<? extends Annotation>, AnnotationmergeBeanInterceptorBindings(BeanManagerImpl beanManagerAnnotatedType<?> clazzCollection<Class<? extends Annotation>> stereotypes) {
        Set<AnnotationclassBindingAnnotations = flattenInterceptorBindings(beanManagerfilterInterceptorBindings(beanManagerclazz.getAnnotations()), truefalse);
        Set<AnnotationinheritedBindingAnnotations = new HashSet<Annotation>();
        inheritedBindingAnnotations.addAll(flattenInterceptorBindings(beanManagerfilterInterceptorBindings(beanManagerclazz.getAnnotations()), falsetrue));
        for (Class<? extends Annotationannotation : stereotypes) {
            inheritedBindingAnnotations.addAll(flattenInterceptorBindings(beanManagerfilterInterceptorBindings(beanManagerbeanManager.getStereotypeDefinition(annotation)), truetrue));
        }
        try {
            return mergeBeanInterceptorBindings(beanManagerclazzclassBindingAnnotationsinheritedBindingAnnotations);
        } catch (DeploymentException e) {
        }
    }

    
Merge class-level interceptor bindings with interceptor bindings inherited from interceptor bindings and stereotypes.
    public static Map<Class<? extends Annotation>, AnnotationmergeBeanInterceptorBindings(BeanManagerImpl beanManagerAnnotatedType<?> clazzCollection<AnnotationclassBindingAnnotations,
            Collection<AnnotationinheritedBindingAnnotations) {
        Map<Class<? extends Annotation>, AnnotationmergedBeanBindings = new HashMap<Class<? extends Annotation>, Annotation>();
        // conflict detection
        Map<Class<? extends Annotation>, AnnotationacceptedInheritedBindingTypes = new HashMap<Class<? extends Annotation>, Annotation>();
        // add all class-level interceptor bindings (these have precedence)
        for (Annotation bindingAnnotation : classBindingAnnotations) {
            if (mergedBeanBindings.containsKey(bindingAnnotation.annotationType())) {
                // not possible in Java, but we never know what extension-provided AnnotatedType returns
                throw new DeploymentException(..conflictingInterceptorBindings(clazz.getJavaClass()));
            }
            mergedBeanBindings.put(bindingAnnotation.annotationType(), bindingAnnotation);
        }
        // add inherited interceptor bindings
        for (Annotation bindingAnnotation : inheritedBindingAnnotations) {
            Class<? extends AnnotationbindingAnnotationType = bindingAnnotation.annotationType();
            // replace the previous interceptor binding with current binding
            Annotation previousValue = mergedBeanBindings.get(bindingAnnotationType);
            if (previousValue == null) {
                mergedBeanBindings.put(bindingAnnotationTypebindingAnnotation);
                acceptedInheritedBindingTypes.put(bindingAnnotationTypebindingAnnotation);
            } else {
                // check for conflicts
                if (acceptedInheritedBindingTypes.containsKey(bindingAnnotationType)
                        && !beanManager.getServices().get(MetaAnnotationStore.class).getInterceptorBindingModel(bindingAnnotationType)
                                .isEqual(previousValuebindingAnnotationtrue)) {
                    throw new DeploymentException(..conflictingInterceptorBindings(clazz.getJavaClass()));
                }
            }
        }
        return mergedBeanBindings;
    }
New to GrepCode? Check out our FAQ X