Improve HandlerMethod#resolvedFromHandlerMethod initialization

Ensure the original instance is always the one returned no matter how
many times the HandlerMethod is re-created.

Make the constructor protected to allow subclasses to re-create the
HandlerMethod as the concrete subclass.

See gh-34375
This commit is contained in:
rstoyanchev
2025-02-07 13:01:04 +00:00
parent 174d0e4576
commit 56c4d2d4ca
2 changed files with 26 additions and 3 deletions

View File

@@ -181,9 +181,13 @@ public class HandlerMethod extends AnnotatedMethod {
}
/**
* Re-create HandlerMethod with additional input.
* Re-create new HandlerMethod instance that copies the given HandlerMethod
* but replaces the handler, and optionally checks for the presence of
* validation annotations.
* <p>Subclasses can override this to ensure that a HandlerMethod is of the
* same type if re-created.
*/
private HandlerMethod(HandlerMethod handlerMethod, @Nullable Object handler, boolean initValidateFlags) {
protected HandlerMethod(HandlerMethod handlerMethod, @Nullable Object handler, boolean initValidateFlags) {
super(handlerMethod);
this.bean = (handler != null ? handler : handlerMethod.bean);
this.beanFactory = handlerMethod.beanFactory;
@@ -197,7 +201,8 @@ public class HandlerMethod extends AnnotatedMethod {
handlerMethod.validateReturnValue);
this.responseStatus = handlerMethod.responseStatus;
this.responseStatusReason = handlerMethod.responseStatusReason;
this.resolvedFromHandlerMethod = handlerMethod;
this.resolvedFromHandlerMethod = (handlerMethod.resolvedFromHandlerMethod != null ?
handlerMethod.resolvedFromHandlerMethod : handlerMethod);
this.description = handlerMethod.toString();
}