Skip to content

Commit

Permalink
Polish "Check for Class and ResolvableType object type attributes"
Browse files Browse the repository at this point in the history
  • Loading branch information
wilkinsona committed Jul 5, 2023
1 parent 3a71852 commit f4c996b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -248,29 +248,22 @@ private Set<String> getExistingBeans(ConfigurableListableBeanFactory beanFactory
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ void cannotMockMultipleQualifiedBeans() {
}

@Test
void canMockBeanProducedByFactoryBeanWithObjectTypeAttribute() {
void canMockBeanProducedByFactoryBeanWithStringObjectTypeAttribute() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
MockitoPostProcessor.register(context);
RootBeanDefinition factoryBeanDefinition = new RootBeanDefinition(TestFactoryBean.class);
Expand All @@ -86,7 +86,7 @@ void canMockBeanProducedByFactoryBeanWithObjectTypeAttribute() {
}

@Test
void canMockBeanProducedByFactoryBeanWithClassAttribute() {
void canMockBeanProducedByFactoryBeanWithClassObjectTypeAttribute() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
MockitoPostProcessor.register(context);
RootBeanDefinition factoryBeanDefinition = new RootBeanDefinition(TestFactoryBean.class);
Expand All @@ -98,7 +98,7 @@ void canMockBeanProducedByFactoryBeanWithClassAttribute() {
}

@Test
void canMockBeanProducedByFactoryBeanWithResolvableTypeAttribute() {
void canMockBeanProducedByFactoryBeanWithResolvableTypeObjectTypeAttribute() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
MockitoPostProcessor.register(context);
RootBeanDefinition factoryBeanDefinition = new RootBeanDefinition(TestFactoryBean.class);
Expand Down

0 comments on commit f4c996b

Please sign in to comment.