Reject null
return value from MethodReplacer
for primitive return type
#32412
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
If a
MethodReplacer
replaces a method with a primitive return type, and if the replacer returnsnull
, CGLIB silently converts thenull
to a zero-value primitive (false or 0), instead of throwing an exception (seeCodeEmitter::zero_or_null
in CGLIB).This does not align with the rest of the Spring Framework. For example, the AOP module checks for a
null
in advices for methods with primitive return types before it reaches the CGLIB proxy (CglibAopProxy::processReturnType
) and throws anAopInvocationException
. Similarly, the AOP module maintains functional parity between the JDKProxy
and CGLIB proxies by wrapping an undeclared checked exception in anUndeclaredThrowableException
. Moreover, silently converting anull
to a non-null value can lead to hard-to-find bugs.MvcUriComponentsBuilder.ControllerMethodInvocationInterceptor
also has this quirk, but the return values of the proxies it returns aren't meant to be used, so I didn't touch that one.This PR follows the pattern used by
CglibAopProxy
and throws an exception ifnull
is returned in the advice for a method with a primitive return type.