Search in sources :

Example 1 with BeanDefinitionProcessor

use of io.micronaut.context.processor.BeanDefinitionProcessor in project micronaut-core by micronaut-projects.

the class AnnotationProcessorListener method onCreated.

@Override
public AnnotationProcessor onCreated(BeanCreatedEvent<AnnotationProcessor> event) {
    AnnotationProcessor processor = event.getBean();
    BeanDefinition<AnnotationProcessor> processorDefinition = event.getBeanDefinition();
    BeanContext beanContext = event.getSource();
    if (processor instanceof LifeCycle) {
        try {
            ((LifeCycle<?>) processor).start();
        } catch (Exception e) {
            throw new BeanContextException("Error starting bean processing: " + e.getMessage(), e);
        }
    }
    if (processor instanceof ExecutableMethodProcessor) {
        List<Argument<?>> typeArguments = processorDefinition.getTypeArguments(ExecutableMethodProcessor.class);
        if (!typeArguments.isEmpty()) {
            final Argument<?> firstArgument = typeArguments.get(0);
            Collection<BeanDefinition<?>> beanDefinitions = beanContext.getBeanDefinitions(Qualifiers.byStereotype((Class) firstArgument.getType()));
            boolean isParallel = firstArgument.isAnnotationPresent(Parallel.class);
            if (isParallel) {
                for (BeanDefinition<?> beanDefinition : beanDefinitions) {
                    Collection<? extends ExecutableMethod<?, ?>> executableMethods = beanDefinition.getExecutableMethods();
                    for (ExecutableMethod<?, ?> executableMethod : executableMethods) {
                        ForkJoinPool.commonPool().execute(() -> {
                            try {
                                if (beanContext.isRunning()) {
                                    processor.process(beanDefinition, executableMethod);
                                }
                            } catch (Throwable e) {
                                if (LOG.isErrorEnabled()) {
                                    LOG.error("Error processing bean method " + beanDefinition + "." + executableMethod + " with processor (" + processor + "): " + e.getMessage(), e);
                                }
                                Boolean shutdownOnError = executableMethod.getAnnotationMetadata().booleanValue(Parallel.class, "shutdownOnError").orElse(true);
                                if (shutdownOnError) {
                                    beanContext.stop();
                                }
                            }
                        });
                    }
                }
            } else {
                for (BeanDefinition<?> beanDefinition : beanDefinitions) {
                    Collection<? extends ExecutableMethod<?, ?>> executableMethods = beanDefinition.getExecutableMethods();
                    for (ExecutableMethod<?, ?> executableMethod : executableMethods) {
                        try {
                            processor.process(beanDefinition, executableMethod);
                        } catch (Exception e) {
                            throw new BeanContextException("Error processing bean [" + beanDefinition + "] method definition [" + executableMethod + "]: " + e.getMessage(), e);
                        }
                    }
                }
            }
        }
    } else if (processor instanceof BeanDefinitionProcessor) {
        BeanDefinitionProcessor beanDefinitionProcessor = (BeanDefinitionProcessor) processor;
        final List<Argument<?>> typeArguments = processorDefinition.getTypeArguments(BeanDefinitionProcessor.class);
        if (typeArguments.size() == 1) {
            final Argument<?> annotation = typeArguments.get(0);
            Collection<BeanDefinition<?>> beanDefinitions = beanContext.getBeanDefinitions(Qualifiers.byStereotype((Class) annotation.getType()));
            for (BeanDefinition<?> beanDefinition : beanDefinitions) {
                try {
                    beanDefinitionProcessor.process(beanDefinition, beanContext);
                } catch (Exception e) {
                    throw new BeanContextException("Error processing bean definition [" + beanDefinition + "]: " + e.getMessage(), e);
                }
            }
        }
    }
    if (processor instanceof LifeCycle) {
        try {
            ((LifeCycle<?>) processor).stop();
        } catch (Exception e) {
            throw new BeanContextException("Error finalizing bean processing: " + e.getMessage(), e);
        }
    }
    return processor;
}
Also used : Argument(io.micronaut.core.type.Argument) AnnotationProcessor(io.micronaut.context.processor.AnnotationProcessor) BeanDefinition(io.micronaut.inject.BeanDefinition) BeanDefinitionProcessor(io.micronaut.context.processor.BeanDefinitionProcessor) BeanContextException(io.micronaut.context.exceptions.BeanContextException) BeanContextException(io.micronaut.context.exceptions.BeanContextException) Collection(java.util.Collection) List(java.util.List) ExecutableMethodProcessor(io.micronaut.context.processor.ExecutableMethodProcessor)

Aggregations

BeanContextException (io.micronaut.context.exceptions.BeanContextException)1 AnnotationProcessor (io.micronaut.context.processor.AnnotationProcessor)1 BeanDefinitionProcessor (io.micronaut.context.processor.BeanDefinitionProcessor)1 ExecutableMethodProcessor (io.micronaut.context.processor.ExecutableMethodProcessor)1 Argument (io.micronaut.core.type.Argument)1 BeanDefinition (io.micronaut.inject.BeanDefinition)1 Collection (java.util.Collection)1 List (java.util.List)1