Search in sources :

Example 1 with ProcessAnnotatedType

use of javax.enterprise.inject.spi.ProcessAnnotatedType 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 ProcessAnnotatedType

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

the class ContainerLifecyleObserverTest method testExtensionBuilder.

@SuppressWarnings({ "serial" })
@Test
public void testExtensionBuilder() {
    ActionSequence.reset();
    Extension myExtension = ContainerLifecycleObserver.extensionBuilder().add(beforeBeanDiscovery((e) -> addAction(BeforeBeanDiscovery.class.getSimpleName()))).add(afterTypeDiscovery().notify((e, b) -> {
        addAction(AfterTypeDiscovery.class.getSimpleName());
        e.addAnnotatedType(b.createAnnotatedType(Charlie.class), Charlie.class.getName());
    })).add(afterBeanDiscovery((e) -> {
        addAction(AfterBeanDiscovery.class.getSimpleName());
        e.addObserverMethod().beanClass(Foo.class).observedType(Foo.class).notifyWith((ctx) -> {
        });
        e.addBean().beanClass(Integer.class).addType(Integer.class).addQualifier(Juicy.Literal.INSTANCE).createWith((ctx) -> Integer.valueOf(10));
    })).add(afterDeploymentValidation((e) -> addAction(AfterDeploymentValidation.class.getSimpleName()))).add(beforeShutdown((e) -> addAction(BeforeShutdown.class.getSimpleName()))).build();
    Extension myExtension2 = ContainerLifecycleObserver.extensionBuilder().add(processAnnotatedType().withAnnotations(RequestScoped.class).notify((e) -> e.veto())).add(processBeanAttributes().notify((e) -> addAction(ProcessBeanAttributes.class.getSimpleName()))).add(processSyntheticAnnotatedType(new TypeLiteral<ProcessSyntheticAnnotatedType<?>>() {
    }.getType()).notify((e) -> addAction(ProcessSyntheticAnnotatedType.class.getSimpleName()))).add(processInjectionPoint().notify((e) -> addAction(ProcessInjectionPoint.class.getSimpleName()))).add(processProducer().notify((e) -> addAction(ProcessProducer.class.getSimpleName()))).add(processBean().notify((e) -> addAction(ProcessBean.class.getSimpleName()))).add(processManagedBean().notify((e) -> addAction(ProcessManagedBean.class.getSimpleName()))).add(processProducerField().notify((e) -> addAction(ProcessProducerField.class.getSimpleName()))).add(processProducerMethod().notify((e) -> {
        // Weld SE defines some producer methods, e.g. ParametersFactory
        addAction(ProcessProducerMethod.class.getSimpleName());
    })).add(processBeanAttributes().notify((e) -> addAction(ProcessBeanAttributes.class.getSimpleName()))).add(processObserverMethod().notify((e) -> addAction(ProcessObserverMethod.class.getSimpleName()))).add(processObserverMethod(new TypeLiteral<ProcessObserverMethod<String, ?>>() {
    }.getType()).notify((e) -> addAction(ProcessObserverMethod.class.getSimpleName() + String.class.getSimpleName()))).add(processSyntheticObserverMethod(new TypeLiteral<ProcessSyntheticObserverMethod<Foo, ?>>() {
    }.getType()).notify((e) -> addAction(ProcessSyntheticObserverMethod.class.getSimpleName() + Foo.class.getSimpleName()))).add(processSyntheticBean(new TypeLiteral<ProcessSyntheticBean<Integer>>() {
    }.getType()).notify((e) -> addAction(ProcessSyntheticBean.class.getSimpleName() + Integer.class.getSimpleName()))).build();
    try (WeldContainer container = new Weld().disableDiscovery().beanClasses(Foo.class, Bravo.class).addExtension(myExtension).addExtension(myExtension2).initialize()) {
        assertTrue(container.select(Foo.class).isUnsatisfied());
        assertFalse(container.select(Bravo.class).isUnsatisfied());
        Assert.assertEquals(Integer.valueOf(10), container.select(Integer.class, Juicy.Literal.INSTANCE).get());
    }
    ActionSequence.assertSequenceDataContainsAll(BeforeBeanDiscovery.class, AfterTypeDiscovery.class, AfterBeanDiscovery.class, AfterDeploymentValidation.class, BeforeShutdown.class);
    ActionSequence.assertSequenceDataContainsAll(ProcessBeanAttributes.class, ProcessSyntheticAnnotatedType.class, ProcessInjectionPoint.class, ProcessObserverMethod.class, ProcessBeanAttributes.class, ProcessProducer.class);
    ActionSequence.assertSequenceDataContainsAll(ProcessObserverMethod.class.getSimpleName() + String.class.getSimpleName(), ProcessSyntheticObserverMethod.class.getSimpleName() + Foo.class.getSimpleName(), ProcessSyntheticBean.class.getSimpleName() + Integer.class.getSimpleName());
    ActionSequence.assertSequenceDataContainsAll(ProcessBean.class, ProcessManagedBean.class, ProcessProducerMethod.class, ProcessProducerField.class);
}
Also used : ContainerLifecycleObserver.processObserverMethod(org.jboss.weld.environment.se.ContainerLifecycleObserver.processObserverMethod) ContainerLifecycleObserver.processSyntheticBean(org.jboss.weld.environment.se.ContainerLifecycleObserver.processSyntheticBean) AfterBeanDiscovery(javax.enterprise.inject.spi.AfterBeanDiscovery) ContainerLifecycleObserver.beforeBeanDiscovery(org.jboss.weld.environment.se.ContainerLifecycleObserver.beforeBeanDiscovery) ContainerLifecycleObserver.afterBeanDiscovery(org.jboss.weld.environment.se.ContainerLifecycleObserver.afterBeanDiscovery) ContainerLifecycleObserver.afterTypeDiscovery(org.jboss.weld.environment.se.ContainerLifecycleObserver.afterTypeDiscovery) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ProcessSyntheticBean(javax.enterprise.inject.spi.ProcessSyntheticBean) ProcessBean(javax.enterprise.inject.spi.ProcessBean) ContainerLifecycleObserver.processProducer(org.jboss.weld.environment.se.ContainerLifecycleObserver.processProducer) ContainerLifecycleObserver.processBean(org.jboss.weld.environment.se.ContainerLifecycleObserver.processBean) ContainerLifecycleObserver.beforeShutdown(org.jboss.weld.environment.se.ContainerLifecycleObserver.beforeShutdown) ProcessProducer(javax.enterprise.inject.spi.ProcessProducer) ProcessProducerField(javax.enterprise.inject.spi.ProcessProducerField) BeforeBeanDiscovery(javax.enterprise.inject.spi.BeforeBeanDiscovery) TypeLiteral(javax.enterprise.util.TypeLiteral) ActionSequence(org.jboss.weld.test.util.ActionSequence) ContainerLifecycleObserver.processInjectionPoint(org.jboss.weld.environment.se.ContainerLifecycleObserver.processInjectionPoint) ProcessObserverMethod(javax.enterprise.inject.spi.ProcessObserverMethod) AfterDeploymentValidation(javax.enterprise.inject.spi.AfterDeploymentValidation) ContainerLifecycleObserver.processSyntheticAnnotatedType(org.jboss.weld.environment.se.ContainerLifecycleObserver.processSyntheticAnnotatedType) ContainerLifecycleObserver.processProducerField(org.jboss.weld.environment.se.ContainerLifecycleObserver.processProducerField) Weld(org.jboss.weld.environment.se.Weld) ProcessInjectionPoint(javax.enterprise.inject.spi.ProcessInjectionPoint) ContainerLifecycleObserver.processSyntheticObserverMethod(org.jboss.weld.environment.se.ContainerLifecycleObserver.processSyntheticObserverMethod) Extension(javax.enterprise.inject.spi.Extension) ProcessManagedBean(javax.enterprise.inject.spi.ProcessManagedBean) ProcessAnnotatedType(javax.enterprise.inject.spi.ProcessAnnotatedType) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) ContainerLifecycleObserver.processManagedBean(org.jboss.weld.environment.se.ContainerLifecycleObserver.processManagedBean) ProcessSyntheticAnnotatedType(javax.enterprise.inject.spi.ProcessSyntheticAnnotatedType) WeldContainer(org.jboss.weld.environment.se.WeldContainer) ProcessProducerMethod(javax.enterprise.inject.spi.ProcessProducerMethod) ContainerLifecycleObserver.afterDeploymentValidation(org.jboss.weld.environment.se.ContainerLifecycleObserver.afterDeploymentValidation) ContainerLifecycleObserver.processProducerMethod(org.jboss.weld.environment.se.ContainerLifecycleObserver.processProducerMethod) AfterTypeDiscovery(javax.enterprise.inject.spi.AfterTypeDiscovery) ContainerLifecycleObserver(org.jboss.weld.environment.se.ContainerLifecycleObserver) Assert.assertFalse(org.junit.Assert.assertFalse) RequestScoped(javax.enterprise.context.RequestScoped) BeforeShutdown(javax.enterprise.inject.spi.BeforeShutdown) ActionSequence.addAction(org.jboss.weld.test.util.ActionSequence.addAction) ProcessBeanAttributes(javax.enterprise.inject.spi.ProcessBeanAttributes) ContainerLifecycleObserver.processAnnotatedType(org.jboss.weld.environment.se.ContainerLifecycleObserver.processAnnotatedType) Assert(org.junit.Assert) ContainerLifecycleObserver.processBeanAttributes(org.jboss.weld.environment.se.ContainerLifecycleObserver.processBeanAttributes) ProcessSyntheticObserverMethod(javax.enterprise.inject.spi.ProcessSyntheticObserverMethod) WeldContainer(org.jboss.weld.environment.se.WeldContainer) AfterDeploymentValidation(javax.enterprise.inject.spi.AfterDeploymentValidation) ProcessObserverMethod(javax.enterprise.inject.spi.ProcessObserverMethod) ProcessSyntheticAnnotatedType(javax.enterprise.inject.spi.ProcessSyntheticAnnotatedType) Weld(org.jboss.weld.environment.se.Weld) Extension(javax.enterprise.inject.spi.Extension) ProcessManagedBean(javax.enterprise.inject.spi.ProcessManagedBean) ProcessSyntheticBean(javax.enterprise.inject.spi.ProcessSyntheticBean) TypeLiteral(javax.enterprise.util.TypeLiteral) ProcessProducer(javax.enterprise.inject.spi.ProcessProducer) AfterBeanDiscovery(javax.enterprise.inject.spi.AfterBeanDiscovery) Test(org.junit.Test)

Example 3 with ProcessAnnotatedType

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

the class ProbeExtensionsTest method testExtensionVisibleInProbe.

@Test
public void testExtensionVisibleInProbe() throws IOException {
    JsonObject beansInTestArchive = getPageAsJSONObject(BEANS_PATH_ALL, url);
    assertNotNull(beansInTestArchive);
    JsonArray beansArray = beansInTestArchive.getJsonArray(DATA);
    ReadContext ctx = JsonPath.parse(beansInTestArchive.toString());
    List<String> beanClasses = ctx.read("$." + DATA + "[*]." + BEAN_CLASS, List.class);
    assertBeanClassVisibleInProbe(TestExtension.class, beanClasses);
    // test extension attributes
    JsonObject extensionBeanDetail = getBeanDetail(BEANS_PATH_ALL, TestExtension.class, url);
    assertEquals(BeanType.EXTENSION.name(), extensionBeanDetail.getString(KIND));
    ctx = JsonPath.parse(extensionBeanDetail.toString());
    List<String> observedTypes = ctx.read("$." + DECLARED_OBSERVERS + "[*]." + OBSERVED_TYPE, List.class);
    assertTrue("Cannot find ProcessAnnotatedType observer method!", observedTypes.contains(ProcessAnnotatedType.class.getName() + "<DummyBean>"));
    // test bean altered by extension
    JsonObject dummyBeanDetail = getBeanDetail(BEANS_PATH_ALL, DummyBean.class, url);
    ctx = JsonPath.parse(dummyBeanDetail.toString());
    List<String> qualifiers = ctx.read("$." + QUALIFIERS + "[*]", List.class);
    assertTrue("Cannot find " + Collector.class + " qualifier on " + DummyBean.class, qualifiers.contains("@" + Collector.class.getName().concat("(value=\"\")")));
}
Also used : JsonArray(javax.json.JsonArray) ProcessAnnotatedType(javax.enterprise.inject.spi.ProcessAnnotatedType) ReadContext(com.jayway.jsonpath.ReadContext) Collector(org.jboss.weld.probe.tests.integration.deployment.annotations.Collector) JsonObject(javax.json.JsonObject) DummyBean(org.jboss.weld.probe.tests.integration.deployment.beans.DummyBean) Test(org.junit.Test)

Example 4 with ProcessAnnotatedType

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

the class FastProcessAnnotatedTypeResolver method process.

private void process(ContainerLifecycleEventObserverMethod<?> observer, Type observedType) throws UnsupportedObserverMethodException {
    if (Object.class.equals(observedType)) {
        // void observe(Object event)
        catchAllObservers.add(observer);
    } else if (ProcessAnnotatedType.class.equals(observedType)) {
        // void observe(ProcessAnnotatedType event)
        catchAllObservers.add(observer);
    } else if (observedType instanceof ParameterizedType) {
        ParameterizedType type = (ParameterizedType) observedType;
        if (ProcessAnnotatedType.class.equals(type.getRawType())) {
            Type typeParameter = type.getActualTypeArguments()[0];
            if (typeParameter instanceof Class<?>) {
                this.observers.put(observer, new ExactTypePredicate(Reflections.getRawType(typeParameter)));
            } else if (typeParameter instanceof ParameterizedType) {
                /*
                     * The event type always has the form of ProcessAnnotatedType<X> where X is a raw type.
                     * Therefore, no event will ever match an observer with type ProcessAnnotatedType<Foo<Y>> no matter
                     * what Y is. This would be because primarily because parameterized are invariant. Event for an exact match
                     * of the raw type, Foo raw event type is not assignable to Foo<?> parameterized type according to CDI assignability rules.
                     */
                return;
            } else if (typeParameter instanceof WildcardType) {
                // void observe(ProcessAnnotatedType<?> event)
                WildcardType wildCard = (WildcardType) typeParameter;
                checkBounds(observer, wildCard.getUpperBounds());
                this.observers.put(observer, CompositePredicate.assignable(Types.getRawTypes(wildCard.getUpperBounds())));
            } else if (typeParameter instanceof TypeVariable<?>) {
                // <T> void observe(ProcessAnnotatedType<T> event)
                TypeVariable<?> variable = (TypeVariable<?>) typeParameter;
                checkBounds(observer, variable.getBounds());
                this.observers.put(observer, CompositePredicate.assignable(Types.getRawTypes(variable.getBounds())));
            }
        }
    } else if (observedType instanceof TypeVariable<?>) {
        defaultRules(observer, observedType);
    }
}
Also used : ProcessAnnotatedType(javax.enterprise.inject.spi.ProcessAnnotatedType) ParameterizedType(java.lang.reflect.ParameterizedType) WildcardType(java.lang.reflect.WildcardType) ProcessAnnotatedType(javax.enterprise.inject.spi.ProcessAnnotatedType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) WildcardType(java.lang.reflect.WildcardType) TypeVariable(java.lang.reflect.TypeVariable)

Example 5 with ProcessAnnotatedType

use of javax.enterprise.inject.spi.ProcessAnnotatedType in project kie-wb-common by kiegroup.

the class ControllerExtensionTest method createAnnotatedType.

protected ProcessAnnotatedType createAnnotatedType(final Class aClass) {
    final ProcessAnnotatedType processAnnotatedType = mock(ProcessAnnotatedType.class);
    final AnnotatedType annotatedType = mock(AnnotatedType.class);
    when(processAnnotatedType.getAnnotatedType()).thenReturn(annotatedType);
    when(annotatedType.getJavaClass()).thenReturn(aClass);
    return processAnnotatedType;
}
Also used : ProcessAnnotatedType(javax.enterprise.inject.spi.ProcessAnnotatedType) ProcessAnnotatedType(javax.enterprise.inject.spi.ProcessAnnotatedType) AnnotatedType(javax.enterprise.inject.spi.AnnotatedType)

Aggregations

ProcessAnnotatedType (javax.enterprise.inject.spi.ProcessAnnotatedType)13 Test (org.junit.Test)8 AnnotatedType (javax.enterprise.inject.spi.AnnotatedType)3 Extension (javax.enterprise.inject.spi.Extension)3 ParameterizedType (java.lang.reflect.ParameterizedType)2 Type (java.lang.reflect.Type)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 RequestScoped (javax.enterprise.context.RequestScoped)2 AfterBeanDiscovery (javax.enterprise.inject.spi.AfterBeanDiscovery)2 AfterDeploymentValidation (javax.enterprise.inject.spi.AfterDeploymentValidation)2 AfterTypeDiscovery (javax.enterprise.inject.spi.AfterTypeDiscovery)2 BeforeBeanDiscovery (javax.enterprise.inject.spi.BeforeBeanDiscovery)2 BeforeShutdown (javax.enterprise.inject.spi.BeforeShutdown)2 ProcessBean (javax.enterprise.inject.spi.ProcessBean)2 ProcessBeanAttributes (javax.enterprise.inject.spi.ProcessBeanAttributes)2 ProcessInjectionPoint (javax.enterprise.inject.spi.ProcessInjectionPoint)2 ProcessManagedBean (javax.enterprise.inject.spi.ProcessManagedBean)2 ProcessObserverMethod (javax.enterprise.inject.spi.ProcessObserverMethod)2