Use the bean factory to get the type produced by a factory bean

Previously, we only looked at the OBJECT_TYPE_ATTRIBUTE on the
factory bean's definition. This did not work for situations
where the information's provided by the definition's target type
rather than the attribute.

Rather than manually considering the target type in addition to
the existing consideration of the attribute, we now ask the bean
factory for the type that will be produced by the factory bean
instead. This should insulate us from any changes and
enhancements in Framework in the future.

Fixes gh-40234
This commit is contained in:
Andy Wilkinson
2024-05-21 15:27:59 +01:00
parent a894879085
commit 486ceecc9a
2 changed files with 95 additions and 3 deletions

View File

@@ -254,9 +254,8 @@ public class MockitoPostProcessor implements InstantiationAwareBeanPostProcessor
Class<?> type = resolvableType.resolve(Object.class);
for (String beanName : beanFactory.getBeanNamesForType(FactoryBean.class, true, false)) {
beanName = BeanFactoryUtils.transformedBeanName(beanName);
BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanName);
Object attribute = beanDefinition.getAttribute(FactoryBean.OBJECT_TYPE_ATTRIBUTE);
if (resolvableType.equals(attribute) || type.equals(attribute)) {
Class<?> producedType = beanFactory.getType(beanName, false);
if (type.equals(producedType)) {
beans.add(beanName);
}
}