Skip to content

Commit

Permalink
Avoid repeated FactoryBean targetType check
Browse files Browse the repository at this point in the history
  • Loading branch information
jhoeller committed Aug 3, 2023
1 parent 4b6fabb commit 9333ed2
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 38 deletions.
Expand Up @@ -837,16 +837,13 @@ protected ResolvableType getTypeForFactoryBean(String beanName, RootBeanDefiniti
return result;
}

ResolvableType beanType =
(mbd.hasBeanClass() ? ResolvableType.forClass(mbd.getBeanClass()) : ResolvableType.NONE);

// For instance supplied beans try the target type and bean class
// For instance supplied beans, try the target type and bean class immediately
if (mbd.getInstanceSupplier() != null) {
result = getFactoryBeanGeneric(mbd.targetType);
if (result.resolve() != null) {
return result;
}
result = getFactoryBeanGeneric(beanType);
result = getFactoryBeanGeneric(mbd.hasBeanClass() ? ResolvableType.forClass(mbd.getBeanClass()) : null);
if (result.resolve() != null) {
return result;
}
Expand Down Expand Up @@ -909,22 +906,20 @@ protected ResolvableType getTypeForFactoryBean(String beanName, RootBeanDefiniti
return getTypeForFactoryBeanFromMethod(mbd.getBeanClass(), factoryMethodName);
}

result = getFactoryBeanGeneric(mbd.targetType);
if (result.resolve() != null) {
return result;
}
result = getFactoryBeanGeneric(beanType);
if (result.resolve() != null) {
return result;
// For regular beans, try the target type and bean class as fallback
if (mbd.getInstanceSupplier() == null) {
result = getFactoryBeanGeneric(mbd.targetType);
if (result.resolve() != null) {
return result;
}
result = getFactoryBeanGeneric(mbd.hasBeanClass() ? ResolvableType.forClass(mbd.getBeanClass()) : null);
if (result.resolve() != null) {
return result;
}
}
return ResolvableType.NONE;
}

private ResolvableType getFactoryBeanGeneric(@Nullable ResolvableType type) {
if (type == null) {
return ResolvableType.NONE;
}
return type.as(FactoryBean.class).getGeneric();
// FactoryBean type not resolvable
return ResolvableType.NONE;
}

/**
Expand Down
Expand Up @@ -64,7 +64,6 @@
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
import org.springframework.beans.factory.config.Scope;
import org.springframework.beans.factory.config.SmartInstantiationAwareBeanPostProcessor;
import org.springframework.core.AttributeAccessor;
import org.springframework.core.DecoratingClassLoader;
import org.springframework.core.NamedThreadLocal;
import org.springframework.core.ResolvableType;
Expand Down Expand Up @@ -1684,25 +1683,8 @@ else if (mbd.isLazyInit()) {
onSuppressedException(ex);
}
}
return ResolvableType.NONE;
}

/**
* Determine the bean type for a FactoryBean by inspecting its attributes for a
* {@link FactoryBean#OBJECT_TYPE_ATTRIBUTE} value.
* @param attributes the attributes to inspect
* @return a {@link ResolvableType} extracted from the attributes or
* {@code ResolvableType.NONE}
* @since 5.2
*/
ResolvableType getTypeForFactoryBeanFromAttributes(AttributeAccessor attributes) {
Object attribute = attributes.getAttribute(FactoryBean.OBJECT_TYPE_ATTRIBUTE);
if (attribute instanceof ResolvableType resolvableType) {
return resolvableType;
}
if (attribute instanceof Class<?> clazz) {
return ResolvableType.forClass(clazz);
}
// FactoryBean type not resolvable
return ResolvableType.NONE;
}

Expand Down
Expand Up @@ -24,6 +24,8 @@
import org.springframework.beans.factory.BeanCurrentlyInCreationException;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.FactoryBeanNotInitializedException;
import org.springframework.core.AttributeAccessor;
import org.springframework.core.ResolvableType;
import org.springframework.lang.Nullable;

/**
Expand Down Expand Up @@ -61,6 +63,34 @@ protected Class<?> getTypeForFactoryBean(FactoryBean<?> factoryBean) {
}
}

/**
* Determine the bean type for a FactoryBean by inspecting its attributes for a
* {@link FactoryBean#OBJECT_TYPE_ATTRIBUTE} value.
* @param attributes the attributes to inspect
* @return a {@link ResolvableType} extracted from the attributes or
* {@code ResolvableType.NONE}
* @since 5.2
*/
ResolvableType getTypeForFactoryBeanFromAttributes(AttributeAccessor attributes) {
Object attribute = attributes.getAttribute(FactoryBean.OBJECT_TYPE_ATTRIBUTE);
if (attribute instanceof ResolvableType resolvableType) {
return resolvableType;
}
if (attribute instanceof Class<?> clazz) {
return ResolvableType.forClass(clazz);
}
return ResolvableType.NONE;
}

/**
* Determine the FactoryBean object type from the given generic declaration.
* @param type the FactoryBean type
* @return the nested object type, or {@code NONE} if not resolvable
*/
ResolvableType getFactoryBeanGeneric(@Nullable ResolvableType type) {
return (type != null ? type.as(FactoryBean.class).getGeneric() : ResolvableType.NONE);
}

/**
* Obtain an object to expose from the given FactoryBean, if available
* in cached form. Quick check for minimal synchronization.
Expand Down

0 comments on commit 9333ed2

Please sign in to comment.