Search in sources :

Example 1 with BeanConfiguration

use of io.micronaut.inject.BeanConfiguration in project micronaut-core by micronaut-projects.

the class AbstractBeanContextConditional method isEnabled.

@Override
public boolean isEnabled(@NonNull BeanContext context, @Nullable BeanResolutionContext resolutionContext) {
    AnnotationMetadata annotationMetadata = getAnnotationMetadata();
    Condition condition = annotationMetadata.hasStereotype(Requires.class) ? new RequiresCondition(annotationMetadata) : null;
    DefaultConditionContext<AbstractBeanContextConditional> conditionContext = new DefaultConditionContext<>((DefaultBeanContext) context, this, resolutionContext);
    boolean enabled = condition == null || condition.matches(conditionContext);
    if (LOG.isDebugEnabled() && !enabled) {
        if (this instanceof BeanConfiguration) {
            LOG.debug(this + " will not be loaded due to failing conditions:");
        } else {
            LOG.debug("Bean [" + this + "] will not be loaded due to failing conditions:");
        }
        for (Failure failure : conditionContext.getFailures()) {
            LOG.debug("* {}", failure.getMessage());
        }
    }
    return enabled;
}
Also used : Condition(io.micronaut.context.condition.Condition) Requires(io.micronaut.context.annotation.Requires) BeanConfiguration(io.micronaut.inject.BeanConfiguration) AnnotationMetadata(io.micronaut.core.annotation.AnnotationMetadata) Failure(io.micronaut.context.condition.Failure)

Example 2 with BeanConfiguration

use of io.micronaut.inject.BeanConfiguration in project micronaut-core by micronaut-projects.

the class DefaultBeanContext method readAllBeanDefinitionClasses.

private void readAllBeanDefinitionClasses() {
    List<BeanDefinitionReference> contextScopeBeans = new ArrayList<>(20);
    List<BeanDefinitionReference> processedBeans = new ArrayList<>(10);
    List<BeanDefinitionReference> parallelBeans = new ArrayList<>(10);
    List<BeanDefinitionReference> beanDefinitionReferences = resolveBeanDefinitionReferences();
    beanDefinitionsClasses.addAll(beanDefinitionReferences);
    Set<BeanConfiguration> configurationsDisabled = new HashSet<>();
    for (BeanConfiguration bc : beanConfigurations.values()) {
        if (!bc.isEnabled(this)) {
            configurationsDisabled.add(bc);
        }
    }
    reference: for (BeanDefinitionReference beanDefinitionReference : beanDefinitionReferences) {
        for (BeanConfiguration disableConfiguration : configurationsDisabled) {
            if (disableConfiguration.isWithin(beanDefinitionReference)) {
                beanDefinitionsClasses.remove(beanDefinitionReference);
                continue reference;
            }
        }
        final AnnotationMetadata annotationMetadata = beanDefinitionReference.getAnnotationMetadata();
        Class[] indexes = annotationMetadata.classValues(INDEXES_TYPE);
        if (indexes.length > 0) {
            // noinspection ForLoopReplaceableByForEach
            for (int i = 0; i < indexes.length; i++) {
                Class indexedType = indexes[i];
                resolveTypeIndex(indexedType).add(beanDefinitionReference);
            }
        } else {
            if (annotationMetadata.hasStereotype(ADAPTER_TYPE)) {
                final Class aClass = annotationMetadata.classValue(ADAPTER_TYPE, AnnotationMetadata.VALUE_MEMBER).orElse(null);
                if (indexedTypes.contains(aClass)) {
                    resolveTypeIndex(aClass).add(beanDefinitionReference);
                }
            }
        }
        if (isEagerInit(beanDefinitionReference)) {
            contextScopeBeans.add(beanDefinitionReference);
        } else if (annotationMetadata.hasDeclaredStereotype(PARALLEL_TYPE)) {
            parallelBeans.add(beanDefinitionReference);
        }
        if (beanDefinitionReference.requiresMethodProcessing()) {
            processedBeans.add(beanDefinitionReference);
        }
    }
    beanDefinitionReferences = null;
    beanConfigurationsList = null;
    initializeEventListeners();
    initializeContext(contextScopeBeans, processedBeans, parallelBeans);
}
Also used : BeanDefinitionReference(io.micronaut.inject.BeanDefinitionReference) ArrayList(java.util.ArrayList) BeanConfiguration(io.micronaut.inject.BeanConfiguration) AnnotationMetadata(io.micronaut.core.annotation.AnnotationMetadata) HashSet(java.util.HashSet)

Aggregations

AnnotationMetadata (io.micronaut.core.annotation.AnnotationMetadata)2 BeanConfiguration (io.micronaut.inject.BeanConfiguration)2 Requires (io.micronaut.context.annotation.Requires)1 Condition (io.micronaut.context.condition.Condition)1 Failure (io.micronaut.context.condition.Failure)1 BeanDefinitionReference (io.micronaut.inject.BeanDefinitionReference)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1