javax.enterprise.inject.spi.BeanAttributes Java Examples

The following examples show how to use javax.enterprise.inject.spi.BeanAttributes. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source Project: HotswapAgent   Author: HotswapProjects   File: BeanReloadExecutor.java    License: GNU General Public License v2.0 6 votes vote down vote up
@SuppressWarnings({ "rawtypes", "unchecked" })
private static void doDefineNewManagedBean(BeanManagerImpl beanManager, String bdaId, Class<?> beanClass) {
    try {
        ClassTransformer classTransformer = getClassTransformer();
        SlimAnnotatedType<?> annotatedType = classTransformer.getBackedAnnotatedType(beanClass, bdaId);
        boolean managedBeanOrDecorator = Beans.isTypeManagedBeanOrDecoratorOrInterceptor(annotatedType);

        if (managedBeanOrDecorator) {
            EnhancedAnnotatedType eat = EnhancedAnnotatedTypeImpl.of(annotatedType, classTransformer);
            BeanAttributes attributes = BeanAttributesFactory.forBean(eat, beanManager);
            ManagedBean<?> bean = ManagedBean.of(attributes, eat, beanManager);
            ReflectionHelper.set(beanManager, beanManager.getClass(), "beanSet", Collections.synchronizedSet(new HashSet<Bean<?>>()));
            beanManager.addBean(bean);
            beanManager.getBeanResolver().clear();
            bean.initializeAfterBeanDiscovery();
            LOGGER.debug("Bean defined '{}'", beanClass.getName());
        } else {
            // TODO : define session bean
            LOGGER.warning("Bean NOT? defined '{}', session bean?", beanClass.getName());
        }
    } catch (Exception e) {
        LOGGER.debug("Bean definition failed.", e);
    }
}
 
Example #2
Source Project: smallrye-jwt   Author: smallrye   File: ProviderExtensionSupport.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Replace the general producer method BeanAttributes with one bound to the collected injection site
 * types to properly reflect all of the type locations the producer method applies to.
 *
 * @param pba the ProcessBeanAttributes
 * @see ProviderBeanAttributes
 */
public void addTypeToClaimProducer(@Observes ProcessBeanAttributes pba) {
    if (!providerOptionalTypes.isEmpty() && pba.getAnnotated().isAnnotationPresent(Claim.class)) {
        Claim claim = pba.getAnnotated().getAnnotation(Claim.class);
        if (claim.value().length() == 0 && claim.standard() == Claims.UNKNOWN) {
            CDILogging.log.addTypeToClaimProducer(pba.getAnnotated());
            BeanAttributes delegate = pba.getBeanAttributes();
            if (delegate.getTypes().contains(Optional.class)) {
                pba.setBeanAttributes(new ProviderBeanAttributes(delegate, providerOptionalTypes, providerQualifiers));
            }
        }
    }
}
 
Example #3
Source Project: smallrye-reactive-messaging   Author: smallrye   File: ConfiguredChannelFactory.java    License: Apache License 2.0 5 votes vote down vote up
private List<String> getConnectors(BeanManager beanManager, Class<?> clazz) {
    return beanManager.getBeans(clazz, Any.Literal.INSTANCE).stream()
            .map(BeanAttributes::getQualifiers)
            .flatMap(set -> set.stream().filter(a -> a.annotationType().equals(Connector.class)))
            .map(annotation -> ((Connector) annotation).value())
            .collect(Collectors.toList());
}
 
Example #4
Source Project: tomee   Author: apache   File: CdiEjbBean.java    License: Apache License 2.0 5 votes vote down vote up
public CdiEjbBean(final BeanContext bc, final WebBeansContext webBeansContext, final Class beanClass, final AnnotatedType<T> at,
                  final InjectionTargetFactoryImpl<T> factory, final BeanAttributes<T> attributes) {
    super(webBeansContext, toSessionType(bc.getComponentType()), at,
            new EJBBeanAttributesImpl<T>(bc, attributes),
            beanClass, factory);
    this.beanContext = bc;
    bc.set(Bean.class, this);
    passivatingId = bc.getDeploymentID() + getReturnType().getName();

    final boolean stateful = BeanType.STATEFUL.equals(bc.getComponentType());
    final boolean isDependent = getScope().equals(Dependent.class);
    isDependentAndStateful = isDependent && stateful;
    if (webBeansContext.getBeanManagerImpl().isPassivatingScope(getScope()) && stateful) {
        if (!getBeanContext().isPassivable()) {
            throw new DefinitionException(
                    getBeanContext().getBeanClass()
                            + " is a not apssivation-capable @Stateful with a scope "
                            + getScope().getSimpleName() + " which need passivation");
        }
        passivable = true;
    } else {
        passivable = false;
    }
    if (!isDependent) {
        for (final Type type : attributes.getTypes()) {
            if (ParameterizedType.class.isInstance(type)) {
                throw new DefinitionException("Parameterized session bean should be @Dependent: " + beanClass);
            }
        }
    }
    if (getAnnotatedType().isAnnotationPresent(Interceptor.class) || getAnnotatedType().isAnnotationPresent(Decorator.class)) {
        throw new DefinitionException("An EJB can't be an interceptor or a decorator: " + beanClass);
    }
}
 
Example #5
Source Project: quarkus   Author: quarkusio   File: BeanManagerImpl.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public <T> BeanAttributes<T> createBeanAttributes(AnnotatedType<T> type) {
    throw new UnsupportedOperationException();
}
 
Example #6
Source Project: quarkus   Author: quarkusio   File: BeanManagerImpl.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public BeanAttributes<?> createBeanAttributes(AnnotatedMember<?> type) {
    throw new UnsupportedOperationException();
}
 
Example #7
Source Project: quarkus   Author: quarkusio   File: BeanManagerImpl.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public <T> Bean<T> createBean(BeanAttributes<T> attributes, Class<T> beanClass,
        InjectionTargetFactory<T> injectionTargetFactory) {
    throw new UnsupportedOperationException();
}
 
Example #8
Source Project: quarkus   Author: quarkusio   File: BeanManagerImpl.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public <T, X> Bean<T> createBean(BeanAttributes<T> attributes, Class<X> beanClass, ProducerFactory<X> producerFactory) {
    throw new UnsupportedOperationException();
}
 
Example #9
Source Project: thorntail   Author: thorntail   File: ImplicitArchiveExtension.java    License: Apache License 2.0 4 votes vote down vote up
<T> void processBeanAttributes(@Observes ProcessBeanAttributes<T> pba, BeanManager beanManager) throws Exception {
    final BeanAttributes<T> beanAttributes = pba.getBeanAttributes();
    if (beanAttributes.getTypes().contains(Archive.class)) {
        if (!DeploymentScoped.class.isAssignableFrom(beanAttributes.getScope())) {
            pba.setBeanAttributes(new BeanAttributes<T>() {
                @Override
                public Set<Type> getTypes() {
                    return beanAttributes.getTypes();
                }

                @Override
                public Set<Annotation> getQualifiers() {
                    Set<Annotation> qualifiers = new HashSet<>();
                    qualifiers.addAll(beanAttributes.getQualifiers());
                    qualifiers.add(ImplicitDeployment.Literal.INSTANCE);
                    qualifiers.removeIf(e -> Default.class.isAssignableFrom(e.getClass()));
                    return qualifiers;
                }

                @Override
                public Class<? extends Annotation> getScope() {
                    return beanAttributes.getScope();
                }

                @Override
                public String getName() {
                    return beanAttributes.getName();
                }

                @Override
                public Set<Class<? extends Annotation>> getStereotypes() {
                    return beanAttributes.getStereotypes();
                }

                @Override
                public boolean isAlternative() {
                    return beanAttributes.isAlternative();
                }
            });
        }
    }
}
 
Example #10
Source Project: HotswapAgent   Author: HotswapProjects   File: BeanClassRefreshAgent.java    License: GNU General Public License v2.0 4 votes vote down vote up
@SuppressWarnings("rawtypes")
private static void doDefineNewBean(BeanManagerImpl beanManager, Class<?> beanClass, URL beanArchiveUrl) {

    BeanArchiveInformation beanArchiveInfo =
            beanManager.getWebBeansContext().getBeanArchiveService().getBeanArchiveInformation(beanArchiveUrl);

    if (beanArchiveInfo.isClassExcluded(beanClass.getName())) {
        LOGGER.debug("Bean '{}' is excluded in BeanArchive.", beanClass.getName());
        return;
    }

    if (beanArchiveInfo.getBeanDiscoveryMode() == BeanDiscoveryMode.ANNOTATED) {
        if (beanClass.getAnnotations().length == 0 || !isCDIAnnotatedClass(beanManager, beanClass)) {
            LOGGER.debug("Class '{}' is not considered as bean for BeanArchive with bean-discovery-mode=\"annotated\"", beanClass.getName());
            return;
        }
    }

    WebBeansContext wbc = beanManager.getWebBeansContext();

    AnnotatedElementFactory annotatedElementFactory = wbc.getAnnotatedElementFactory();
    // Clear AnnotatedElementFactory caches (is it necessary for definition ?)
    annotatedElementFactory.clear();

    // Injection resolver cache must be cleared before / after definition
    beanManager.getInjectionResolver().clearCaches();

    AnnotatedType<?> annotatedType = annotatedElementFactory.newAnnotatedType(beanClass);
    BeanAttributesImpl<?> attributes = BeanAttributesBuilder.forContext(wbc).newBeanAttibutes(annotatedType).build();
    Map<AnnotatedType<?>, ExtendedBeanAttributes<?>> annotatedTypes = new HashMap<>();

    BeansDeployer beansDeployer = new BeansDeployer(wbc);

    try {
        // OWB 1.7
        ReflectionHelper.invoke(beansDeployer, BeansDeployer.class, "defineManagedBean",
                new Class[] { javax.enterprise.inject.spi.AnnotatedType.class, BeanAttributes.class, java.util.Map.class },
                annotatedType, attributes, annotatedTypes);
    } catch (Exception e) {
        try {
            // OWB 2.0
            ExtendedBeanAttributes extendedBeanAttributes =
                    ExtendedBeanAttributes.class.getConstructor(BeanAttributes.class, boolean.class, boolean.class)
                    .newInstance(attributes, false, false);
            ReflectionHelper.invoke(beansDeployer, BeansDeployer.class, "defineManagedBean",
                    new Class[] { javax.enterprise.inject.spi.AnnotatedType.class, ExtendedBeanAttributes.class, java.util.Map.class },
                    annotatedType, extendedBeanAttributes, annotatedTypes);
        } catch (Exception ex) {
            LOGGER.error("Bean '{}' definition failed.", beanClass.getName());
        }
    }
}
 
Example #11
Source Project: tomee   Author: apache   File: CdiPlugin.java    License: Apache License 2.0 4 votes vote down vote up
public <T> BeanAttributes<T> createBeanAttributes(final AnnotatedType<T> type) {
    return new CdiEjbBean.EJBBeanAttributesImpl(
            findBeanContext(webBeansContext, type.getJavaClass()),
            BeanAttributesBuilder.forContext(webBeansContext).newBeanAttibutes(type).build());
}
 
Example #12
Source Project: tomee   Author: apache   File: CdiEjbBean.java    License: Apache License 2.0 4 votes vote down vote up
public CdiEjbBean(final BeanContext beanContext, final WebBeansContext webBeansContext, final AnnotatedType<T> at,
                  final BeanAttributes<T> attributes) {
    this(beanContext, webBeansContext, beanContext.getManagedClass(), at, new EjbInjectionTargetFactory<T>(beanContext, at, webBeansContext), attributes);
    EjbInjectionTargetImpl.class.cast(getInjectionTarget()).setCdiEjbBean(this);
}
 
Example #13
Source Project: tomee   Author: apache   File: CdiEjbBean.java    License: Apache License 2.0 4 votes vote down vote up
public EJBBeanAttributesImpl(final BeanContext bc, final BeanAttributes<T> beanAttributes) {
    super(beanAttributes, false);
    this.beanContext = bc;
    this.ejbTypes = new HashSet<>();
    initTypes();
}
 
Example #14
Source Project: tomee   Author: apache   File: OpenEJBBeanBuilder.java    License: Apache License 2.0 4 votes vote down vote up
public OpenEJBBeanBuilder(final BeanContext bc, final WebBeansContext webBeansContext, final AnnotatedType<A> annotatedType,
                          final BeanAttributes<A> attributes) {
    super(webBeansContext, annotatedType, attributes);
    this.beanContext = bc;
}
 
Example #15
Source Project: microprofile-jwt-auth   Author: eclipse   File: ConfigPropertyBeanAttribute.java    License: Apache License 2.0 3 votes vote down vote up
/**
 * Decorate the ConfigPropertyProducer BeanAttributes to set the types the producer applies to. This set is collected
 * from all injection points annotated with @ConfigProperty.
 *
 * @see MPConfigExtension#processConfigPropertyInjections
 * @see ConfigPropertyProducer#produceConfigProperty(javax.enterprise.inject.spi.InjectionPoint)
 *
 * @param delegate - the original producer method BeanAttributes
 * @param types - the full set of @ConfigProperty injection point types
 */
public ConfigPropertyBeanAttribute(BeanAttributes<Object> delegate, Set<Type> types) {
    this.delegate = delegate;
    this.types = types;
    if(this.types.size() == 0) {
        this.types.add(String.class);
    }
}
 
Example #16
Source Project: smallrye-jwt   Author: smallrye   File: ProviderExtensionSupport.java    License: Apache License 2.0 2 votes vote down vote up
/**
 * Decorate the ConfigPropertyProducer BeanAttributes to set the types the producer applies to. This set is collected
 * from all injection points annotated with @ConfigProperty.
 *
 * @param delegate - the original producer method BeanAttributes
 * @param types - the full set of @Claim injection point types
 * @param qualifiers - @Claim qualifiers
 */
public ProviderBeanAttributes(BeanAttributes<Object> delegate, Set<Type> types, Set<Annotation> qualifiers) {
    this.delegate = delegate;
    this.types = types;
    this.qualifiers = qualifiers;
}