Skip to content

Commit

Permalink
Code generation not applying shortcut if the target method is ambiguous
Browse files Browse the repository at this point in the history
  • Loading branch information
Devashish2293 committed Jun 26, 2023
1 parent 6072a53 commit 81e3d08
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
Expand Up @@ -220,7 +220,9 @@ private CodeBlock generateCodeForAccessibleFactoryMethod(String beanName,
CodeBlock.Builder code = CodeBlock.builder();
code.add("$T.<$T>forFactoryMethod($T.class, $S)", BeanInstanceSupplier.class,
suppliedType, declaringClass, factoryMethod.getName());
code.add(".withGenerator($T::$L)", declaringClass, factoryMethod.getName());
if (!isFactoryMethodAmbiguous(factoryMethod.getName(), declaringClass)) {
code.add(".withGenerator($T::$L)", declaringClass, factoryMethod.getName());
}
return code.build();
}

Expand All @@ -229,6 +231,12 @@ private CodeBlock generateCodeForAccessibleFactoryMethod(String beanName,
declaringClass, dependsOnBean, PRIVATE_STATIC));
return generateReturnStatement(getInstanceMethod);
}
private boolean isFactoryMethodAmbiguous(String methodName, Class<?> declaringClass) {
long count = Arrays.stream(declaringClass.getMethods())
.filter(method -> method.getName().equals(methodName))
.count();
return count > 1;
}

private CodeBlock generateCodeForInaccessibleFactoryMethod(
String beanName, Method factoryMethod, Class<?> declaringClass) {
Expand Down
Expand Up @@ -584,6 +584,13 @@ void generateBeanDefinitionMethodWhenBeanIsInJavaxPackage() {
testBeanDefinitionMethodInCurrentFile(DocumentBuilderFactory.class, beanDefinition);
}

@Test
void generateBeanDefinitionMethodWhenFactoryMethodIsAmbiguous() {
RootBeanDefinition beanDefinition = (RootBeanDefinition) BeanDefinitionBuilder
.rootBeanDefinition(DocumentBuilderFactory.class).setFactoryMethod("newNSInstance").getBeanDefinition();
testBeanDefinitionMethodInCurrentFile(DocumentBuilderFactory.class, beanDefinition);
}

@Test
void generateBeanDefinitionMethodWhenBeanIsOfPrimitiveType() {
RootBeanDefinition beanDefinition = (RootBeanDefinition) BeanDefinitionBuilder
Expand Down

0 comments on commit 81e3d08

Please sign in to comment.