Search in sources :

Example 1 with ProxyBeanDefinition

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

the class DefaultBeanContext method filterReplacedBeans.

private <T> void filterReplacedBeans(BeanResolutionContext resolutionContext, Collection<? extends BeanType<T>> candidates) {
    if (candidates.size() > 1) {
        List<BeanType<T>> replacementTypes = new ArrayList<>(2);
        for (BeanType<T> candidate : candidates) {
            if (candidate.getAnnotationMetadata().hasStereotype(REPLACES_ANN)) {
                replacementTypes.add(candidate);
            }
        }
        if (!replacementTypes.isEmpty()) {
            candidates.removeIf(definition -> {
                if (!definition.isEnabled(this, resolutionContext)) {
                    return true;
                }
                final AnnotationMetadata annotationMetadata = definition.getAnnotationMetadata();
                if (annotationMetadata.hasDeclaredStereotype(Infrastructure.class)) {
                    return false;
                }
                Optional<Class<?>> declaringType = Optional.empty();
                if (definition instanceof BeanDefinition) {
                    declaringType = ((BeanDefinition<?>) definition).getDeclaringType();
                }
                Function<Class, Boolean> comparisonFunction = null;
                for (BeanType<T> replacingCandidate : replacementTypes) {
                    if (definition == replacingCandidate) {
                        // don't replace yourself
                        continue;
                    }
                    if (replacingCandidate instanceof ProxyBeanDefinition && ((ProxyBeanDefinition<T>) replacingCandidate).getTargetDefinitionType() == definition.getClass()) {
                        continue;
                    }
                    final AnnotationValue<Replaces> replacesAnn = replacingCandidate.getAnnotation(Replaces.class);
                    Class beanType = replacesAnn.classValue().orElse(getCanonicalBeanType(replacingCandidate));
                    if (replacesAnn.contains(NAMED_MEMBER)) {
                        final String qualifier = replacesAnn.stringValue(NAMED_MEMBER).orElse(null);
                        if (qualifier != null) {
                            final Optional qualified = Qualifiers.<T>byName(qualifier).qualify(beanType, Stream.of(definition));
                            if (qualified.isPresent()) {
                                return true;
                            }
                            continue;
                        }
                    }
                    Optional<Class<?>> factory = replacesAnn.classValue("factory");
                    if (LOG.isDebugEnabled()) {
                        if (factory.isPresent()) {
                            LOG.debug("Bean [{}] replaces existing bean of type [{}] in factory type [{}]", replacingCandidate.getBeanType(), beanType, factory.get());
                        } else {
                            LOG.debug("Bean [{}] replaces existing bean of type [{}]", replacingCandidate.getBeanType(), beanType);
                        }
                    }
                    if (comparisonFunction == null) {
                        comparisonFunction = typeMatches(definition, annotationMetadata);
                    }
                    if (factory.isPresent() && declaringType.isPresent()) {
                        if (factory.get() == declaringType.get()) {
                            if (comparisonFunction.apply(beanType)) {
                                return true;
                            }
                        }
                    } else {
                        if (comparisonFunction.apply(beanType)) {
                            return true;
                        }
                    }
                }
                return false;
            });
        }
    }
}
Also used : Optional(java.util.Optional) ArrayList(java.util.ArrayList) Replaces(io.micronaut.context.annotation.Replaces) ProxyBeanDefinition(io.micronaut.inject.ProxyBeanDefinition) BeanDefinition(io.micronaut.inject.BeanDefinition) ValidatedBeanDefinition(io.micronaut.inject.ValidatedBeanDefinition) DisposableBeanDefinition(io.micronaut.inject.DisposableBeanDefinition) InitializingBeanDefinition(io.micronaut.inject.InitializingBeanDefinition) AnnotationMetadata(io.micronaut.core.annotation.AnnotationMetadata) ProxyBeanDefinition(io.micronaut.inject.ProxyBeanDefinition) AdvisedBeanType(io.micronaut.inject.AdvisedBeanType) BeanType(io.micronaut.inject.BeanType) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean)

Example 2 with ProxyBeanDefinition

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

the class DefaultBeanContext method filterProxiedTypes.

private <T> void filterProxiedTypes(Collection<BeanDefinition<T>> candidates, boolean filterProxied, boolean filterDelegates, Predicate<BeanDefinition<T>> predicate) {
    int count = candidates.size();
    Set<Class> proxiedTypes = new HashSet<>(count);
    Iterator<BeanDefinition<T>> i = candidates.iterator();
    Collection<BeanDefinition<T>> delegates = filterDelegates ? new ArrayList<>(count) : Collections.emptyList();
    while (i.hasNext()) {
        BeanDefinition<T> candidate = i.next();
        if (candidate instanceof ProxyBeanDefinition) {
            if (filterProxied) {
                proxiedTypes.add(((ProxyBeanDefinition) candidate).getTargetDefinitionType());
            } else {
                proxiedTypes.add(candidate.getClass());
            }
        } else if (candidate instanceof BeanDefinitionDelegate) {
            BeanDefinition<T> delegate = ((BeanDefinitionDelegate<T>) candidate).getDelegate();
            if (filterDelegates) {
                i.remove();
                if (!delegates.contains(delegate) && (predicate == null || predicate.test(delegate))) {
                    delegates.add(delegate);
                }
            } else if (filterProxied && delegate instanceof ProxyBeanDefinition) {
                proxiedTypes.add(((ProxyBeanDefinition) delegate).getTargetDefinitionType());
            }
        }
    }
    if (filterDelegates) {
        candidates.addAll(delegates);
    }
    if (!proxiedTypes.isEmpty()) {
        candidates.removeIf(candidate -> {
            if (candidate instanceof BeanDefinitionDelegate) {
                return proxiedTypes.contains(((BeanDefinitionDelegate<T>) candidate).getDelegate().getClass());
            } else {
                return proxiedTypes.contains(candidate.getClass());
            }
        });
    }
}
Also used : ProxyBeanDefinition(io.micronaut.inject.ProxyBeanDefinition) ProxyBeanDefinition(io.micronaut.inject.ProxyBeanDefinition) BeanDefinition(io.micronaut.inject.BeanDefinition) ValidatedBeanDefinition(io.micronaut.inject.ValidatedBeanDefinition) DisposableBeanDefinition(io.micronaut.inject.DisposableBeanDefinition) InitializingBeanDefinition(io.micronaut.inject.InitializingBeanDefinition) FieldInjectionPoint(io.micronaut.inject.FieldInjectionPoint) ConstructorInjectionPoint(io.micronaut.inject.ConstructorInjectionPoint) InjectionPoint(io.micronaut.inject.InjectionPoint) MethodInjectionPoint(io.micronaut.inject.MethodInjectionPoint) HashSet(java.util.HashSet)

Aggregations

BeanDefinition (io.micronaut.inject.BeanDefinition)2 DisposableBeanDefinition (io.micronaut.inject.DisposableBeanDefinition)2 InitializingBeanDefinition (io.micronaut.inject.InitializingBeanDefinition)2 ProxyBeanDefinition (io.micronaut.inject.ProxyBeanDefinition)2 ValidatedBeanDefinition (io.micronaut.inject.ValidatedBeanDefinition)2 Replaces (io.micronaut.context.annotation.Replaces)1 AnnotationMetadata (io.micronaut.core.annotation.AnnotationMetadata)1 AdvisedBeanType (io.micronaut.inject.AdvisedBeanType)1 BeanType (io.micronaut.inject.BeanType)1 ConstructorInjectionPoint (io.micronaut.inject.ConstructorInjectionPoint)1 FieldInjectionPoint (io.micronaut.inject.FieldInjectionPoint)1 InjectionPoint (io.micronaut.inject.InjectionPoint)1 MethodInjectionPoint (io.micronaut.inject.MethodInjectionPoint)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Optional (java.util.Optional)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1