Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source
   * Copyright 2008, 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 static org.jboss.weld.util.Types.buildClassNameMap;
 
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
 
Checks a list of beans for DeploymentExceptions and their subclasses

Author(s):
Nicklas Karlsson
David Allen
Jozef Hartinger
Stuart Douglas
Ales Justin
public class Validator implements Service {
    protected void validateGeneralBean(Bean<?> beanBeanManagerImpl beanManager) {
        for (InjectionPoint ij : bean.getInjectionPoints()) {
            validateInjectionPoint(ijbeanManager);
        }
        // Validate all pseudo-scoped beans, except for built-in beans and session beans which are proxied by the EJB container
        if (!beanManager.isNormalScope(bean.getScope()) && !(bean instanceof AbstractBuiltInBean) && !(bean instanceof SessionBean)) {
            validatePseudoScopedBean(beanbeanManager);
        }
        if (beanManager.isPassivatingScope(bean.getScope()) && !Beans.isPassivationCapableBean(bean)) {
        }
    }

    
Validate an RIBean. This includes validating whether two beans specialize the same bean

Parameters:
bean the bean to validate
beanManager the current manager
specializedBeans the existing specialized beans
    protected void validateRIBean(CommonBean<?> beanBeanManagerImpl beanManagerCollection<CommonBean<?>> specializedBeans) {
        validateGeneralBean(beanbeanManager);
        if (bean instanceof NewBean) {
            return;
        }
        if (bean instanceof DecorableBean) {
            validateDecorators(beanManager, (DecorableBean<?>) bean);
        }
        if ((bean instanceof AbstractClassBean<?>)) {
            AbstractClassBean<?> classBean = (AbstractClassBean<?>) bean;
            // validate CDI-defined interceptors
            if (classBean.hasInterceptors()) {
                validateInterceptors(beanManagerclassBean);
            }
        }
        // for each producer bean validate its disposer method
        if (bean instanceof AbstractProducerBean<?, ?, ?>) {
            AbstractProducerBean<?, ?, ?> producerBean = Reflections.<AbstractProducerBean<?, ?, ?>> cast(bean);
            if (producerBean.getProducer() instanceof AbstractMemberProducer<?, ?>) {
                AbstractMemberProducer<?, ?> producer = Reflections.<AbstractMemberProducer<?, ?>> cast(producerBean
                        .getProducer());
                if (producer.getDisposalMethod() != null) {
                    for (InjectionPoint ip : producer.getDisposalMethod().getInjectionPoints()) {
                        // pass the producer bean instead of the disposal method bean
                        validateInjectionPointForDefinitionErrors(ipnullbeanManager);
                        validateMetadataInjectionPoint(ipnull.);
                        validateEventMetadataInjectionPoint(ip);
                        validateInjectionPointForDeploymentProblems(ipnullbeanManager);
                    }
                }
            }
        }
    }
    private void validateCustomBean(Bean<?> beanBeanManagerImpl beanManager) {
        validateGeneralBean(beanbeanManager);
        if (!(bean instanceof PassivationCapable) && beanManager.isNormalScope(bean.getScope())) {
            ..beanNotPassivationCapable(bean);
        }
    }
    private void validateInterceptors(BeanManagerImpl beanManagerAbstractClassBean<?> classBean) {
        InterceptionModel interceptionModel = beanManager.getInterceptorModelRegistry().get(classBean.getAnnotated());
        if (interceptionModel != null) {
            Set<? extends InterceptorClassMetadata<?>> interceptors = interceptionModel.getAllInterceptors();
            if (interceptors.size() > 0) {
                boolean passivationCapabilityCheckRequired = beanManager.isPassivatingScope(classBean.getScope());
                for (InterceptorClassMetadata<?> interceptorMetadata : interceptors) {
                    // in the case of CDI interceptors we only need to additionally validate passivation capability (if required)
                    if (interceptorMetadata.getInterceptorFactory() instanceof CdiInterceptorFactory<?> && passivationCapabilityCheckRequired) {
                        CdiInterceptorFactory<?> cdiInterceptorFactory = (CdiInterceptorFactory<?>) interceptorMetadata.getInterceptorFactory();
                        Interceptor<?> interceptor = cdiInterceptorFactory.getInterceptor();
                        boolean isSerializable = (interceptor instanceof InterceptorImpl) ? ((InterceptorImpl<?>) interceptor).isSerializable() : Beans.isPassivationCapableDependency(interceptor);
                        if (!isSerializable) {
                            throw ..passivatingBeanWithNonserializableInterceptor(classBeaninterceptor);
                        }
                        if (interceptor instanceof InterceptorImpl) {
                            beanManager = ((InterceptorImpl<?>) interceptor).getBeanManager();
                        }
                        for (InjectionPoint injectionPoint : interceptor.getInjectionPoints()) {
                            Bean<?> resolvedBean = beanManager.resolve(beanManager.getBeans(injectionPoint));
                            validateInterceptorDecoratorInjectionPointPassivationCapable(injectionPointresolvedBeanbeanManagerclassBean);
                        }
                    }
                    if (interceptorMetadata.getInterceptorFactory() instanceof PlainInterceptorFactory<?>) {
                        PlainInterceptorFactory<?> factory = (PlainInterceptorFactory<?>) interceptorMetadata.getInterceptorFactory();
                        Class<?> interceptorClass = interceptorMetadata.getJavaClass();
                        if (passivationCapabilityCheckRequired && !Reflections.isSerializable(interceptorClass)) {
                            throw ..passivatingBeanWithNonserializableInterceptor(thisinterceptorClass.getName());
                        }
                        // if we can't get to the interceptor's BeanManager, we will use the bean's BM instead
                        InjectionTarget<?> injectionTarget = factory.getInjectionTarget();
                        if (injectionTarget instanceof BasicInjectionTarget<?>) {
                            beanManager = ((BasicInjectionTarget<?>) injectionTarget).getBeanManager();
                        }
                        for (InjectionPoint injectionPoint : factory.getInjectionTarget().getInjectionPoints()) {
                            validateInjectionPoint(injectionPointbeanManager);
                            if (passivationCapabilityCheckRequired) {
                                Bean<?> resolvedBean = beanManager.resolve(beanManager.getBeans(injectionPoint));
                                validateInterceptorDecoratorInjectionPointPassivationCapable(injectionPointresolvedBeanbeanManagerclassBean);
                            }
                        }
                    }
                }
            }
        }
    }
    private void validateDecorators(BeanManagerImpl beanManagerDecorableBean<?> bean) {
        if (!(beanManager.isPassivatingScope(bean.getScope()) || bean instanceof AbstractDecorableBuiltInBean<?>)) {
            return;
        }
        List<Decorator<?>> decorators = bean.getDecorators();
        if (decorators.isEmpty()) {
            return;
        }
        for (Decorator<?> decorator : decorators) {
            if (!Decorators.isPassivationCapable(decorator)) {
                if (bean instanceof AbstractDecorableBuiltInBean<?>) {
                    throw ..builtinBeanWithNonserializableDecorator(decoratorbean);
                } else {
                    throw ..passivatingBeanWithNonserializableDecorator(beandecorator);
                }
            }
            if (decorator instanceof DecoratorImpl) {
                beanManager = ((DecoratorImpl<?>) decorator).getBeanManager();
            }
            for (InjectionPoint ij : decorator.getInjectionPoints()) {
                if (!ij.isDelegate()) {
                    Bean<?> resolvedBean = beanManager.resolve(beanManager.getBeans(ij));
                    validateInterceptorDecoratorInjectionPointPassivationCapable(ijresolvedBeanbeanManagerbean);
                }
            }
        }
    }

    
Validate an injection point

Parameters:
ij the injection point to validate
beanManager the bean manager
    public void validateInjectionPoint(InjectionPoint ijBeanManagerImpl beanManager) {
        validateInjectionPointForDefinitionErrors(ijij.getBean(), beanManager);
        validateInjectionPointForDeploymentProblems(ijij.getBean(), beanManager);
    }

    
Checks for definition errors associated with a given javax.enterprise.inject.spi.InjectionPoint
    public void validateInjectionPointForDefinitionErrors(InjectionPoint ijBean<?> beanBeanManagerImpl beanManager) {
        if (ij.getAnnotated().getAnnotation(New.class) != null && ij.getQualifiers().size() > 1) {
            throw ..newWithQualifiers(ij);
        }
        if (ij.getType() instanceof TypeVariable<?>) {
            throw ..injectionPointWithTypeVariable(ij);
        }
        if (!(ij.getMember() instanceof Field) && ij.getAnnotated().isAnnotationPresent(Named.class) && ij.getAnnotated().getAnnotation(Named.class).value().equals("")) {
            throw ..nonFieldInjectionPointCannotUseNamed(ij);
        }
        if (ij.getAnnotated().isAnnotationPresent(Produces.class)) {
            if (bean != null) {
                throw ..injectedFieldCannotBeProducer(ij.getAnnotated(), bean);
            } else {
                throw ..injectedFieldCannotBeProducer(ij.getAnnotated(), Reflections.<AnnotatedField<?>>cast(ij.getAnnotated()).getDeclaringType());
            }
        }
        boolean newBean = (bean instanceof NewManagedBean<?>) || (bean instanceof NewSessionBean<?>);
        if (!newBean) {
            checkScopeAnnotations(ijbeanManager.getServices().get(MetaAnnotationStore.class));
        }
        checkFacadeInjectionPoint(ijInstance.class);
        checkFacadeInjectionPoint(ijEvent.class);
        // check that UserTransaction is not injected into a SessionBean with container-managed transactions
        if (bean instanceof SessionBean<?>) {
            JtaApiAbstraction jtaApi = beanManager.getServices().get(JtaApiAbstraction.class);
            if (jtaApi.USER_TRANSACTION_CLASS.equals(ij.getType()) &&
                    (ij.getQualifiers().isEmpty() || ij.getQualifiers().contains(.)) &&
                    beanManager.getServices().get(EJBApiAbstraction.class).isSessionBeanWithContainerManagedTransactions(bean)) {
            }
        }
    }
    public void validateMetadataInjectionPoint(InjectionPoint ijBean<?> beanMessageCallback<DefinitionExceptionmessageCallback) {
        // metadata injection points
        if (ij.getType().equals(InjectionPoint.class) && bean == null) {
            throw messageCallback.construct(ij);
        }
        if (ij.getType().equals(InjectionPoint.class) && !Dependent.class.equals(bean.getScope())) {
            throw ..injectionIntoNonDependentBean(ij);
        }
        Class<?> rawType = Reflections.getRawType(ij.getType());
        if (Bean.class.equals(rawType) || Interceptor.class.equals(rawType) || Decorator.class.equals(rawType)) {
            if (bean == null) {
                throw messageCallback.construct(ij);
            }
            if (bean instanceof AbstractClassBean<?>) {
                checkBeanMetadataInjectionPoint(beanij, AnnotatedTypes.getDeclaringAnnotatedType(ij.getAnnotated()).getBaseType());
            }
            if (bean instanceof ProducerMethod<?, ?>) {
                ProducerMethod<?, ?> producerMethod = Reflections.cast(bean);
                checkBeanMetadataInjectionPoint(beanijproducerMethod.getAnnotated().getBaseType());
            }
        }
    }
        if (EventMetadata.class.equals(ip.getType()) && ip.getQualifiers().contains(.)) {
        }
    }

    
Checks for deployment problems associated with a given javax.enterprise.inject.spi.InjectionPoint
    public void validateInjectionPointForDeploymentProblems(InjectionPoint ijBean<?> beanBeanManagerImpl beanManager) {
        if (ij.isDelegate()) {
            return// do not validate delegate injection points as these are special
        }
        Set<?> resolvedBeans = beanManager.getBeanResolver().resolve(beanManager.getBeans(ij));
        if (!isInjectionPointSatisfied(ijresolvedBeansbeanManager)) {
                ij,
                Formats.formatAnnotations(ij.getQualifiers()),
                Formats.formatInjectionPointType(ij.getType()),
                Formats.formatAsStackTraceElement(ij),
                getUnsatisfiedDependenciesAdditionalInfo(ijbeanManager));
        }
        if (resolvedBeans.size() > 1) {
                ij,
                Formats.formatAnnotations(ij.getQualifiers()),
                Formats.formatInjectionPointType(ij.getType()),
                Formats.formatAsStackTraceElement(ij),
                WeldCollections.toMultiRowString(resolvedBeans));
        }
        // Account for the case this is disabled decorator
        if (!resolvedBeans.isEmpty()) {
            Bean<?> resolvedBean = (Bean<?>) resolvedBeans.iterator().next();
            if (beanManager.isNormalScope(resolvedBean.getScope())) {
                UnproxyableResolutionException ue = Proxies.getUnproxyableTypeException(ij.getType(), resolvedBeanbeanManager.getServices());
                if (ue != null) {
                    throw ..injectionPointHasNonProxyableDependencies(ijue);
                }
            }
            if (bean != null && Beans.isPassivatingScope(beanbeanManager)) {
                validateInjectionPointPassivationCapable(ijresolvedBeanbeanManager);
            }
        }
    }
        Set<Bean<?>> beansMatchedByType = beanManager.getBeans(ij.getType(), .);
        if (beansMatchedByType.isEmpty()) {
            Class<?> rawType = Reflections.getRawType(ij.getType());
            if (rawType != null) {
                MissingDependenciesRegistry missingDependenciesRegistry = beanManager.getServices().get(MissingDependenciesRegistry.class);
                String missingDependency = missingDependenciesRegistry.getMissingDependencyForClass(rawType.getName());
                if (missingDependency != null) {
                    return ..unsatisfiedDependencyBecauseClassIgnored(
                        rawType.getName(),
                        missingDependency);
                }
            }
        } else {
                WeldCollections.toMultiRowString(beansMatchedByType));
        }
        return "";
    }
    public void validateProducers(Collection<Producer<?>> producersBeanManagerImpl beanManager) {
        for (Producer<?> producer : producers) {
            validateProducer(producerbeanManager);
        }
    }
    public void validateProducer(Producer<?> producerBeanManagerImpl beanManager) {
        for (InjectionPoint injectionPoint : producer.getInjectionPoints()) {
            validateInjectionPoint(injectionPointbeanManager);
        }
    }
    private void checkScopeAnnotations(InjectionPoint ijMetaAnnotationStore metaAnnotationStore) {
        Annotated annotated = ij.getAnnotated();
        if (annotated instanceof EnhancedAnnotated<?, ?>) {
            EnhancedAnnotated<?, ?> weldAnnotated = (EnhancedAnnotated<?, ?>) annotated;
            Set<Annotationscopes = weldAnnotated.getMetaAnnotations(Scope.class);
            Set<AnnotationnormalScopes = weldAnnotated.getMetaAnnotations(NormalScope.class);
            for (Annotation annotation : scopes) {
                logScopeOnInjectionPointWarning(ijannotation);
            }
            for (Annotation annotation : normalScopes) {
                logScopeOnInjectionPointWarning(ijannotation);
            }
        } else {
            for (Annotation annotation : annotated.getAnnotations()) {
                if (hasScopeMetaAnnotation(annotation)) {
                    logScopeOnInjectionPointWarning(ijannotation);
                }
            }
        }
    }
    private void logScopeOnInjectionPointWarning(InjectionPoint ijAnnotation annotation) {
        ..scopeAnnotationOnInjectionPoint(annotationij);
    }
    private boolean hasScopeMetaAnnotation(Annotation annotation) {
        Class<? extends AnnotationannotationType = annotation.annotationType();
        return annotationType.isAnnotationPresent(Scope.class) || annotationType.isAnnotationPresent(NormalScope.class);
    }
    private boolean isInjectionPointPassivationCapable(InjectionPoint ijBean<?> resolvedBeanBeanManagerImpl beanManager) {
        if (!Beans.isPassivationCapableDependency(resolvedBean)) {
            if (((ij.getMember() instanceof Field) && ij.isTransient())) {
                return true;
            }
            if (ij.getAnnotated() instanceof AnnotatedParameter<?> && ij.getAnnotated().isAnnotationPresent(TransientReference.class)) {
                return true;
            }
            return false;
        }
        return true;
    }
    public void validateInjectionPointPassivationCapable(InjectionPoint ijBean<?> resolvedBeanBeanManagerImpl beanManager) {
        if (!isInjectionPointPassivationCapable(ijresolvedBeanbeanManager)) {
            throw ..injectionPointHasNonSerializableDependency(ij.getBean(), resolvedBean);
        }
    }
    public void validateInterceptorDecoratorInjectionPointPassivationCapable(InjectionPoint ijBean<?> resolvedBeanBeanManagerImpl beanManagerBean<?> bean) {
        if (!isInjectionPointPassivationCapable(ijresolvedBeanbeanManager)) {
            throw ..interceptorDecoratorInjectionPointHasNonSerializableDependency(beanij.getBean(), resolvedBean);
        }
    }
    public void validateDeployment(BeanManagerImpl managerBeanDeployment deployment) {
        validateDecorators(manager.getDecorators(), manager);
        validateInterceptors(manager.getInterceptors(), manager);
        validateBeans(manager.getBeans(), manager);
        validateEnabledDecoratorClasses(managerdeployment);
        validateEnabledInterceptorClasses(managerdeployment);
        validateEnabledAlternativeStereotypes(managerdeployment);
        validateEnabledAlternativeClasses(managerdeployment);
        validateSpecialization(manager);
        validateObserverMethods(deployment.getBeanDeployer().getEnvironment().getObservers(), manager);
        validateBeanNames(manager);
    }
    public void validateSpecialization(BeanManagerImpl manager) {
        for (Entry<AbstractBean<?, ?>> entry : registry.getBeansSpecializedInAnyDeploymentAsMultiset().entrySet()) {
            if (entry.getCount() > 1) {
                throw ..beanSpecializedTooManyTimes(entry.getElement());
            }
        }
    }
    public void validateBeans(Collection<? extends Bean<?>> beansBeanManagerImpl manager) {
        final List<RuntimeExceptionproblems = new ArrayList<RuntimeException>();
        final Set<CommonBean<?>> specializedBeans = new HashSet<CommonBean<?>>();
        for (Bean<?> bean : beans) {
            validateBean(beanspecializedBeansmanagerproblems);
        }
        if (!problems.isEmpty()) {
            if (problems.size() == 1) {
                throw problems.get(0);
            } else {
                throw new DeploymentException(problems);
            }
        }
    }
    protected void validateBean(Bean<?> beanCollection<CommonBean<?>> specializedBeansBeanManagerImpl managerList<RuntimeExceptionproblems) {
        try {
            if (bean instanceof CommonBean<?>) {
                validateRIBean((CommonBean<?>) beanmanagerspecializedBeans);
            } else {
                validateCustomBean(beanmanager);
            }
        } catch (RuntimeException e) {
            problems.add(e);
        }
    }
    public void validateInterceptors(Collection<? extends Interceptor<?>> interceptorsBeanManagerImpl manager) {
        for (Interceptor<?> interceptor : interceptors) {
            validateInterceptor(interceptormanager);
        }
    }
    protected void validateInterceptor(Interceptor<?> interceptorBeanManagerImpl manager) {
        if (interceptor instanceof InterceptorImpl<?>) {
            EnhancedAnnotatedType<?> annotated = ((InterceptorImpl<?>) interceptor).getEnhancedAnnotated();
            if (!BeanMethods.getObserverMethods(annotated).isEmpty()) {
                throw ..interceptorsCannotHaveObserverMethods(interceptor);
            }
            if (!interceptor.getScope().equals(Dependent.class)) {
                throw ..interceptorMustBeDependent(interceptor);
            }
            while (annotated != null && annotated.getJavaClass() != Object.class) {
                if (!annotated.getDeclaredEnhancedMethods(Produces.class).isEmpty()) {
                    throw ..interceptorsCannotHaveProducerMethods(interceptor);
                }
                if (!annotated.getDeclaredEnhancedFields(Produces.class).isEmpty()) {
                    throw ..interceptorsCannotHaveProducerFields(interceptor);
                }
                if (!annotated.getDeclaredEnhancedMethodsWithAnnotatedParameters(Disposes.class).isEmpty()) {
                    throw ..interceptorsCannotHaveDisposerMethods(interceptor);
                }
                annotated = annotated.getEnhancedSuperclass();
            }
        }
        for (InjectionPoint injectionPoint : interceptor.getInjectionPoints()) {
            validateInjectionPoint(injectionPointmanager);
        }
    }
    public void validateDecorators(Collection<? extends Decorator<?>> decoratorsBeanManagerImpl manager) {
        Set<CommonBean<?>> specializedBeans = new HashSet<CommonBean<?>>();
        for (Decorator<?> decorator : decorators) {
            validateDecorator(decoratorspecializedBeansmanager);
        }
    }
    protected void validateDecorator(Decorator<?> decoratorCollection<CommonBean<?>> specializedBeansBeanManagerImpl manager) {
        if (decorator.getDecoratedTypes().isEmpty()) {
            throw ..noDecoratedTypes(decorator);
        }
        Decorators.checkDelegateType(decorator);
        /*
         * Validate decorators of facade built-in beans
         */
        Type delegateType = decorator.getDelegateType();
        if (delegateType instanceof ParameterizedType) {
            ParameterizedType parameterizedDelegateType = (ParameterizedTypedelegateType;
            if (!Decorators.isPassivationCapable(decorator)) {
                if (Instance.class.equals(parameterizedDelegateType.getRawType()) || Provider.class.equals(parameterizedDelegateType.getRawType())) {
                    throw ..builtinBeanWithNonserializableDecorator(decoratorInstance.class.getName());
                }
                if (Event.class.equals(parameterizedDelegateType.getRawType())) {
                    throw ..builtinBeanWithNonserializableDecorator(decoratorEvent.class.getName());
                }
            }
        }
        if (decorator instanceof WeldDecorator<?>) {
            EnhancedAnnotatedType<?> annotated = ((WeldDecorator<?>) decorator).getEnhancedAnnotated();
            if (decorator instanceof DecoratorImpl<?>) {
                // Discovered decorator bean - abstract methods and delegate injection point are validated during bean initialization
                validateRIBean((CommonBean<?>) decoratormanagerspecializedBeans);
                // Following checks are not legal for custom decorator beans as we cannot rely on decorator bean class methods
                if (!BeanMethods.getObserverMethods(annotated).isEmpty()) {
                    throw ..decoratorsCannotHaveObserverMethods(decorator);
                }
                while (annotated != null && annotated.getJavaClass() != Object.class) {
                    if (!annotated.getDeclaredEnhancedMethods(Produces.class).isEmpty()) {
                        throw ..decoratorsCannotHaveProducerMethods(decorator);
                    }
                    if (!annotated.getDeclaredEnhancedFields(Produces.class).isEmpty()) {
                        throw ..decoratorsCannotHaveProducerFields(decorator);
                    }
                    if (!annotated.getDeclaredEnhancedMethodsWithAnnotatedParameters(Disposes.class).isEmpty()) {
                        throw ..decoratorsCannotHaveDisposerMethods(decorator);
                    }
                    annotated = annotated.getEnhancedSuperclass();
                }
            } else {
                // Custom decorator bean
                validateGeneralBean(decoratormanager);
                Decorators.findDelegateInjectionPoint(annotateddecorator.getInjectionPoints());
            }
        }
    }
    public void validateBeanNames(BeanManagerImpl beanManager) {
        SetMultimap<StringBean<?>> namedAccessibleBeans = Multimaps.newSetMultimap(new HashMap<StringCollection<Bean<?>>>(), HashSetSupplier.<Bean<?>>instance());
        for (Bean<?> bean : beanManager.getAccessibleBeans()) {
            if (bean.getName() != null) {
                namedAccessibleBeans.put(bean.getName(), bean);
            }
        }
        List<StringaccessibleNamespaces = new ArrayList<String>();
        for (String namespace : beanManager.getAccessibleNamespaces()) {
            accessibleNamespaces.add(namespace);
        }
        for (String name : namedAccessibleBeans.keySet()) {
            Set<Bean<?>> resolvedBeans = beanManager.getBeanResolver().<Object>resolve(Beans.removeDisabledBeans(namedAccessibleBeans.get(name), beanManagerregistry));
            if (resolvedBeans.size() > 1) {
                throw ..ambiguousElName(nameresolvedBeans);
            }
            if (accessibleNamespaces.contains(name)) {
                throw ..beanNameIsPrefix(name);
            }
        }
    }
    private void validateEnabledInterceptorClasses(BeanManagerImpl beanManagerBeanDeployment deployment) {
        BeansXml beansXml = deployment.getBeanDeploymentArchive().getBeansXml();
        if (beansXml != null && !beansXml.getEnabledInterceptors().isEmpty()) {
            Set<StringinterceptorBeanClasses = new HashSet<String>();
            for (Interceptor<?> interceptor : beanManager.getAccessibleInterceptors()) {
                interceptorBeanClasses.add(interceptor.getBeanClass().getName());
            }
            for (Metadata<StringinterceptorClassName : beansXml.getEnabledInterceptors()) {
                if (!interceptorBeanClasses.contains(interceptorClassName.getValue())) {
                    throw ..interceptorClassDoesNotMatchInterceptorBean(interceptorClassName);
                }
            }
        }
    }
    private void validateEnabledDecoratorClasses(BeanManagerImpl beanManagerBeanDeployment deployment) {
        BeansXml beansXml = deployment.getBeanDeploymentArchive().getBeansXml();
        if (beansXml != null && !beansXml.getEnabledDecorators().isEmpty()) {
            Set<StringdecoratorBeanClasses = new HashSet<String>();
            for (Decorator<?> bean : beanManager.getAccessibleDecorators()) {
                decoratorBeanClasses.add(bean.getBeanClass().getName());
            }
            for (Metadata<StringdecoratorClassName : beansXml.getEnabledDecorators()) {
                if (!decoratorBeanClasses.contains(decoratorClassName.getValue())) {
                    throw ..decoratorClassNotBeanClassOfDecorator(decoratorClassName, WeldCollections.toMultiRowString(decoratorBeanClasses));
                }
            }
        }
    }
    private void validateEnabledAlternativeStereotypes(BeanManagerImpl beanManagerBeanDeployment deployment) {
        BeansXml beansXml = deployment.getBeanDeploymentArchive().getBeansXml();
        if (beansXml != null && !beansXml.getEnabledAlternativeStereotypes().isEmpty()) {
            // prepare lookup structure
            Map<StringClass<? extends Annotation>> loadedStereotypes = buildClassNameMap(beanManager.getEnabled().getAlternativeStereotypes());
            for (Metadata<Stringdefinition : beansXml.getEnabledAlternativeStereotypes()) {
                Class<? extends Annotationstereotype = loadedStereotypes.get(definition.getValue());
                if (!beanManager.isStereotype(stereotype)) {
                    throw ..alternativeStereotypeNotStereotype(definition);
                }
                if (!isAlternative(beanManagerstereotype)) {
                    throw ..alternativeStereotypeNotAnnotated(definition);
                }
            }
        }
    }
    private void validateEnabledAlternativeClasses(BeanManagerImpl beanManagerBeanDeployment deployment) {
        BeansXml beansXml = deployment.getBeanDeploymentArchive().getBeansXml();
        if (beansXml != null && !beansXml.getEnabledAlternativeClasses().isEmpty()) {
            // prepare lookup structure
            Map<StringClass<?>> loadedClasses = buildClassNameMap(beanManager.getEnabled().getAlternativeClasses());
            // lookup structure for validation of alternatives
            Multimap<Class<?>, Bean<?>> beansByClass = HashMultimap.create();
            for (Bean<?> bean : beanManager.getAccessibleBeans()) {
                if (!(bean instanceof NewBean)) {
                    beansByClass.put(bean.getBeanClass(), bean);
                }
            }
            for (Metadata<Stringdefinition : beansXml.getEnabledAlternativeClasses()) {
                Class<?> enabledClass = loadedClasses.get(definition.getValue());
                if (enabledClass.isAnnotation() || enabledClass.isInterface()) {
                    throw ..alternativeBeanClassNotClass(definition);
                } else {
                    // check that the class is a bean class of at least one alternative
                    boolean alternativeBeanFound = false;
                    for (Bean<?> bean : beansByClass.get(enabledClass)) {
                        if (bean.isAlternative()) {
                            alternativeBeanFound = true;
                        }
                    }
                    if (!alternativeBeanFound) {
                        throw ..alternativeBeanClassNotAnnotated(definition);
                    }
                }
            }
        }
    }
    private static boolean isAlternative(BeanManager beanManagerClass<? extends Annotationstereotype) {
        for (Annotation annotation : beanManager.getStereotypeDefinition(stereotype)) {
            if (annotation.annotationType().equals(Alternative.class)) {
                return true;
            }
        }
        return false;
    }
    private void validateDisposalMethods(BeanDeployerEnvironment environment) {
        Set<DisposalMethod<?, ?>> beans = environment.getUnresolvedDisposalBeans();
        if (!beans.isEmpty()) {
            throw ..disposalMethodsWithoutProducer(WeldCollections.toMultiRowString(beans));
        }
    }
    protected void validateObserverMethods(Iterable<ObserverInitializationContext<?, ?>> observersBeanManagerImpl beanManager) {
        for (ObserverInitializationContext<?, ?> omi : observers) {
            for (InjectionPoint ip : omi.getObserver().getInjectionPoints()) {
                validateInjectionPointForDefinitionErrors(ipip.getBean(), beanManager);
                validateMetadataInjectionPoint(ipnull.);
                validateInjectionPointForDeploymentProblems(ipip.getBean(), beanManager);
            }
        }
    }
    private static void checkFacadeInjectionPoint(InjectionPoint injectionPointClass<?> type) {
        Type injectionPointType = injectionPoint.getType();
        if (injectionPointType instanceof Class<?> && type.equals(injectionPointType)) {
            throw ..injectionPointMustHaveTypeParameter(typeinjectionPoint);
        }
        if (injectionPointType instanceof ParameterizedType && !injectionPoint.isDelegate()) {
            ParameterizedType parameterizedType = (ParameterizedTypeinjectionPointType;
            if (type.equals(parameterizedType.getRawType())) {
                if (parameterizedType.getActualTypeArguments()[0] instanceof TypeVariable<?>) {
                    throw ..injectionPointWithTypeVariable(injectionPoint);
                }
                if (parameterizedType.getActualTypeArguments()[0] instanceof WildcardType) {
                    throw ..injectionPointHasWildcard(typeinjectionPoint);
                }
            }
        }
    }
    public static void checkBeanMetadataInjectionPoint(Object beanInjectionPoint ipType expectedTypeArgument) {
        if (!(ip.getType() instanceof ParameterizedType)) {
            throw ..invalidBeanMetadataInjectionPointType(ip.getType(), ip);
        }
        ParameterizedType parameterizedType = (ParameterizedTypeip.getType();
        if (parameterizedType.getActualTypeArguments().length != 1) {
            throw ..invalidBeanMetadataInjectionPointType(ip.getType(), ip);
        }
        Class<?> rawType = (Class<?>) parameterizedType.getRawType();
        Type typeArgument = parameterizedType.getActualTypeArguments()[0];
        if (bean == null) {
            throw ..injectionIntoNonBean(ip);
        }
        /*
         * If an Interceptor instance is injected into a bean instance other than an interceptor instance, the container
         * automatically detects the problem and treats it as a definition error.
         */
        if (rawType.equals(Interceptor.class) && !(bean instanceof Interceptor<?>)) {
            throw ..invalidBeanMetadataInjectionPointType(ip.getType(), ip);
        }
        /*
         * If a Decorator instance is injected into a bean instance other than a decorator instance, the container automatically
         * detects the problem and treats it as a definition error.
         */
        if (rawType.equals(Decorator.class) && !(bean instanceof Decorator<?>)) {
            throw ..invalidBeanMetadataInjectionPointType(ip.getType(), ip);
        }
        Set<Annotationqualifiers = ip.getQualifiers();
        if (qualifiers.contains(.)) {
            /*
             * If a Bean instance with qualifier @Intercepted is injected into a bean instance other than an interceptor
             * instance, the container automatically detects the problem and treats it as a definition error.
             */
            if (!(bean instanceof Interceptor<?>)) {
                throw ..invalidBeanMetadataInjectionPointQualifier(Intercepted.classInterceptor.classip);
            }
            /*
             * If the injection point is a field, an initializer method parameter or a bean constructor of an interceptor, with
             * qualifier @Intercepted, then the type parameter of the injected Bean must be an unbounded wildcard.
             */
            if (!rawType.equals(Bean.class)) {
                throw ..invalidBeanMetadataInjectionPointType(ip.getType(), ip);
            }
            if (!Reflections.isUnboundedWildcard(typeArgument)) {
                throw ..invalidBeanMetadataInjectionPointTypeArgument(typeArgumentip);
            }
        }
        if (qualifiers.contains(.)) {
            /*
             * If a Bean instance with qualifier @Decorated is injected into a bean instance other than a decorator instance,
             * the container automatically detects the problem and treats it as a definition error.
             */
            if (!(bean instanceof Decorator<?>)) {
                throw ..invalidBeanMetadataInjectionPointQualifier(Decorated.classDecorator.classip);
            }
            Decorator<?> decorator = Reflections.cast(bean);
            /*
             * If the injection point is a field, an initializer method parameter or a bean constructor of a decorator, with
             * qualifier @Decorated, then the type parameter of the injected Bean must be the same as the delegate type.
             */
            if (!rawType.equals(Bean.class)) {
                throw ..invalidBeanMetadataInjectionPointType(ip.getType(), ip);
            }
            if (!typeArgument.equals(decorator.getDelegateType())) {
                throw ..invalidBeanMetadataInjectionPointTypeArgument(typeArgumentip);
            }
        }
        if (qualifiers.contains(.)) {
            /*
             * If the injection point is a field, an initializer method parameter or a bean constructor, with qualifier
             * @Default, then the type parameter of the injected Bean, Interceptor or Decorator must be the same as the type
             * declaring the injection point.
             *
             * If the injection point is a producer method parameter then the type parameter of the injected Bean must be the
             * same as the producer method return type.
             *
             * If the injection point is a disposer method parameter then the type parameter of the injected Bean must be the
             * same as the disposed parameter.
             */
            if (!expectedTypeArgument.equals(typeArgument)) {
                throw ..invalidBeanMetadataInjectionPointTypeArgument(typeArgumentip);
            }
        }
    }
    private static boolean isInjectionPointSatisfied(InjectionPoint ijSet<?> resolvedBeansBeanManagerImpl beanManager) {
        if (ij.getBean() instanceof Decorator<?>) {
            if (beanManager.getEnabled().isDecoratorEnabled(ij.getBean().getBeanClass())) {
                return resolvedBeans.size() > 0;
            } else {
                return true;
            }
        } else {
            return resolvedBeans.size() > 0;
        }
    }

    
Checks to make sure that pseudo scoped beans (i.e.

Dependent:
scoped beans) have no circular dependencies.
    private static void validatePseudoScopedBean(Bean<?> beanBeanManagerImpl beanManager) {
        reallyValidatePseudoScopedBean(beanbeanManagernew LinkedHashSet<Object>(), new HashSet<Bean<?>>());
    }

    
checks if a bean has been seen before in the dependencyPath. If not, it resolves the InjectionPoints and adds the resolved beans to the set of beans to be validated
    private static void reallyValidatePseudoScopedBean(Bean<?> beanBeanManagerImpl beanManagerSet<ObjectdependencyPathSet<Bean<?>> validatedBeans) {
        // see if we have already seen this bean in the dependency path
        if (dependencyPath.contains(bean)) {
            // create a list that shows the path to the bean
            List<ObjectrealDependencyPath = new ArrayList<Object>(dependencyPath);
            realDependencyPath.add(bean);
            throw ..pseudoScopedBeanHasCircularReferences(WeldCollections.toMultiRowString(realDependencyPath));
        }
        if (validatedBeans.contains(bean)) {
            return;
        }
        dependencyPath.add(bean);
        for (InjectionPoint injectionPoint : bean.getInjectionPoints()) {
            if (!injectionPoint.isDelegate()) {
                dependencyPath.add(injectionPoint);
                validatePseudoScopedInjectionPoint(injectionPointbeanManagerdependencyPathvalidatedBeans);
                dependencyPath.remove(injectionPoint);
            }
        }
        if (bean instanceof DecorableBean<?>) {
            final List<Decorator<?>> decorators = Reflections.<DecorableBean<?>>cast(bean).getDecorators();
            if (!decorators.isEmpty()) {
                for (final Decorator<?> decorator : decorators) {
                    reallyValidatePseudoScopedBean(decoratorbeanManagerdependencyPathvalidatedBeans);
                }
            }
        }
        if (bean instanceof AbstractProducerBean<?, ?, ?> && !(bean instanceof EEResourceProducerField<?, ?>)) {
            AbstractProducerBean<?, ?, ?> producer = (AbstractProducerBean<?, ?, ?>) bean;
            if (!beanManager.isNormalScope(producer.getDeclaringBean().getScope()) && !producer.getAnnotated().isStatic()) {
                reallyValidatePseudoScopedBean(producer.getDeclaringBean(), beanManagerdependencyPathvalidatedBeans);
            }
        }
        validatedBeans.add(bean);
        dependencyPath.remove(bean);
    }
    private static void validatePseudoScopedInjectionPoint(InjectionPoint ijBeanManagerImpl beanManagerSet<ObjectdependencyPathSet<Bean<?>> validatedBeans) {
        Set<Bean<?>> resolved = beanManager.getBeans(ij);
        Bean<?> bean = beanManager.resolve(resolved);
        if (bean != null) {
            if (!(bean instanceof AbstractBuiltInBean<?>)) {
                if (!ij.isDelegate()) {
                    boolean normalScoped = beanManager.isNormalScope(bean.getScope());
                    if (!normalScoped && !(bean instanceof SessionBean)) {
                        reallyValidatePseudoScopedBean(beanbeanManagerdependencyPathvalidatedBeans);
                    }
                }
            }
        }