use of io.micronaut.context.processor.AnnotationProcessor 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;
}
Aggregations