Polish "Check for Class and ResolvableType object type attributes"

See gh-36224
pull/36620/head
Andy Wilkinson 1 year ago
parent 3a7185206e
commit f4c996b432

@ -248,29 +248,22 @@ public class MockitoPostProcessor implements InstantiationAwareBeanPostProcessor
return candidates;
}
private Set<String> getExistingBeans(ConfigurableListableBeanFactory beanFactory, ResolvableType type) {
Set<String> beans = new LinkedHashSet<>(Arrays.asList(beanFactory.getBeanNamesForType(type, true, false)));
String typeName = type.resolve(Object.class).getName();
private Set<String> getExistingBeans(ConfigurableListableBeanFactory beanFactory, ResolvableType resolvableType) {
Set<String> beans = new LinkedHashSet<>(
Arrays.asList(beanFactory.getBeanNamesForType(resolvableType, true, false)));
Class<?> type = resolvableType.resolve(Object.class);
String typeName = type.getName();
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(attribute instanceof Class) {
Class<?> attributeClass = (Class<?>) attribute;
if (typeName.equals(attributeClass.getName())) {
beans.add(beanName);
}
} else if (attribute instanceof ResolvableType) {
ResolvableType resolvableType = (ResolvableType) attribute;
if (typeName.equals(resolvableType.resolve(Object.class).getName())) {
beans.add(beanName);
}
} else if (typeName.equals(attribute)){
if (resolvableType.equals(attribute) || type.equals(attribute) || typeName.equals(attribute)) {
beans.add(beanName);
}
}
beans.removeIf(this::isScopedTarget);
return beans;
}
private boolean isScopedTarget(String beanName) {

@ -74,7 +74,7 @@ class MockitoPostProcessorTests {
}
@Test
void canMockBeanProducedByFactoryBeanWithObjectTypeAttribute() {
void canMockBeanProducedByFactoryBeanWithStringObjectTypeAttribute() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
MockitoPostProcessor.register(context);
RootBeanDefinition factoryBeanDefinition = new RootBeanDefinition(TestFactoryBean.class);
@ -86,7 +86,7 @@ class MockitoPostProcessorTests {
}
@Test
void canMockBeanProducedByFactoryBeanWithClassAttribute() {
void canMockBeanProducedByFactoryBeanWithClassObjectTypeAttribute() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
MockitoPostProcessor.register(context);
RootBeanDefinition factoryBeanDefinition = new RootBeanDefinition(TestFactoryBean.class);
@ -98,7 +98,7 @@ class MockitoPostProcessorTests {
}
@Test
void canMockBeanProducedByFactoryBeanWithResolvableTypeAttribute() {
void canMockBeanProducedByFactoryBeanWithResolvableTypeObjectTypeAttribute() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
MockitoPostProcessor.register(context);
RootBeanDefinition factoryBeanDefinition = new RootBeanDefinition(TestFactoryBean.class);

Loading…
Cancel
Save