Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source
   * Copyright 2013, 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;
 
 import java.util.Map;
 import java.util.Set;
 
 
 
ProcessAnnotatedType observer method resolver. It uses org.jboss.weld.resources.spi.ClassFileServices for resolution and thus entirely avoids loading the classes which speeds up especially large deployments. Although this resolver covers most of the possible PAT observer method types, there are several cases when org.jboss.weld.resources.spi.ClassFileInfo used by this resolver is not sufficient to perform observer method resolution correctly. If such observer method is present in the deployment, the constructor of this class throws UnsupportedObserverMethodException. This exception is expected to be caught by the deployer and observer method resolution using the default org.jboss.weld.resolution.TypeSafeObserverResolver is performed instead.

Author(s):
Jozef Hartinger
 
 
     private static class ExactTypePredicate implements Predicate<ClassFileInfo> {
         private final Class<?> type;
 
         public ExactTypePredicate(Class<?> type) {
             this. = type;
         }
 
         @Override
         public boolean apply(ClassFileInfo input) {
             return .getName().equals(input.getClassName());
         }
     }
 
     private static class AssignableToPredicate implements Predicate<ClassFileInfo> {
 
         private final Class<?> type;
 
         public AssignableToPredicate(Class<?> type) {
             this. = type;
         }
 
         @Override
         public boolean apply(ClassFileInfo input) {
             return input.isAssignableTo();
         }
     }
 
     @SuppressWarnings("unchecked")
     private static class CompositePredicate implements Predicate<ClassFileInfo> {
 
         private static CompositePredicate assignable(Class<?>[] classes) {
             Predicate<ClassFileInfo>[] predicates = new Predicate[classes.length];
             for (int i = 0; i < classes.lengthi++) {
                 predicates[i] = new AssignableToPredicate(classes[i]);
             }
             return new CompositePredicate(predicates);
         }
 
         private final Predicate<ClassFileInfo>[] predicates;
 
         public CompositePredicate(Predicate<ClassFileInfo>[] predicates) {
             this. = predicates;
        }
        @Override
        public boolean apply(ClassFileInfo input) {
            for (Predicate<ClassFileInfopredicate : ) {
                if (!predicate.apply(input)) {
                    return false;
                }
            }
            return true;
        }
    }
    private final Set<ExtensionObserverMethodImpl<?, ?>> catchAllObservers;
        this. = Sets.newHashSet();
        for (ObserverMethod<?> o : observers) {
            if (o instanceof ExtensionObserverMethodImpl<?, ?>) {
                final Set<Annotationqualifiers = o.getObservedQualifiers();
                // only process observer methods with no qualifiers or with @Any
                if (qualifiers.isEmpty() || (qualifiers.size() == 1 && Any.class.equals(qualifiers.iterator().next().annotationType()))) {
                    process((ExtensionObserverMethodImpl<?, ?>) oo.getObservedType());
                }
            }
        }
    }
    private void process(ExtensionObserverMethodImpl<?, ?> observerType observedTypethrows UnsupportedObserverMethodException {
        if (Object.class.equals(observedType)) {
            // void observe(Object event)
            .add(observer);
        } else if (ProcessAnnotatedType.class.equals(observedType)) {
            // void observe(ProcessAnnotatedType event)
            .add(observer);
        } else if (observedType instanceof ParameterizedType) {
            ParameterizedType type = (ParameterizedTypeobservedType;
            if (ProcessAnnotatedType.class.equals(type.getRawType())) {
                Type typeParameter = type.getActualTypeArguments()[0];
                if (typeParameter instanceof Class<?>) {
                    this..put(observernew ExactTypePredicate(Reflections.getRawType(typeParameter)));
                } else if (typeParameter instanceof ParameterizedType) {
                    // void observe(ProcessAnnotatedType<Iterable<?>> event)
                    ParameterizedType parameterizedType = (ParameterizedTypetypeParameter;
                    for (Type t : parameterizedType.getActualTypeArguments()) {
                        if (!Reflections.isUnboundedTypeVariable(t) && !Reflections.isUnboundedWildcard(t)) {
                            return// skip - this observer would never be invoked per spec
                        }
                        this..put(observernew ExactTypePredicate(Reflections.getRawType(typeParameter)));
                    }
                } else if (typeParameter instanceof WildcardType) {
                    // void observe(ProcessAnnotatedType<?> event)
                    WildcardType wildCard = (WildcardTypetypeParameter;
                    checkBounds(observerwildCard.getUpperBounds());
                    this..put(observer, CompositePredicate.assignable(Types.getRawTypes(wildCard.getUpperBounds())));
                } else if (typeParameter instanceof TypeVariable<?>) {
                    // <T> void observe(ProcessAnnotatedType<T> event)
                    TypeVariable<?> variable = (TypeVariable<?>) typeParameter;
                    checkBounds(observervariable.getBounds());
                    this..put(observer, CompositePredicate.assignable(Types.getRawTypes(variable.getBounds())));
                }
            }
        } else if (observedType instanceof TypeVariable<?>) {
            defaultRules(observerobservedType);
        }
    }
    private void checkBounds(ExtensionObserverMethodImpl<?, ?> observerType[] boundsthrows UnsupportedObserverMethodException {
        for (Type type : bounds) {
            if (!(type instanceof Class<?>)) {
                throw new UnsupportedObserverMethodException(observer);
            }
        }
    }
    private void defaultRules(ExtensionObserverMethodImpl<?, ?> observerType observedTypethrows UnsupportedObserverMethodException {
        if (ProcessAnnotatedType.class.equals(observedType)) {
            .add(observer);
        } else if (observedType instanceof ParameterizedType) {
            ParameterizedType parameterizedType = (ParameterizedTypeobservedType;
            if (ProcessAnnotatedType.class.equals(parameterizedType.getRawType())) {
                Type argument = parameterizedType.getActualTypeArguments()[0];
                if (argument instanceof Class<?>) {
                    this..put(observernew AssignableToPredicate(Reflections.getRawType(argument)));
                } else {
                    throw new UnsupportedObserverMethodException(observer);
                }
            }
        } else if (observedType instanceof TypeVariable) {
            final TypeVariable<?> typeVariable = (TypeVariable<?>) observedType;
            if (Reflections.isUnboundedTypeVariable(observedType)) {
                // <T> void observe(T event)
                .add(observer);
            } else {
                if (typeVariable.getBounds().length == 1) { // here we expect that a PAT impl only implements the PAT interface
                    defaultRules(observertypeVariable.getBounds()[0]);
                }
            }
        }
    }

    
Resolves a set of ProcessAnnotatedType observer methods for the specified class. If no observer methods are resolved, an empty set is returned.

Parameters:
className the specified class name
Returns:
the set of resolved ProcessAnnotatedType observer methods
    public Set<ExtensionObserverMethodImpl<?, ?>> resolveProcessAnnotatedTypeObservers(ClassFileServices classFileServicesString className) {
        Set<ExtensionObserverMethodImpl<?, ?>> result = new HashSet<ExtensionObserverMethodImpl<?, ?>>();
        result.addAll();
        ClassFileInfo classInfo = classFileServices.getClassFileInfo(className);
        for (Map.Entry<ExtensionObserverMethodImpl<?, ?>, Predicate<ClassFileInfo>> entry : .entrySet()) {
            ExtensionObserverMethodImpl<?, ?> observer = entry.getKey();
            if (containsRequiredAnnotation(classInfoobserver) && entry.getValue().apply(classInfo)) {
                result.add(observer);
            }
        }
        return result;
    }
    private boolean containsRequiredAnnotation(ClassFileInfo classInfoExtensionObserverMethodImpl<?, ?> observer) {
        if (observer.getRequiredAnnotations().isEmpty()) {
            return true;
        }
        for (Class<? extends Annotationannotation : observer.getRequiredAnnotations()) {
            if (classInfo.containsAnnotation(annotation)) {
                return true;
            }
        }
        return false;
    }
    @Override
    public void cleanupAfterBoot() {
        .clear();
        .clear();
    }
New to GrepCode? Check out our FAQ X