use of jakarta.enterprise.inject.spi.configurator.BeanConfigurator 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();
}
Aggregations