Skip to content

Commit

Permalink
Introduce findAllAnnotationsOnBean variant on ListableBeanFactory
Browse files Browse the repository at this point in the history
Closes gh-29446
  • Loading branch information
jhoeller committed Nov 8, 2022
1 parent e1010a1 commit 86d4557
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 80 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,6 +18,7 @@

import java.lang.annotation.Annotation;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.BeansException;
import org.springframework.core.ResolvableType;
Expand Down Expand Up @@ -338,6 +339,8 @@ <T> Map<String, T> getBeansOfType(@Nullable Class<T> type, boolean includeNonSin
* @throws BeansException if a bean could not be created
* @since 3.0
* @see #findAnnotationOnBean
* @see #findAllAnnotationsOnBean(String, Class, boolean)
* @see #findAllAnnotationsOnBean(String, Class, boolean)
*/
Map<String, Object> getBeansWithAnnotation(Class<? extends Annotation> annotationType) throws BeansException;

Expand Down Expand Up @@ -380,4 +383,24 @@ <A extends Annotation> A findAnnotationOnBean(
String beanName, Class<A> annotationType, boolean allowFactoryBeanInit)
throws NoSuchBeanDefinitionException;

/**
* Find all {@link Annotation} instances of {@code annotationType} on the specified
* bean, traversing its interfaces and superclasses if no annotation can be found on
* the given class itself, as well as checking the bean's factory method (if any).
* @param beanName the name of the bean to look for annotations on
* @param annotationType the type of annotation to look for
* (at class, interface or factory method level of the specified bean)
* @param allowFactoryBeanInit whether a {@code FactoryBean} may get initialized
* just for the purpose of determining its object type
* @return the set of annotations of the given type found (potentially empty)
* @throws NoSuchBeanDefinitionException if there is no bean with the given name
* @since 6.0
* @see #getBeanNamesForAnnotation
* @see #findAnnotationOnBean(String, Class, boolean)
* @see #getType(String, boolean)
*/
<A extends Annotation> Set<A> findAllAnnotationsOnBean(
String beanName, Class<A> annotationType, boolean allowFactoryBeanInit)
throws NoSuchBeanDefinitionException;

}
Expand Up @@ -729,19 +729,12 @@ public <A extends Annotation> A findAnnotationOnBean(
String beanName, Class<A> annotationType, boolean allowFactoryBeanInit)
throws NoSuchBeanDefinitionException {

return findMergedAnnotationOnBean(beanName, annotationType, allowFactoryBeanInit)
.synthesize(MergedAnnotation::isPresent).orElse(null);
}

private <A extends Annotation> MergedAnnotation<A> findMergedAnnotationOnBean(
String beanName, Class<A> annotationType, boolean allowFactoryBeanInit) {

Class<?> beanType = getType(beanName, allowFactoryBeanInit);
if (beanType != null) {
MergedAnnotation<A> annotation =
MergedAnnotations.from(beanType, SearchStrategy.TYPE_HIERARCHY).get(annotationType);
if (annotation.isPresent()) {
return annotation;
return annotation.synthesize();
}
}
if (containsBeanDefinition(beanName)) {
Expand All @@ -753,7 +746,7 @@ private <A extends Annotation> MergedAnnotation<A> findMergedAnnotationOnBean(
MergedAnnotation<A> annotation =
MergedAnnotations.from(beanClass, SearchStrategy.TYPE_HIERARCHY).get(annotationType);
if (annotation.isPresent()) {
return annotation;
return annotation.synthesize();
}
}
}
Expand All @@ -763,11 +756,48 @@ private <A extends Annotation> MergedAnnotation<A> findMergedAnnotationOnBean(
MergedAnnotation<A> annotation =
MergedAnnotations.from(factoryMethod, SearchStrategy.TYPE_HIERARCHY).get(annotationType);
if (annotation.isPresent()) {
return annotation;
return annotation.synthesize();
}
}
}
return null;
}

@Override
public <A extends Annotation> Set<A> findAllAnnotationsOnBean(
String beanName, Class<A> annotationType, boolean allowFactoryBeanInit)
throws NoSuchBeanDefinitionException {

Set<A> annotations = new LinkedHashSet<>();
Class<?> beanType = getType(beanName, allowFactoryBeanInit);
if (beanType != null) {
MergedAnnotations.from(beanType, MergedAnnotations.SearchStrategy.TYPE_HIERARCHY)
.stream(annotationType)
.filter(MergedAnnotation::isPresent)
.forEach(mergedAnnotation -> annotations.add(mergedAnnotation.synthesize()));
}
if (containsBeanDefinition(beanName)) {
RootBeanDefinition bd = getMergedLocalBeanDefinition(beanName);
// Check raw bean class, e.g. in case of a proxy.
if (bd.hasBeanClass() && bd.getFactoryMethodName() == null) {
Class<?> beanClass = bd.getBeanClass();
if (beanClass != beanType) {
MergedAnnotations.from(beanClass, MergedAnnotations.SearchStrategy.TYPE_HIERARCHY)
.stream(annotationType)
.filter(MergedAnnotation::isPresent)
.forEach(mergedAnnotation -> annotations.add(mergedAnnotation.synthesize()));
}
}
// Check annotations declared on factory method, if any.
Method factoryMethod = bd.getResolvedFactoryMethod();
if (factoryMethod != null) {
MergedAnnotations.from(factoryMethod, MergedAnnotations.SearchStrategy.TYPE_HIERARCHY)
.stream(annotationType)
.filter(MergedAnnotation::isPresent)
.forEach(mergedAnnotation -> annotations.add(mergedAnnotation.synthesize()));
}
}
return MergedAnnotation.missing();
return annotations;
}


Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,9 +19,11 @@
import java.lang.annotation.Annotation;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Stream;

import org.springframework.beans.BeansException;
Expand Down Expand Up @@ -472,4 +474,13 @@ public <A extends Annotation> A findAnnotationOnBean(
return (beanType != null ? AnnotatedElementUtils.findMergedAnnotation(beanType, annotationType) : null);
}

@Override
public <A extends Annotation> Set<A> findAllAnnotationsOnBean(
String beanName, Class<A> annotationType, boolean allowFactoryBeanInit) throws NoSuchBeanDefinitionException {

Class<?> beanType = getType(beanName, allowFactoryBeanInit);
return (beanType != null ?
AnnotatedElementUtils.findAllMergedAnnotations(beanType, annotationType) : Collections.emptySet());
}

}
Expand Up @@ -61,7 +61,7 @@
* @since 6.0
* @see org.springframework.aot.hint.RuntimeHints
*/
@Target({ ElementType.TYPE, ElementType.METHOD })
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface ImportRuntimeHints {
Expand Down
Expand Up @@ -16,12 +16,8 @@

package org.springframework.context.aot;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

Expand All @@ -36,12 +32,8 @@
import org.springframework.beans.factory.aot.BeanFactoryInitializationAotContribution;
import org.springframework.beans.factory.aot.BeanFactoryInitializationAotProcessor;
import org.springframework.beans.factory.aot.BeanFactoryInitializationCode;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.annotation.ImportRuntimeHints;
import org.springframework.core.annotation.MergedAnnotation;
import org.springframework.core.annotation.MergedAnnotations;
import org.springframework.core.log.LogMessage;
import org.springframework.lang.Nullable;

Expand All @@ -54,6 +46,8 @@
*
* @author Brian Clozel
* @author Sebastien Deleuze
* @author Juergen Hoeller
* @since 6.0
*/
class RuntimeHintsBeanFactoryInitializationAotProcessor implements BeanFactoryInitializationAotProcessor {

Expand All @@ -67,94 +61,51 @@ public BeanFactoryInitializationAotContribution processAheadOfTime(ConfigurableL
.collect(LinkedHashMap::new, (map, item) -> map.put(item.getClass(), item), Map::putAll);
extractFromBeanFactory(beanFactory).forEach(registrarClass ->
registrars.computeIfAbsent(registrarClass, BeanUtils::instantiateClass));
return new RuntimeHintsRegistrarContribution(registrars.values(),
beanFactory.getBeanClassLoader());
return new RuntimeHintsRegistrarContribution(registrars.values(), beanFactory.getBeanClassLoader());
}

private Set<Class<? extends RuntimeHintsRegistrar>> extractFromBeanFactory(ConfigurableListableBeanFactory beanFactory) {
Set<Class<? extends RuntimeHintsRegistrar>> registrarClasses = new LinkedHashSet<>();
for (String beanName : beanFactory
.getBeanNamesForAnnotation(ImportRuntimeHints.class)) {
findAnnotationsOnBean(beanFactory, beanName,
ImportRuntimeHints.class).forEach(annotation ->
registrarClasses.addAll(extractFromBeanDefinition(beanName, annotation)));
for (String beanName : beanFactory.getBeanDefinitionNames()) {
beanFactory.findAllAnnotationsOnBean(beanName, ImportRuntimeHints.class, true)
.forEach(annotation -> registrarClasses.addAll(extractFromBeanDefinition(beanName, annotation)));
}
return registrarClasses;
}

private <A extends Annotation> List<A> findAnnotationsOnBean(ConfigurableListableBeanFactory beanFactory,
String beanName, Class<A> annotationType) {

List<A> annotations = new ArrayList<>();
Class<?> beanType = beanFactory.getType(beanName, true);
if (beanType != null) {
MergedAnnotations.from(beanType, MergedAnnotations.SearchStrategy.TYPE_HIERARCHY)
.stream(annotationType)
.filter(MergedAnnotation::isPresent)
.forEach(mergedAnnotation -> annotations.add(mergedAnnotation.synthesize()));
}
if (beanFactory.containsBeanDefinition(beanName)) {
BeanDefinition bd = beanFactory.getBeanDefinition(beanName);
if (bd instanceof RootBeanDefinition rbd) {
// Check raw bean class, e.g. in case of a proxy.
if (rbd.hasBeanClass() && rbd.getFactoryMethodName() == null) {
Class<?> beanClass = rbd.getBeanClass();
if (beanClass != beanType) {
MergedAnnotations.from(beanClass, MergedAnnotations.SearchStrategy.TYPE_HIERARCHY)
.stream(annotationType)
.filter(MergedAnnotation::isPresent)
.forEach(mergedAnnotation -> annotations.add(mergedAnnotation.synthesize()));
}
}
// Check annotations declared on factory method, if any.
Method factoryMethod = rbd.getResolvedFactoryMethod();
if (factoryMethod != null) {
MergedAnnotations.from(factoryMethod, MergedAnnotations.SearchStrategy.TYPE_HIERARCHY)
.stream(annotationType)
.filter(MergedAnnotation::isPresent)
.forEach(mergedAnnotation -> annotations.add(mergedAnnotation.synthesize()));
}
}
}
return annotations;
}

private Set<Class<? extends RuntimeHintsRegistrar>> extractFromBeanDefinition(String beanName,
ImportRuntimeHints annotation) {

Set<Class<? extends RuntimeHintsRegistrar>> registrars = new LinkedHashSet<>();
for (Class<? extends RuntimeHintsRegistrar> registrarClass : annotation.value()) {
if (logger.isTraceEnabled()) {
logger.trace(
LogMessage.format("Loaded [%s] registrar from annotated bean [%s]",
registrarClass.getCanonicalName(), beanName));
logger.trace(LogMessage.format("Loaded [%s] registrar from annotated bean [%s]",
registrarClass.getCanonicalName(), beanName));
}
registrars.add(registrarClass);
}
return registrars;
}


static class RuntimeHintsRegistrarContribution
implements BeanFactoryInitializationAotContribution {

static class RuntimeHintsRegistrarContribution implements BeanFactoryInitializationAotContribution {

private final Iterable<RuntimeHintsRegistrar> registrars;

@Nullable
private final ClassLoader beanClassLoader;


RuntimeHintsRegistrarContribution(Iterable<RuntimeHintsRegistrar> registrars,
@Nullable ClassLoader beanClassLoader) {

this.registrars = registrars;
this.beanClassLoader = beanClassLoader;
}


@Override
public void applyTo(GenerationContext generationContext,
BeanFactoryInitializationCode beanFactoryInitializationCode) {

RuntimeHints hints = generationContext.getRuntimeHints();
this.registrars.forEach(registrar -> {
if (logger.isTraceEnabled()) {
Expand All @@ -165,7 +116,6 @@ public void applyTo(GenerationContext generationContext,
registrar.registerHints(hints, this.beanClassLoader);
});
}

}

}
Expand Up @@ -1318,6 +1318,15 @@ public <A extends Annotation> A findAnnotationOnBean(
return getBeanFactory().findAnnotationOnBean(beanName, annotationType, allowFactoryBeanInit);
}

@Override
public <A extends Annotation> Set<A> findAllAnnotationsOnBean(
String beanName, Class<A> annotationType, boolean allowFactoryBeanInit)
throws NoSuchBeanDefinitionException {

assertBeanFactoryActive();
return getBeanFactory().findAllAnnotationsOnBean(beanName, annotationType, allowFactoryBeanInit);
}


//---------------------------------------------------------------------
// Implementation of HierarchicalBeanFactory interface
Expand Down
Expand Up @@ -829,8 +829,7 @@ private static MultiValueMap<String, Object> nullIfEmpty(MultiValueMap<String, O
}

private static <A extends Annotation> Comparator<MergedAnnotation<A>> highAggregateIndexesFirst() {
return Comparator.<MergedAnnotation<A>> comparingInt(
MergedAnnotation::getAggregateIndex).reversed();
return Comparator.<MergedAnnotation<A>> comparingInt(MergedAnnotation::getAggregateIndex).reversed();
}

@Nullable
Expand All @@ -840,8 +839,7 @@ private static AnnotationAttributes getAnnotationAttributes(MergedAnnotation<?>
if (!annotation.isPresent()) {
return null;
}
return annotation.asAnnotationAttributes(
Adapt.values(classValuesAsString, nestedAnnotationsAsMap));
return annotation.asAnnotationAttributes(Adapt.values(classValuesAsString, nestedAnnotationsAsMap));
}


Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2022 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -323,6 +323,14 @@ public <A extends Annotation> A findAnnotationOnBean(
return this.beanFactory.findAnnotationOnBean(beanName, annotationType, allowFactoryBeanInit);
}

@Override
public <A extends Annotation> Set<A> findAllAnnotationsOnBean(
String beanName, Class<A> annotationType, boolean allowFactoryBeanInit)
throws NoSuchBeanDefinitionException {

return this.beanFactory.findAllAnnotationsOnBean(beanName, annotationType, allowFactoryBeanInit);
}


//---------------------------------------------------------------------
// Implementation of HierarchicalBeanFactory interface
Expand Down

0 comments on commit 86d4557

Please sign in to comment.