Skip to content

Commit

Permalink
Better error reporting when instance does not match factory method
Browse files Browse the repository at this point in the history
This commit improves the handling of IllegalArgumentException in
SimpleInstantiationStrategy. Previously, only arguments mismatch were
handled but the exception can also be thrown if the factory instance
does not match the target method.

Closes gh-28897
  • Loading branch information
snicoll committed Oct 24, 2023
1 parent 6299a9d commit 999b5d4
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
*
* @author Rod Johnson
* @author Juergen Hoeller
* @author Stephane Nicoll
* @since 1.1
*/
public class SimpleInstantiationStrategy implements InstantiationStrategy {
Expand Down Expand Up @@ -152,6 +153,12 @@ public Object instantiate(RootBeanDefinition bd, @Nullable String beanName, Bean
}
}
catch (IllegalArgumentException ex) {
if (factoryBean != null
&& !factoryMethod.getDeclaringClass().isAssignableFrom(factoryBean.getClass())) {
throw new BeanInstantiationException(factoryMethod,
"Illegal factory instance for factory method '" + factoryMethod.getName() + "'; " +
"instance: " + factoryBean.getClass().getName(), ex);
}
throw new BeanInstantiationException(factoryMethod,
"Illegal arguments to factory method '" + factoryMethod.getName() + "'; " +
"args: " + StringUtils.arrayToCommaDelimitedString(args), ex);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ void instantiateWitArgs() {
assertThat(simpleBean).isEqualTo("Test42");
}

@Test
void instantiateWitSubClassFactoryArgs() {
RootBeanDefinition bd = new RootBeanDefinition(String.class);
Object simpleBean = instantiate(bd, new ExtendedSampleFactory(),
method(SampleFactory.class, "beanWithTwoArgs"), "Test", 42);
assertThat(simpleBean).isEqualTo("42Test");
}

@Test
void instantiateWithNullValueReturnsNullBean() {
RootBeanDefinition bd = new RootBeanDefinition(String.class);
Expand All @@ -71,6 +79,28 @@ bd, new SampleFactory(),
.withMessageContaining("args: 42,Test");
}

@Test
void instantiateWithTargetTypeMismatch() {
RootBeanDefinition bd = new RootBeanDefinition(String.class);
assertThatExceptionOfType(BeanInstantiationException.class).isThrownBy(() -> instantiate(
bd, new AnotherFactory(),
method(SampleFactory.class, "beanWithTwoArgs"), "Test", 42))
.withMessageContaining("Illegal factory instance for factory method 'beanWithTwoArgs'")
.withMessageContaining("instance: " + AnotherFactory.class.getName())
.withMessageNotContaining("args: Test,42");
}

@Test
void instantiateWithTargetTypeNotAssignable() {
RootBeanDefinition bd = new RootBeanDefinition(String.class);
assertThatExceptionOfType(BeanInstantiationException.class).isThrownBy(() -> instantiate(
bd, new SampleFactory(),
method(ExtendedSampleFactory.class, "beanWithTwoArgs"), "Test", 42))
.withMessageContaining("Illegal factory instance for factory method 'beanWithTwoArgs'")
.withMessageContaining("instance: " + SampleFactory.class.getName())
.withMessageNotContaining("args: Test,42");
}

@Test
void instantiateWithException() {
RootBeanDefinition bd = new RootBeanDefinition(String.class);
Expand Down Expand Up @@ -115,4 +145,20 @@ String errorBean(String msg) {
}

}

static class ExtendedSampleFactory extends SampleFactory {

@Override
String beanWithTwoArgs(String first, Integer second) {
return second + first;
}
}

static class AnotherFactory {

String beanWithTwoArgs(String first, Integer second) {
return second + first;
}

}
}

0 comments on commit 999b5d4

Please sign in to comment.