Search in sources :

Example 1 with NewBean

use of org.jboss.weld.bean.NewBean in project core by weld.

the class Validator method validateRIBean.

/**
 * Validate an RIBean. This includes validating whether two beans specialize
 * the same bean
 *
 * @param bean             the bean to validate
 * @param beanManager      the current manager
 * @param specializedBeans the existing specialized beans
 */
protected void validateRIBean(CommonBean<?> bean, BeanManagerImpl beanManager, Collection<CommonBean<?>> specializedBeans) {
    validateGeneralBean(bean, beanManager);
    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(beanManager, classBean);
        }
    }
    // 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(ip, null, beanManager);
                    validateMetadataInjectionPoint(ip, null, ValidatorLogger.INJECTION_INTO_DISPOSER_METHOD);
                    validateEventMetadataInjectionPoint(ip);
                    validateInjectionPointForDeploymentProblems(ip, null, beanManager);
                }
            }
        }
    }
}
Also used : InjectionPoint(javax.enterprise.inject.spi.InjectionPoint) AbstractClassBean(org.jboss.weld.bean.AbstractClassBean) DecorableBean(org.jboss.weld.bean.DecorableBean) AbstractProducerBean(org.jboss.weld.bean.AbstractProducerBean) NewBean(org.jboss.weld.bean.NewBean) AbstractMemberProducer(org.jboss.weld.injection.producer.AbstractMemberProducer)

Example 2 with NewBean

use of org.jboss.weld.bean.NewBean in project core by weld.

the class Validator method validateInjectionPointForDefinitionErrors.

/**
 * Checks for definition errors associated with a given {@link InjectionPoint}
 */
public void validateInjectionPointForDefinitionErrors(InjectionPoint ij, Bean<?> bean, BeanManagerImpl beanManager) {
    if (ij.getAnnotated().getAnnotation(New.class) != null && ij.getQualifiers().size() > 1) {
        throw ValidatorLogger.LOG.newWithQualifiers(ij, Formats.formatAsStackTraceElement(ij));
    }
    if (ij.getType() instanceof TypeVariable<?>) {
        throw ValidatorLogger.LOG.injectionPointWithTypeVariable(ij, Formats.formatAsStackTraceElement(ij));
    }
    // WELD-1739
    if (ij.getMember() instanceof Executable && ij.getAnnotated().isAnnotationPresent(Named.class) && ij.getAnnotated().getAnnotation(Named.class).value().equals("")) {
        Executable executable = (Executable) ij.getMember();
        AnnotatedParameter<?> annotatedParameter = (AnnotatedParameter<?>) ij.getAnnotated();
        if (!executable.getParameters()[annotatedParameter.getPosition()].isNamePresent()) {
            // No parameters info available
            throw ValidatorLogger.LOG.nonFieldInjectionPointCannotUseNamed(ij, Formats.formatAsStackTraceElement(ij));
        }
    }
    if (ij.getAnnotated().isAnnotationPresent(Produces.class)) {
        if (bean != null) {
            throw BeanLogger.LOG.injectedFieldCannotBeProducer(ij.getAnnotated(), bean);
        } else {
            throw BeanLogger.LOG.injectedFieldCannotBeProducer(ij.getAnnotated(), Reflections.<AnnotatedField<?>>cast(ij.getAnnotated()).getDeclaringType());
        }
    }
    boolean newBean = (bean instanceof NewBean);
    if (!newBean) {
        checkScopeAnnotations(ij, beanManager.getServices().get(MetaAnnotationStore.class));
    }
    checkFacadeInjectionPoint(ij, Instance.class);
    checkFacadeInjectionPoint(ij, Event.class);
    if (InterceptionFactory.class.equals(Reflections.getRawType(ij.getType())) && !(bean instanceof ProducerMethod<?, ?>)) {
        throw ValidatorLogger.LOG.invalidInterceptionFactoryInjectionPoint(ij, Formats.formatAsStackTraceElement(ij));
    }
    for (PlugableValidator validator : plugableValidators) {
        validator.validateInjectionPointForDefinitionErrors(ij, bean, beanManager);
    }
}
Also used : PlugableValidator(org.jboss.weld.module.PlugableValidator) Named(javax.inject.Named) TypeVariable(java.lang.reflect.TypeVariable) AnnotatedParameter(javax.enterprise.inject.spi.AnnotatedParameter) MetaAnnotationStore(org.jboss.weld.metadata.cache.MetaAnnotationStore) NewBean(org.jboss.weld.bean.NewBean) InterceptionFactory(javax.enterprise.inject.spi.InterceptionFactory) Executable(java.lang.reflect.Executable) AnnotatedField(javax.enterprise.inject.spi.AnnotatedField)

Example 3 with NewBean

use of org.jboss.weld.bean.NewBean in project core by weld.

the class Validator method validateEnabledAlternativeClasses.

private void validateEnabledAlternativeClasses(BeanManagerImpl beanManager, BeanDeployment deployment) {
    BeansXml beansXml = deployment.getBeanDeploymentArchive().getBeansXml();
    if (beansXml != null && !beansXml.getEnabledAlternativeClasses().isEmpty()) {
        // prepare lookup structure
        Map<String, Class<?>> loadedClasses = buildClassNameMap(beanManager.getEnabled().getAlternativeClasses());
        // lookup structure for validation of alternatives
        Multimap<Class<?>, Bean<?>> beansByClass = SetMultimap.newSetMultimap();
        for (Bean<?> bean : beanManager.getDynamicAccessibleBeans()) {
            if (!(bean instanceof NewBean)) {
                beansByClass.put(bean.getBeanClass(), bean);
            }
        }
        for (Metadata<String> definition : beansXml.getEnabledAlternativeClasses()) {
            Class<?> enabledClass = loadedClasses.get(definition.getValue());
            if (enabledClass.isAnnotation() || enabledClass.isInterface()) {
                throw ValidatorLogger.LOG.alternativeBeanClassNotClass(definition);
            } else {
                if (!isAlternativeBean(enabledClass, beansByClass) && !isAlternativeCandidate(enabledClass, beanManager)) {
                    throw ValidatorLogger.LOG.alternativeBeanClassNotAnnotated(definition.getValue(), definition.getLocation());
                }
            }
        }
    }
}
Also used : BeansXml(org.jboss.weld.bootstrap.spi.BeansXml) NewBean(org.jboss.weld.bean.NewBean) SessionBean(org.jboss.weld.bean.SessionBean) AbstractClassBean(org.jboss.weld.bean.AbstractClassBean) DecorableBean(org.jboss.weld.bean.DecorableBean) AbstractBuiltInBean(org.jboss.weld.bean.builtin.AbstractBuiltInBean) NewBean(org.jboss.weld.bean.NewBean) AbstractBean(org.jboss.weld.bean.AbstractBean) AbstractDecorableBuiltInBean(org.jboss.weld.bean.builtin.AbstractDecorableBuiltInBean) AbstractProducerBean(org.jboss.weld.bean.AbstractProducerBean) CommonBean(org.jboss.weld.bean.CommonBean) Bean(javax.enterprise.inject.spi.Bean)

Aggregations

NewBean (org.jboss.weld.bean.NewBean)3 AbstractClassBean (org.jboss.weld.bean.AbstractClassBean)2 AbstractProducerBean (org.jboss.weld.bean.AbstractProducerBean)2 DecorableBean (org.jboss.weld.bean.DecorableBean)2 Executable (java.lang.reflect.Executable)1 TypeVariable (java.lang.reflect.TypeVariable)1 AnnotatedField (javax.enterprise.inject.spi.AnnotatedField)1 AnnotatedParameter (javax.enterprise.inject.spi.AnnotatedParameter)1 Bean (javax.enterprise.inject.spi.Bean)1 InjectionPoint (javax.enterprise.inject.spi.InjectionPoint)1 InterceptionFactory (javax.enterprise.inject.spi.InterceptionFactory)1 Named (javax.inject.Named)1 AbstractBean (org.jboss.weld.bean.AbstractBean)1 CommonBean (org.jboss.weld.bean.CommonBean)1 SessionBean (org.jboss.weld.bean.SessionBean)1 AbstractBuiltInBean (org.jboss.weld.bean.builtin.AbstractBuiltInBean)1 AbstractDecorableBuiltInBean (org.jboss.weld.bean.builtin.AbstractDecorableBuiltInBean)1 BeansXml (org.jboss.weld.bootstrap.spi.BeansXml)1 AbstractMemberProducer (org.jboss.weld.injection.producer.AbstractMemberProducer)1 MetaAnnotationStore (org.jboss.weld.metadata.cache.MetaAnnotationStore)1