Search in sources :

Example 1 with AnnotatedType

use of javax.enterprise.inject.spi.AnnotatedType in project jersey by jersey.

the class CdiComponentProvider method processAnnotatedType.

@SuppressWarnings("unused")
private void processAnnotatedType(@Observes final //                                              PathParam.class})
ProcessAnnotatedType processAnnotatedType) {
    final AnnotatedType<?> annotatedType = processAnnotatedType.getAnnotatedType();
    // if one of the JAX-RS annotations is present in the currently seen class, add it to the "whitelist"
    if (containsJaxRsConstructorInjection(annotatedType) || containsJaxRsFieldInjection(annotatedType) || containsJaxRsMethodInjection(annotatedType)) {
        jaxrsInjectableTypes.add(annotatedType.getBaseType());
    }
    if (customHk2TypesProvider != null) {
        final Type baseType = annotatedType.getBaseType();
        if (customHk2TypesProvider.getHk2Types().contains(baseType)) {
            processAnnotatedType.veto();
            jerseyVetoedTypes.add(baseType);
        }
    }
    if (containsJaxRsParameterizedCtor(annotatedType)) {
        processAnnotatedType.setAnnotatedType(new AnnotatedType() {

            @Override
            public Class getJavaClass() {
                return annotatedType.getJavaClass();
            }

            @Override
            public Set<AnnotatedConstructor> getConstructors() {
                final Set<AnnotatedConstructor> result = new HashSet<>();
                for (final AnnotatedConstructor c : annotatedType.getConstructors()) {
                    result.add(enrichedConstructor(c));
                }
                return result;
            }

            @Override
            public Set getMethods() {
                return annotatedType.getMethods();
            }

            @Override
            public Set getFields() {
                return annotatedType.getFields();
            }

            @Override
            public Type getBaseType() {
                return annotatedType.getBaseType();
            }

            @Override
            public Set<Type> getTypeClosure() {
                return annotatedType.getTypeClosure();
            }

            @Override
            public <T extends Annotation> T getAnnotation(final Class<T> annotationType) {
                return annotatedType.getAnnotation(annotationType);
            }

            @Override
            public Set<Annotation> getAnnotations() {
                return annotatedType.getAnnotations();
            }

            @Override
            public boolean isAnnotationPresent(final Class<? extends Annotation> annotationType) {
                return annotatedType.isAnnotationPresent(annotationType);
            }
        });
    }
}
Also used : ProcessAnnotatedType(javax.enterprise.inject.spi.ProcessAnnotatedType) Type(java.lang.reflect.Type) AnnotatedType(javax.enterprise.inject.spi.AnnotatedType) ParameterizedType(java.lang.reflect.ParameterizedType) ProcessAnnotatedType(javax.enterprise.inject.spi.ProcessAnnotatedType) AnnotatedType(javax.enterprise.inject.spi.AnnotatedType) Set(java.util.Set) HashSet(java.util.HashSet) AnnotatedConstructor(javax.enterprise.inject.spi.AnnotatedConstructor)

Example 2 with AnnotatedType

use of javax.enterprise.inject.spi.AnnotatedType in project core by weld.

the class GlueDecoratorExtension method registerDecorator.

void registerDecorator(@Observes AfterBeanDiscovery event, BeanManager manager) {
    AnnotatedType<GlueDecorator> annotatedType = manager.createAnnotatedType(GlueDecorator.class);
    final BeanAttributes<GlueDecorator> attributes = manager.createBeanAttributes(annotatedType);
    final InjectionPoint delegateInjectionPoint = manager.createInjectionPoint(annotatedType.getConstructors().iterator().next().getParameters().get(0));
    Decorator<GlueDecorator> decorator = new DecoratorImpl<GlueDecorator>() {

        @Override
        public Type getDelegateType() {
            return Glue.class;
        }

        @Override
        public Set<Annotation> getDelegateQualifiers() {
            return Collections.<Annotation>singleton(Any.Literal.INSTANCE);
        }

        @Override
        public Set<Type> getDecoratedTypes() {
            return Collections.emptySet();
        }

        @Override
        public Class<?> getBeanClass() {
            return GlueDecorator.class;
        }

        @Override
        public Set<InjectionPoint> getInjectionPoints() {
            final Decorator<GlueDecorator> decorator = this;
            InjectionPoint wrappedInjectionPoint = new ForwardingInjectionPoint() {

                @Override
                public Bean<?> getBean() {
                    return decorator;
                }

                @Override
                protected InjectionPoint delegate() {
                    return delegateInjectionPoint;
                }
            };
            return Collections.singleton(wrappedInjectionPoint);
        }

        @Override
        public GlueDecorator create(CreationalContext<GlueDecorator> creationalContext) {
            return new GlueDecorator(null);
        }

        @Override
        public void destroy(GlueDecorator instance, CreationalContext<GlueDecorator> creationalContext) {
            creationalContext.release();
        }

        @Override
        protected BeanAttributes<GlueDecorator> attributes() {
            return attributes;
        }
    };
    event.addBean(decorator);
}
Also used : CreationalContext(javax.enterprise.context.spi.CreationalContext) AnnotatedType(javax.enterprise.inject.spi.AnnotatedType) Type(java.lang.reflect.Type) ForwardingInjectionPoint(org.jboss.weld.injection.ForwardingInjectionPoint) InjectionPoint(javax.enterprise.inject.spi.InjectionPoint) ForwardingInjectionPoint(org.jboss.weld.injection.ForwardingInjectionPoint) Annotation(java.lang.annotation.Annotation)

Example 3 with AnnotatedType

use of javax.enterprise.inject.spi.AnnotatedType 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 4 with AnnotatedType

use of javax.enterprise.inject.spi.AnnotatedType in project dolphin-platform by canoo.

the class CdiManagedBeanFactory method createDependentInstance.

@Override
public <T> T createDependentInstance(Class<T> cls) {
    Assert.requireNonNull(cls, "cls");
    BeanManager bm = BeanManagerProvider.getInstance().getBeanManager();
    AnnotatedType annotatedType = bm.createAnnotatedType(cls);
    final InjectionTarget<T> injectionTarget = bm.createInjectionTarget(annotatedType);
    final Bean<T> bean = new BeanBuilder<T>(bm).beanClass(cls).name(UUID.randomUUID().toString()).scope(Dependent.class).beanLifecycle(new DelegatingContextualLifecycle<T>(injectionTarget)).create();
    Class<?> beanClass = bean.getBeanClass();
    CreationalContext<T> creationalContext = bm.createCreationalContext(bean);
    T instance = (T) bm.getReference(bean, beanClass, creationalContext);
    contextMap.put(instance, creationalContext);
    beanMap.put(instance, bean);
    return instance;
}
Also used : DelegatingContextualLifecycle(org.apache.deltaspike.core.util.metadata.builder.DelegatingContextualLifecycle) AnnotatedType(javax.enterprise.inject.spi.AnnotatedType) BeanManager(javax.enterprise.inject.spi.BeanManager)

Example 5 with AnnotatedType

use of javax.enterprise.inject.spi.AnnotatedType in project wildfly-swarm by wildfly-swarm.

the class HealthExtension method afterDeploymentValidation.

/**
 * Instantiates <em>unmanaged instances</em> of HealthCheckProcedure and
 * handle manually their CDI creation lifecycle.
 * Add them to the {@link Monitor}.
 */
private void afterDeploymentValidation(@Observes final AfterDeploymentValidation abd, BeanManager beanManager) {
    try {
        for (AnnotatedType delegate : delegates) {
            Unmanaged<HealthCheck> unmanagedHealthCheck = new Unmanaged<HealthCheck>(beanManager, delegate.getJavaClass());
            Unmanaged.UnmanagedInstance<HealthCheck> healthCheckInstance = unmanagedHealthCheck.newInstance();
            HealthCheck healthCheck = healthCheckInstance.produce().inject().postConstruct().get();
            healthChecks.add(healthCheck);
            healthCheckInstances.add(healthCheckInstance);
            monitor.registerHealthBean(healthCheck);
            log.info(">> Added health bean impl " + healthCheck);
        }
        // we don't need the references anymore
        delegates.clear();
    } catch (Exception e) {
        throw new RuntimeException("Failed to register health bean", e);
    }
}
Also used : ProcessAnnotatedType(javax.enterprise.inject.spi.ProcessAnnotatedType) AnnotatedType(javax.enterprise.inject.spi.AnnotatedType) Unmanaged(javax.enterprise.inject.spi.Unmanaged) HealthCheck(org.eclipse.microprofile.health.HealthCheck) NamingException(javax.naming.NamingException)

Aggregations

AnnotatedType (javax.enterprise.inject.spi.AnnotatedType)24 AnnotatedMethod (javax.enterprise.inject.spi.AnnotatedMethod)7 Annotation (java.lang.annotation.Annotation)5 Method (java.lang.reflect.Method)5 Type (java.lang.reflect.Type)5 HashSet (java.util.HashSet)4 Set (java.util.Set)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 ProcessAnnotatedType (javax.enterprise.inject.spi.ProcessAnnotatedType)4 Test (org.junit.Test)4 InjectionTarget (javax.enterprise.inject.spi.InjectionTarget)3 EnhancedAnnotatedType (org.jboss.weld.annotated.enhanced.EnhancedAnnotatedType)3 ForwardingAnnotatedType (org.jboss.weld.util.annotated.ForwardingAnnotatedType)3 Field (java.lang.reflect.Field)2 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Logger (java.util.logging.Logger)2 CreationalContext (javax.enterprise.context.spi.CreationalContext)2 Alternative (javax.enterprise.inject.Alternative)2