use of io.micronaut.inject.BeanDefinitionReference in project micronaut-core by micronaut-projects.
the class DefaultBeanContext method getBeanDefinitions.
@SuppressWarnings("unchecked")
@Override
@NonNull
public Collection<BeanDefinition<?>> getBeanDefinitions(@Nullable Qualifier<Object> qualifier) {
if (qualifier == null) {
return Collections.emptyList();
}
if (LOG.isDebugEnabled()) {
LOG.debug("Finding candidate beans for qualifier: {}", qualifier);
}
// first traverse component definition classes and load candidates
Collection candidates;
if (!beanDefinitionsClasses.isEmpty()) {
Stream<BeanDefinitionReference> reduced = qualifier.reduce(Object.class, beanDefinitionsClasses.stream());
Stream<BeanDefinition> candidateStream = qualifier.reduce(Object.class, reduced.map(ref -> ref.load(this)).filter(candidate -> candidate.isEnabled(this)));
candidates = candidateStream.collect(Collectors.toList());
} else {
return Collections.emptyList();
}
if (CollectionUtils.isNotEmpty(candidates)) {
filterProxiedTypes(candidates, true, true, null);
filterReplacedBeans(null, candidates);
}
return candidates;
}
use of io.micronaut.inject.BeanDefinitionReference in project micronaut-core by micronaut-projects.
the class DefaultBeanContext method findBeanCandidates.
/**
* Find bean candidates for the given type.
*
* @param <T> The bean generic type
* @param resolutionContext The current resolution context
* @param beanType The bean type
* @param filterProxied Whether to filter out bean proxy targets
* @param predicate The predicate to filter candidates
* @return The candidates
*/
@SuppressWarnings("unchecked")
@NonNull
protected <T> Collection<BeanDefinition<T>> findBeanCandidates(@Nullable BeanResolutionContext resolutionContext, @NonNull Argument<T> beanType, boolean filterProxied, Predicate<BeanDefinition<T>> predicate) {
ArgumentUtils.requireNonNull("beanType", beanType);
final Class<T> beanClass = beanType.getType();
if (LOG.isDebugEnabled()) {
LOG.debug("Finding candidate beans for type: {}", beanType);
}
// first traverse component definition classes and load candidates
Collection<BeanDefinitionReference> beanDefinitionsClasses;
if (indexedTypes.contains(beanClass)) {
beanDefinitionsClasses = beanIndex.get(beanClass);
if (beanDefinitionsClasses == null) {
beanDefinitionsClasses = Collections.emptyList();
}
} else {
beanDefinitionsClasses = this.beanDefinitionsClasses;
}
if (!beanDefinitionsClasses.isEmpty()) {
Set<BeanDefinition<T>> candidates = new HashSet<>();
for (BeanDefinitionReference reference : beanDefinitionsClasses) {
if (!reference.isCandidateBean(beanType) || !reference.isEnabled(this, resolutionContext)) {
continue;
}
BeanDefinition<T> loadedBean;
try {
loadedBean = reference.load(this);
} catch (Throwable e) {
throw new BeanContextException("Error loading bean [" + reference.getName() + "]: " + e.getMessage(), e);
}
if (!loadedBean.isCandidateBean(beanType)) {
continue;
}
if (predicate != null && !predicate.test(loadedBean)) {
continue;
}
if (!loadedBean.isEnabled(this, resolutionContext)) {
continue;
}
candidates.add(loadedBean);
}
if (!candidates.isEmpty()) {
if (filterProxied) {
filterProxiedTypes(candidates, true, false, null);
}
filterReplacedBeans(resolutionContext, candidates);
}
if (LOG.isDebugEnabled()) {
LOG.debug("Resolved bean candidates {} for type: {}", candidates, beanType);
}
return candidates;
} else {
if (LOG.isDebugEnabled()) {
LOG.debug("No bean candidates found for type: {}", beanType);
}
return Collections.emptySet();
}
}
use of io.micronaut.inject.BeanDefinitionReference in project micronaut-core by micronaut-projects.
the class RequiresCondition method matches.
@Override
public boolean matches(ConditionContext context) {
AnnotationMetadataProvider component = context.getComponent();
boolean isBeanReference = component instanceof BeanDefinitionReference;
List<AnnotationValue<Requires>> requirements = annotationMetadata.getAnnotationValuesByType(Requires.class);
if (!requirements.isEmpty()) {
// here we use AnnotationMetadata to avoid loading the classes referenced in the annotations directly
if (isBeanReference) {
for (AnnotationValue<Requires> requirement : requirements) {
processPreStartRequirements(context, requirement);
if (context.isFailing()) {
return false;
}
}
} else {
for (AnnotationValue<Requires> requires : requirements) {
processPostStartRequirements(context, requires);
if (context.isFailing()) {
return false;
}
}
}
}
return true;
}
use of io.micronaut.inject.BeanDefinitionReference in project helidon by oracle.
the class MicronautCdiExtension method processTypes.
/**
* Construct a list of Micronaut interceptors to execute on each CDI method.
* In case a Micronaut bean definition is available for the CDI bean (which should be for application, as
* the CDI annotation processor should be used, and it adds CDI beans as Micronaut beans), the information
* is combined from Micronaut and CDI bean definitions.
*
* @param event CDI event
*/
@SuppressWarnings("unchecked")
void processTypes(@Priority(PLATFORM_AFTER) @Observes ProcessAnnotatedType<?> event) {
Set<Class<?>> classInterceptors = new HashSet<>();
Map<Method, Set<Class<?>>> allMethodInterceptors = new HashMap<>();
List<MicronautBean> miBeans = unprocessedBeans.remove(event.getAnnotatedType().getJavaClass());
if (miBeans != null && miBeans.size() > 0) {
BeanDefinitionReference<?> miBean = findMicronautBeanDefinition(miBeans);
// add all interceptors that are seen based on Micronaut
findMicronautInterceptors(classInterceptors, allMethodInterceptors, miBean);
}
// find all annotations that have meta annotation Around and collect their Type list to add as interceptors
addMicronautInterceptors(classInterceptors, event.getAnnotatedType().getAnnotations());
// for each method, find the same (Around, collect Type), and add the interceptor binding for Micronaut interceptors
// CDI interceptors will be automatic
event.configureAnnotatedType().methods().forEach(method -> {
Method javaMethod = method.getAnnotated().getJavaMember();
Set<Class<?>> methodInterceptors = allMethodInterceptors.computeIfAbsent(javaMethod, it -> new HashSet<>());
methodInterceptors.addAll(classInterceptors);
addMicronautInterceptors(methodInterceptors, method.getAnnotated().getAnnotations());
if (!methodInterceptors.isEmpty()) {
// now I have a set of micronaut interceptors that are needed for this method
method.add(MicronautIntercepted.Literal.INSTANCE);
Set<Class<? extends MethodInterceptor<?, ?>>> interceptors = new HashSet<>();
methodInterceptors.forEach(it -> interceptors.add((Class<? extends MethodInterceptor<?, ?>>) it));
methods.computeIfAbsent(javaMethod, theMethod -> MethodInterceptorMetadata.create(method.getAnnotated(), executableMethodCache.get(theMethod))).addInterceptors(interceptors);
}
});
}
use of io.micronaut.inject.BeanDefinitionReference 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();
}
Aggregations