Search in sources :

Example 1 with BeanConfigurator

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();
}
Also used : Dependent(jakarta.enterprise.context.Dependent) Around(io.micronaut.aop.Around) Observes(jakarta.enterprise.event.Observes) ApplicationScoped(jakarta.enterprise.context.ApplicationScoped) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) ExecutableMethod(io.micronaut.inject.ExecutableMethod) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) HashSet(java.util.HashSet) SoftServiceLoader(io.micronaut.core.io.service.SoftServiceLoader) ApplicationContext(io.micronaut.context.ApplicationContext) Extension(jakarta.enterprise.inject.spi.Extension) ProcessAnnotatedType(jakarta.enterprise.inject.spi.ProcessAnnotatedType) BeforeBeanDiscovery(jakarta.enterprise.inject.spi.BeforeBeanDiscovery) MethodInterceptor(io.micronaut.aop.MethodInterceptor) Map(java.util.Map) PLATFORM_AFTER(jakarta.interceptor.Interceptor.Priority.PLATFORM_AFTER) BeanConfigurator(jakarta.enterprise.inject.spi.configurator.BeanConfigurator) Priority(jakarta.annotation.Priority) LinkedList(java.util.LinkedList) Method(java.lang.reflect.Method) Initialized(jakarta.enterprise.context.Initialized) PLATFORM_BEFORE(jakarta.interceptor.Interceptor.Priority.PLATFORM_BEFORE) Iterator(java.util.Iterator) PropertySource(io.micronaut.context.env.PropertySource) Collection(java.util.Collection) ServiceDefinition(io.micronaut.core.io.service.ServiceDefinition) Set(java.util.Set) Qualifiers(io.micronaut.inject.qualifiers.Qualifiers) AdvisedBeanType(io.micronaut.inject.AdvisedBeanType) Config(org.eclipse.microprofile.config.Config) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) BeforeDestroyed(jakarta.enterprise.context.BeforeDestroyed) Objects(java.util.Objects) List(java.util.List) Type(io.micronaut.context.annotation.Type) Stream(java.util.stream.Stream) AfterBeanDiscovery(jakarta.enterprise.inject.spi.AfterBeanDiscovery) Annotation(java.lang.annotation.Annotation) AnnotationMetadata(io.micronaut.core.annotation.AnnotationMetadata) BeanDefinition(io.micronaut.inject.BeanDefinition) Comparator(java.util.Comparator) Qualifier(jakarta.inject.Qualifier) BeanDefinitionReference(io.micronaut.inject.BeanDefinitionReference) Dependent(jakarta.enterprise.context.Dependent) ApplicationScoped(jakarta.enterprise.context.ApplicationScoped) AnnotationMetadata(io.micronaut.core.annotation.AnnotationMetadata) Annotation(java.lang.annotation.Annotation) ApplicationContext(io.micronaut.context.ApplicationContext) BeanDefinitionReference(io.micronaut.inject.BeanDefinitionReference) Qualifier(jakarta.inject.Qualifier)

Aggregations

Around (io.micronaut.aop.Around)1 MethodInterceptor (io.micronaut.aop.MethodInterceptor)1 ApplicationContext (io.micronaut.context.ApplicationContext)1 Type (io.micronaut.context.annotation.Type)1 PropertySource (io.micronaut.context.env.PropertySource)1 AnnotationMetadata (io.micronaut.core.annotation.AnnotationMetadata)1 ServiceDefinition (io.micronaut.core.io.service.ServiceDefinition)1 SoftServiceLoader (io.micronaut.core.io.service.SoftServiceLoader)1 AdvisedBeanType (io.micronaut.inject.AdvisedBeanType)1 BeanDefinition (io.micronaut.inject.BeanDefinition)1 BeanDefinitionReference (io.micronaut.inject.BeanDefinitionReference)1 ExecutableMethod (io.micronaut.inject.ExecutableMethod)1 Qualifiers (io.micronaut.inject.qualifiers.Qualifiers)1 Priority (jakarta.annotation.Priority)1 ApplicationScoped (jakarta.enterprise.context.ApplicationScoped)1 BeforeDestroyed (jakarta.enterprise.context.BeforeDestroyed)1 Dependent (jakarta.enterprise.context.Dependent)1 Initialized (jakarta.enterprise.context.Initialized)1 Observes (jakarta.enterprise.event.Observes)1 AfterBeanDiscovery (jakarta.enterprise.inject.spi.AfterBeanDiscovery)1