-
Notifications
You must be signed in to change notification settings - Fork 38.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reject null return value from MethodReplacer for primitive return type
This commit throws an exception instead of silently converting a null return value from a MethodReplacer to a primitive 0/false value. See gh-32412
- Loading branch information
Showing
2 changed files
with
133 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
...org/springframework/beans/factory/support/CglibSubclassingInstantiationStrategyTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package org.springframework.beans.factory.support; | ||
|
||
import static org.assertj.core.api.Assertions.assertThatThrownBy; | ||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat; | ||
|
||
import java.lang.reflect.Method; | ||
import java.util.Map; | ||
import java.util.stream.Stream; | ||
|
||
import org.assertj.core.api.ThrowableAssert; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.lang.Nullable; | ||
|
||
class CglibSubclassingInstantiationStrategyTests { | ||
|
||
private final CglibSubclassingInstantiationStrategy strategy = new CglibSubclassingInstantiationStrategy(); | ||
|
||
@Nullable | ||
public static Object valueToReturnFromReplacer; | ||
|
||
@Test | ||
void methodOverride() { | ||
StaticListableBeanFactory beanFactory = new StaticListableBeanFactory(Map.of( | ||
"myBean", new MyBean(), | ||
"replacer", new MyReplacer() | ||
)); | ||
|
||
RootBeanDefinition bd = new RootBeanDefinition(MyBean.class); | ||
MethodOverrides methodOverrides = new MethodOverrides(); | ||
Stream.of("getBoolean", "getShort", "getInt", "getLong", "getFloat", "getDouble", "getByte") | ||
.forEach(methodToOverride -> addOverride(methodOverrides, methodToOverride)); | ||
bd.setMethodOverrides(methodOverrides); | ||
|
||
MyBean bean = (MyBean) strategy.instantiate(bd, "myBean", beanFactory); | ||
|
||
valueToReturnFromReplacer = null; | ||
assertCorrectExceptionThrownBy(bean::getBoolean); | ||
valueToReturnFromReplacer = true; | ||
assertThat(bean.getBoolean()).isTrue(); | ||
|
||
valueToReturnFromReplacer = null; | ||
assertCorrectExceptionThrownBy(bean::getShort); | ||
valueToReturnFromReplacer = 123; | ||
assertThat(bean.getShort()).isEqualTo((short) 123); | ||
|
||
valueToReturnFromReplacer = null; | ||
assertCorrectExceptionThrownBy(bean::getInt); | ||
valueToReturnFromReplacer = 123; | ||
assertThat(bean.getInt()).isEqualTo(123); | ||
|
||
valueToReturnFromReplacer = null; | ||
assertCorrectExceptionThrownBy(bean::getLong); | ||
valueToReturnFromReplacer = 123; | ||
assertThat(bean.getLong()).isEqualTo(123L); | ||
|
||
valueToReturnFromReplacer = null; | ||
assertCorrectExceptionThrownBy(bean::getFloat); | ||
valueToReturnFromReplacer = 123; | ||
assertThat(bean.getFloat()).isEqualTo(123f); | ||
|
||
valueToReturnFromReplacer = null; | ||
assertCorrectExceptionThrownBy(bean::getDouble); | ||
valueToReturnFromReplacer = 123; | ||
assertThat(bean.getDouble()).isEqualTo(123d); | ||
|
||
valueToReturnFromReplacer = null; | ||
assertCorrectExceptionThrownBy(bean::getByte); | ||
valueToReturnFromReplacer = 123; | ||
assertThat(bean.getByte()).isEqualTo((byte) 123); | ||
} | ||
|
||
private void assertCorrectExceptionThrownBy(ThrowableAssert.ThrowingCallable runnable) { | ||
assertThatThrownBy(runnable) | ||
.isInstanceOf(NullPointerException.class) | ||
.hasMessageMatching("Null return value from replacer does not match primitive return type for: " | ||
+ "\\w+ org\\.springframework\\.beans\\.factory\\.support\\.CglibSubclassingInstantiationStrategyTests\\$MyBean\\.\\w+\\(\\)"); | ||
} | ||
|
||
private void addOverride(MethodOverrides methodOverrides, String methodToOverride) { | ||
methodOverrides.addOverride(new ReplaceOverride(methodToOverride, "replacer")); | ||
} | ||
|
||
static class MyBean { | ||
boolean getBoolean() { | ||
return true; | ||
} | ||
|
||
short getShort() { | ||
return 123; | ||
} | ||
|
||
int getInt() { | ||
return 123; | ||
} | ||
|
||
long getLong() { | ||
return 123; | ||
} | ||
|
||
float getFloat() { | ||
return 123; | ||
} | ||
|
||
double getDouble() { | ||
return 123; | ||
} | ||
|
||
byte getByte() { | ||
return 123; | ||
} | ||
} | ||
|
||
static class MyReplacer implements MethodReplacer { | ||
|
||
@Override | ||
public Object reimplement(Object obj, Method method, Object[] args) { | ||
return CglibSubclassingInstantiationStrategyTests.valueToReturnFromReplacer; | ||
} | ||
} | ||
} |