Search in sources :

Example 1 with AnnotatedElementFactory

use of org.apache.webbeans.portable.AnnotatedElementFactory in project HotswapAgent by HotswapProjects.

the class BeanClassRefreshAgent method doDefineNewBean.

private static void doDefineNewBean(BeanManagerImpl beanManager, Class<?> beanClass) {
    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();
    HashMap<AnnotatedType<?>, ExtendedBeanAttributes<?>> annotatedTypes = new HashMap<>();
    BeansDeployer beansDeployer = new BeansDeployer(wbc);
    try {
        ReflectionHelper.invoke(beansDeployer, BeansDeployer.class, "defineManagedBean", new Class[] { javax.enterprise.inject.spi.AnnotatedType.class, javax.enterprise.inject.spi.BeanAttributes.class, java.util.Map.class }, annotatedType, attributes, annotatedTypes);
    } catch (Exception e) {
        LOGGER.error("Bean '{}' definition failed", beanClass.getName(), e);
    }
}
Also used : HashMap(java.util.HashMap) ExtendedBeanAttributes(org.apache.webbeans.config.BeansDeployer.ExtendedBeanAttributes) BeansDeployer(org.apache.webbeans.config.BeansDeployer) ContextNotActiveException(javax.enterprise.context.ContextNotActiveException) IOException(java.io.IOException) AnnotatedType(javax.enterprise.inject.spi.AnnotatedType) WebBeansContext(org.apache.webbeans.config.WebBeansContext) AnnotatedElementFactory(org.apache.webbeans.portable.AnnotatedElementFactory)

Example 2 with AnnotatedElementFactory

use of org.apache.webbeans.portable.AnnotatedElementFactory in project HotswapAgent by HotswapProjects.

the class BeanClassRefreshAgent method createAnnotatedTypeForExistingBeanClass.

@SuppressWarnings({ "rawtypes", "unchecked" })
private static void createAnnotatedTypeForExistingBeanClass(BeanManagerImpl beanManager, Class<?> beanClass, InjectionTargetBean bean) {
    WebBeansContext wbc = beanManager.getWebBeansContext();
    AnnotatedElementFactory annotatedElementFactory = wbc.getAnnotatedElementFactory();
    // Clear AnnotatedElementFactory caches
    annotatedElementFactory.clear();
    AnnotatedType annotatedType = annotatedElementFactory.newAnnotatedType(beanClass);
    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);
    LOGGER.debug("New annotated type created for bean '{}'", beanClass.getName());
}
Also used : AnnotatedType(javax.enterprise.inject.spi.AnnotatedType) WebBeansContext(org.apache.webbeans.config.WebBeansContext) BeanAttributesImpl(org.apache.webbeans.component.BeanAttributesImpl) InjectionTargetFactory(javax.enterprise.inject.spi.InjectionTargetFactory) InjectionTarget(javax.enterprise.inject.spi.InjectionTarget) AnnotatedElementFactory(org.apache.webbeans.portable.AnnotatedElementFactory) InjectionTargetFactoryImpl(org.apache.webbeans.container.InjectionTargetFactoryImpl)

Aggregations

AnnotatedType (javax.enterprise.inject.spi.AnnotatedType)2 WebBeansContext (org.apache.webbeans.config.WebBeansContext)2 AnnotatedElementFactory (org.apache.webbeans.portable.AnnotatedElementFactory)2 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 ContextNotActiveException (javax.enterprise.context.ContextNotActiveException)1 InjectionTarget (javax.enterprise.inject.spi.InjectionTarget)1 InjectionTargetFactory (javax.enterprise.inject.spi.InjectionTargetFactory)1 BeanAttributesImpl (org.apache.webbeans.component.BeanAttributesImpl)1 BeansDeployer (org.apache.webbeans.config.BeansDeployer)1 ExtendedBeanAttributes (org.apache.webbeans.config.BeansDeployer.ExtendedBeanAttributes)1 InjectionTargetFactoryImpl (org.apache.webbeans.container.InjectionTargetFactoryImpl)1