Java Code Examples for org.apache.webbeans.config.WebBeansContext#getAnnotatedElementFactory()

The following examples show how to use org.apache.webbeans.config.WebBeansContext#getAnnotatedElementFactory() . 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 File: BeanClassRefreshAgent.java    From HotswapAgent with GNU General Public License v2.0 4 votes vote down vote up
@SuppressWarnings({ "rawtypes", "unchecked" })
private static void createAnnotatedTypeForExistingBeanClass(BeanManagerImpl beanManager, InjectionTargetBean bean) {

    WebBeansContext wbc = beanManager.getWebBeansContext();

    Object forwardingMethIterceptors = null;

    if (bean.getProducer() instanceof AbstractProducer) {
        // methodInterceptors must be the same instance. It is stored in field owbIntDecHandler of existing
        // InterceptedProxy's instances
        try {
            forwardingMethIterceptors = ReflectionHelper.get(bean.getProducer(), "methodInterceptors");
        } catch (IllegalArgumentException e) {
            LOGGER.warning("Field AbstractProducer.methodInterceptors is not accessible", e);
        }
    }

    AnnotatedElementFactory annotatedElementFactory = wbc.getAnnotatedElementFactory();
    // Clear AnnotatedElementFactory caches
    annotatedElementFactory.clear();

    AnnotatedType annotatedType = annotatedElementFactory.newAnnotatedType(bean.getBeanClass());

    ReflectionHelper.set(bean, InjectionTargetBean.class, "annotatedType", annotatedType);

    // Updated members that were set by bean attributes
    BeanAttributesImpl attributes = BeanAttributesBuilder.forContext(wbc).newBeanAttibutes(annotatedType).build();
    ReflectionHelper.set(bean, BeanAttributesImpl.class, "types", attributes.getTypes());
    ReflectionHelper.set(bean, BeanAttributesImpl.class, "qualifiers", attributes.getQualifiers());
    ReflectionHelper.set(bean, BeanAttributesImpl.class, "scope", attributes.getScope());
    ReflectionHelper.set(bean, BeanAttributesImpl.class, "name", attributes.getName());
    ReflectionHelper.set(bean, BeanAttributesImpl.class, "stereotypes", attributes.getStereotypes());
    ReflectionHelper.set(bean, BeanAttributesImpl.class, "alternative", attributes.isAlternative());

    InjectionTargetFactory factory = new InjectionTargetFactoryImpl(annotatedType, bean.getWebBeansContext());
    InjectionTarget injectionTarget = factory.createInjectionTarget(bean);
    ReflectionHelper.set(bean, InjectionTargetBean.class, "injectionTarget", injectionTarget);

    if (injectionTarget instanceof AbstractProducer) {
        if (forwardingMethIterceptors != null) {
            ReflectionHelper.set(injectionTarget, AbstractProducer.class, "methodInterceptors", forwardingMethIterceptors);
        }
    }

    LOGGER.debug("New annotated type created for bean '{}'", bean.getBeanClass());
}
 
Example 2
Source File: BeanClassRefreshAgent.java    From HotswapAgent with 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());
        }
    }
}