use of jakarta.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=\"\")")));
}
use of jakarta.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);
}
}
use of jakarta.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);
}
use of jakarta.enterprise.inject.spi.ProcessAnnotatedType in project helidon by oracle.
the class MicronautCdiExtension method processTypes.
/**
* Construct a list of Micronaut interceptors to execute on each CDI method.
* In case a Micronaut bean definition is available for the CDI bean (which should be for application, as
* the CDI annotation processor should be used, and it adds CDI beans as Micronaut beans), the information
* is combined from Micronaut and CDI bean definitions.
*
* @param event CDI event
*/
@SuppressWarnings("unchecked")
void processTypes(@Priority(PLATFORM_AFTER) @Observes ProcessAnnotatedType<?> event) {
Set<Class<?>> classInterceptors = new HashSet<>();
Map<Method, Set<Class<?>>> allMethodInterceptors = new HashMap<>();
List<MicronautBean> miBeans = unprocessedBeans.remove(event.getAnnotatedType().getJavaClass());
if (miBeans != null && miBeans.size() > 0) {
BeanDefinitionReference<?> miBean = findMicronautBeanDefinition(miBeans);
// add all interceptors that are seen based on Micronaut
findMicronautInterceptors(classInterceptors, allMethodInterceptors, miBean);
}
// find all annotations that have meta annotation Around and collect their Type list to add as interceptors
addMicronautInterceptors(classInterceptors, event.getAnnotatedType().getAnnotations());
// for each method, find the same (Around, collect Type), and add the interceptor binding for Micronaut interceptors
// CDI interceptors will be automatic
event.configureAnnotatedType().methods().forEach(method -> {
Method javaMethod = method.getAnnotated().getJavaMember();
Set<Class<?>> methodInterceptors = allMethodInterceptors.computeIfAbsent(javaMethod, it -> new HashSet<>());
methodInterceptors.addAll(classInterceptors);
addMicronautInterceptors(methodInterceptors, method.getAnnotated().getAnnotations());
if (!methodInterceptors.isEmpty()) {
// now I have a set of micronaut interceptors that are needed for this method
method.add(MicronautIntercepted.Literal.INSTANCE);
Set<Class<? extends MethodInterceptor<?, ?>>> interceptors = new HashSet<>();
methodInterceptors.forEach(it -> interceptors.add((Class<? extends MethodInterceptor<?, ?>>) it));
methods.computeIfAbsent(javaMethod, theMethod -> MethodInterceptorMetadata.create(method.getAnnotated(), executableMethodCache.get(theMethod))).addInterceptors(interceptors);
}
});
}
use of jakarta.enterprise.inject.spi.ProcessAnnotatedType in project core by weld.
the class ContainerLifecyleObserverTest method testAddContainerLifecycleObserver.
@SuppressWarnings("serial")
@Test
public void testAddContainerLifecycleObserver() {
final AtomicBoolean called = new AtomicBoolean(false);
try (WeldContainer container = new Weld().disableDiscovery().beanClasses(Foo.class).addContainerLifecycleObserver(processAnnotatedType(new TypeLiteral<ProcessAnnotatedType<Foo>>() {
}.getType()).notify((e) -> e.veto())).addContainerLifecycleObserver(afterBeanDiscovery((e) -> called.set(true))).initialize()) {
assertTrue(called.get());
assertTrue(container.select(Foo.class).isUnsatisfied());
}
}
Aggregations