use of jakarta.enterprise.event.Observes in project core by weld.
the class ObserverMethodConfiguratorImpl method read.
@Override
public ObserverMethodConfigurator<T> read(Method method) {
checkArgumentNotNull(method);
Set<Parameter> eventParameters = Configurators.getAnnotatedParameters(method, Observes.class, ObservesAsync.class);
checkEventParams(eventParameters, method);
Parameter eventParameter = eventParameters.iterator().next();
Observes observesAnnotation = eventParameter.getAnnotation(Observes.class);
if (observesAnnotation != null) {
reception(observesAnnotation.notifyObserver());
transactionPhase(observesAnnotation.during());
} else {
reception(eventParameter.getAnnotation(ObservesAsync.class).notifyObserver());
}
Priority priority = method.getAnnotation(Priority.class);
if (priority != null) {
priority(priority.value());
}
beanClass(eventParameter.getDeclaringExecutable().getDeclaringClass());
observedType(eventParameter.getType());
qualifiers(Configurators.getQualifiers(eventParameter));
return this;
}
use of jakarta.enterprise.event.Observes in project core by weld.
the class ObserverMethodConfiguratorImpl method read.
@Override
public ObserverMethodConfigurator<T> read(AnnotatedMethod<?> method) {
checkArgumentNotNull(method);
Set<AnnotatedParameter<?>> eventParameters = method.getParameters().stream().filter((p) -> p.isAnnotationPresent(Observes.class) || p.isAnnotationPresent(ObservesAsync.class)).collect(Collectors.toSet());
checkEventParams(eventParameters, method.getJavaMember());
AnnotatedParameter<?> eventParameter = eventParameters.iterator().next();
Observes observesAnnotation = eventParameter.getAnnotation(Observes.class);
if (observesAnnotation != null) {
reception(observesAnnotation.notifyObserver());
transactionPhase(observesAnnotation.during());
async(false);
} else {
reception(eventParameter.getAnnotation(ObservesAsync.class).notifyObserver());
async(true);
}
Priority priority = method.getAnnotation(Priority.class);
if (priority != null) {
priority(priority.value());
}
beanClass(eventParameter.getDeclaringCallable().getDeclaringType().getJavaClass());
observedType(eventParameter.getBaseType());
qualifiers(Configurators.getQualifiers(eventParameter));
return this;
}
use of jakarta.enterprise.event.Observes 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.event.Observes in project helidon by oracle.
the class MicronautCdiExtension method afterBeanDiscovery.
/**
* Add all (not yet added) Micronaut beans for injection as long as they are singletons.
*
* @param event CDI event
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
void afterBeanDiscovery(@Priority(PLATFORM_BEFORE) @Observes AfterBeanDiscovery event) {
event.addBean().addType(ApplicationContext.class).id(MICRONAUT_BEAN_PREFIX + "context").scope(ApplicationScoped.class).produceWith(instance -> micronautContext.get());
// add the remaining Micronaut beans
for (var entry : unprocessedBeans.entrySet()) {
Class<?> beanType = entry.getKey();
List<MicronautBean> beans = entry.getValue();
List<? extends BeanDefinitionReference<?>> refs = List.of();
if (beans.size() > 1) {
// first make sure these are singletons; if not, ignore
refs = beans.stream().map(MicronautBean::definitionRef).filter(it -> !it.getBeanType().getName().endsWith("$Intercepted")).filter(BeanDefinitionReference::isSingleton).collect(Collectors.toList());
}
// primary
event.addBean().addType(beanType).id(MICRONAUT_BEAN_PREFIX + beanType.getName()).scope(Dependent.class).produceWith(instance -> micronautContext.get().getBean(beanType));
if (refs.size() > 1) {
// we must care about qualifiers
for (var ref : refs) {
AnnotationMetadata annotationMetadata = ref.getAnnotationMetadata();
List<Class<? extends Annotation>> qualifiers = annotationMetadata.getAnnotationTypesByStereotype(Qualifier.class);
Annotation[] synthesized = new Annotation[qualifiers.size()];
io.micronaut.context.Qualifier[] mq = new io.micronaut.context.Qualifier[qualifiers.size()];
for (int i = 0; i < qualifiers.size(); i++) {
Annotation annotation = annotationMetadata.synthesize(qualifiers.get(i));
synthesized[i] = annotation;
if (annotation != null) {
mq[i] = Qualifiers.byAnnotation(annotation);
}
}
io.micronaut.context.Qualifier composite = Qualifiers.byQualifiers(mq);
BeanConfigurator<Object> newBean = event.addBean().addType(beanType).id(MICRONAUT_BEAN_PREFIX + ref.getBeanDefinitionName()).scope(Dependent.class).produceWith(instance -> micronautContext.get().getBean(beanType, composite));
for (Annotation annotation : synthesized) {
newBean.addQualifier(annotation);
}
}
}
}
unprocessedBeans.clear();
}
use of jakarta.enterprise.event.Observes in project helidon by oracle.
the class JedisExtension method addBeans.
private void addBeans(@Observes final AfterBeanDiscovery event, final BeanManager beanManager) throws IntrospectionException {
if (event != null && beanManager != null) {
for (final String instanceName : this.instanceNames) {
if (instanceName != null) {
final Set<Annotation> qualifiers;
if (instanceName.equals("default")) {
qualifiers = Collections.singleton(Default.Literal.INSTANCE);
} else {
qualifiers = Collections.singleton(NamedLiteral.of(instanceName));
}
event.<JedisPoolConfig>addBean().addTransitiveTypeClosure(JedisPoolConfig.class).scope(ApplicationScoped.class).addQualifiers(qualifiers).produceWith((instance) -> {
final JedisPoolConfig returnValue = new JedisPoolConfig();
try {
this.configure(instance.select(Config.class).get(), returnValue, JedisPoolConfig.class, instanceName);
} catch (final IntrospectionException | ReflectiveOperationException e) {
throw new CreationException(e.getMessage(), e);
}
return returnValue;
});
final Annotation[] qualifiersArray = qualifiers.toArray(new Annotation[qualifiers.size()]);
event.<JedisPool>addBean().addTransitiveTypeClosure(JedisPool.class).scope(ApplicationScoped.class).addQualifiers(qualifiers).produceWith(instance -> {
return produceJedisPool(instance, instanceName, qualifiersArray);
}).disposeWith((p, instance) -> p.destroy());
event.<Jedis>addBean().addTransitiveTypeClosure(Jedis.class).scope(Dependent.class).addQualifiers(qualifiers).produceWith(instance -> instance.select(JedisPool.class, qualifiersArray).get().getResource()).disposeWith((j, instance) -> j.close());
}
}
}
}
Aggregations