Revised InvocableHandlerMethod exception handling
Issue: SPR-11281
This commit is contained in:
@@ -72,8 +72,7 @@ public class InvocableHandlerMethod extends HandlerMethod {
|
|||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set {@link HandlerMethodArgumentResolver}s to use to use for resolving method
|
* Set {@link HandlerMethodArgumentResolver}s to use to use for resolving method argument values.
|
||||||
* argument values.
|
|
||||||
*/
|
*/
|
||||||
public void setMessageMethodArgumentResolvers(HandlerMethodArgumentResolverComposite argumentResolvers) {
|
public void setMessageMethodArgumentResolvers(HandlerMethodArgumentResolverComposite argumentResolvers) {
|
||||||
this.argumentResolvers = argumentResolvers;
|
this.argumentResolvers = argumentResolvers;
|
||||||
@@ -98,7 +97,7 @@ public class InvocableHandlerMethod extends HandlerMethod {
|
|||||||
Object[] args = getMethodArgumentValues(message, providedArgs);
|
Object[] args = getMethodArgumentValues(message, providedArgs);
|
||||||
if (logger.isTraceEnabled()) {
|
if (logger.isTraceEnabled()) {
|
||||||
StringBuilder sb = new StringBuilder("Invoking [");
|
StringBuilder sb = new StringBuilder("Invoking [");
|
||||||
sb.append(this.getBeanType().getSimpleName()).append(".");
|
sb.append(getBeanType().getSimpleName()).append(".");
|
||||||
sb.append(getMethod().getName()).append("] method with arguments ");
|
sb.append(getMethod().getName()).append("] method with arguments ");
|
||||||
sb.append(Arrays.asList(args));
|
sb.append(Arrays.asList(args));
|
||||||
logger.trace(sb.toString());
|
logger.trace(sb.toString());
|
||||||
@@ -118,7 +117,7 @@ public class InvocableHandlerMethod extends HandlerMethod {
|
|||||||
Object[] args = new Object[parameters.length];
|
Object[] args = new Object[parameters.length];
|
||||||
for (int i = 0; i < parameters.length; i++) {
|
for (int i = 0; i < parameters.length; i++) {
|
||||||
MethodParameter parameter = parameters[i];
|
MethodParameter parameter = parameters[i];
|
||||||
parameter.initParameterNameDiscovery(parameterNameDiscoverer);
|
parameter.initParameterNameDiscovery(this.parameterNameDiscoverer);
|
||||||
GenericTypeResolver.resolveParameterType(parameter, getBean().getClass());
|
GenericTypeResolver.resolveParameterType(parameter, getBean().getClass());
|
||||||
args[i] = resolveProvidedArgument(parameter, providedArgs);
|
args[i] = resolveProvidedArgument(parameter, providedArgs);
|
||||||
if (args[i] != null) {
|
if (args[i] != null) {
|
||||||
@@ -181,17 +180,17 @@ public class InvocableHandlerMethod extends HandlerMethod {
|
|||||||
* Invoke the handler method with the given argument values.
|
* Invoke the handler method with the given argument values.
|
||||||
*/
|
*/
|
||||||
private Object invoke(Object... args) throws Exception {
|
private Object invoke(Object... args) throws Exception {
|
||||||
ReflectionUtils.makeAccessible(this.getBridgedMethod());
|
ReflectionUtils.makeAccessible(getBridgedMethod());
|
||||||
try {
|
try {
|
||||||
assertTargetBean(getBridgedMethod(), getBean(), args);
|
|
||||||
return getBridgedMethod().invoke(getBean(), args);
|
return getBridgedMethod().invoke(getBean(), args);
|
||||||
}
|
}
|
||||||
catch (IllegalArgumentException e) {
|
catch (IllegalArgumentException ex) {
|
||||||
throw new IllegalArgumentException(getInvocationErrorMessage(e.getMessage(), args), e);
|
assertTargetBean(getBridgedMethod(), getBean(), args);
|
||||||
|
throw new IllegalStateException(getInvocationErrorMessage(ex.getMessage(), args), ex);
|
||||||
}
|
}
|
||||||
catch (InvocationTargetException e) {
|
catch (InvocationTargetException ex) {
|
||||||
// Unwrap for HandlerExceptionResolvers ...
|
// Unwrap for HandlerExceptionResolvers ...
|
||||||
Throwable targetException = e.getTargetException();
|
Throwable targetException = ex.getTargetException();
|
||||||
if (targetException instanceof RuntimeException) {
|
if (targetException instanceof RuntimeException) {
|
||||||
throw (RuntimeException) targetException;
|
throw (RuntimeException) targetException;
|
||||||
}
|
}
|
||||||
@@ -219,11 +218,11 @@ public class InvocableHandlerMethod extends HandlerMethod {
|
|||||||
Class<?> methodDeclaringClass = method.getDeclaringClass();
|
Class<?> methodDeclaringClass = method.getDeclaringClass();
|
||||||
Class<?> targetBeanClass = targetBean.getClass();
|
Class<?> targetBeanClass = targetBean.getClass();
|
||||||
if (!methodDeclaringClass.isAssignableFrom(targetBeanClass)) {
|
if (!methodDeclaringClass.isAssignableFrom(targetBeanClass)) {
|
||||||
String message = "The mapped controller method class '" + methodDeclaringClass.getName() +
|
String msg = "The mapped controller method class '" + methodDeclaringClass.getName() +
|
||||||
"' is not an instance of the actual controller bean instance '" +
|
"' is not an instance of the actual controller bean instance '" +
|
||||||
targetBeanClass.getName() + "'. If the controller requires proxying " +
|
targetBeanClass.getName() + "'. If the controller requires proxying " +
|
||||||
"(e.g. due to @Transactional), please use class-based proxying.";
|
"(e.g. due to @Transactional), please use class-based proxying.";
|
||||||
throw new IllegalArgumentException(getInvocationErrorMessage(message, args));
|
throw new IllegalStateException(getInvocationErrorMessage(msg, args));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -47,15 +47,15 @@ import org.springframework.web.method.HandlerMethod;
|
|||||||
*/
|
*/
|
||||||
public class InvocableHandlerMethod extends HandlerMethod {
|
public class InvocableHandlerMethod extends HandlerMethod {
|
||||||
|
|
||||||
private HandlerMethodArgumentResolverComposite argumentResolvers = new HandlerMethodArgumentResolverComposite();
|
|
||||||
|
|
||||||
private WebDataBinderFactory dataBinderFactory;
|
private WebDataBinderFactory dataBinderFactory;
|
||||||
|
|
||||||
|
private HandlerMethodArgumentResolverComposite argumentResolvers = new HandlerMethodArgumentResolverComposite();
|
||||||
|
|
||||||
private ParameterNameDiscoverer parameterNameDiscoverer = new DefaultParameterNameDiscoverer();
|
private ParameterNameDiscoverer parameterNameDiscoverer = new DefaultParameterNameDiscoverer();
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates an instance from the given handler and method.
|
* Create an instance from the given handler and method.
|
||||||
*/
|
*/
|
||||||
public InvocableHandlerMethod(Object bean, Method method) {
|
public InvocableHandlerMethod(Object bean, Method method) {
|
||||||
super(bean, method);
|
super(bean, method);
|
||||||
@@ -124,7 +124,7 @@ public class InvocableHandlerMethod extends HandlerMethod {
|
|||||||
Object[] args = getMethodArgumentValues(request, mavContainer, providedArgs);
|
Object[] args = getMethodArgumentValues(request, mavContainer, providedArgs);
|
||||||
if (logger.isTraceEnabled()) {
|
if (logger.isTraceEnabled()) {
|
||||||
StringBuilder sb = new StringBuilder("Invoking [");
|
StringBuilder sb = new StringBuilder("Invoking [");
|
||||||
sb.append(this.getBeanType().getSimpleName()).append(".");
|
sb.append(getBeanType().getSimpleName()).append(".");
|
||||||
sb.append(getMethod().getName()).append("] method with arguments ");
|
sb.append(getMethod().getName()).append("] method with arguments ");
|
||||||
sb.append(Arrays.asList(args));
|
sb.append(Arrays.asList(args));
|
||||||
logger.trace(sb.toString());
|
logger.trace(sb.toString());
|
||||||
@@ -154,7 +154,8 @@ public class InvocableHandlerMethod extends HandlerMethod {
|
|||||||
}
|
}
|
||||||
if (this.argumentResolvers.supportsParameter(parameter)) {
|
if (this.argumentResolvers.supportsParameter(parameter)) {
|
||||||
try {
|
try {
|
||||||
args[i] = this.argumentResolvers.resolveArgument(parameter, mavContainer, request, this.dataBinderFactory);
|
args[i] = this.argumentResolvers.resolveArgument(
|
||||||
|
parameter, mavContainer, request, this.dataBinderFactory);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
catch (Exception ex) {
|
catch (Exception ex) {
|
||||||
@@ -209,17 +210,17 @@ public class InvocableHandlerMethod extends HandlerMethod {
|
|||||||
* Invoke the handler method with the given argument values.
|
* Invoke the handler method with the given argument values.
|
||||||
*/
|
*/
|
||||||
private Object invoke(Object... args) throws Exception {
|
private Object invoke(Object... args) throws Exception {
|
||||||
ReflectionUtils.makeAccessible(this.getBridgedMethod());
|
ReflectionUtils.makeAccessible(getBridgedMethod());
|
||||||
try {
|
try {
|
||||||
assertTargetBean(getBridgedMethod(), getBean(), args);
|
|
||||||
return getBridgedMethod().invoke(getBean(), args);
|
return getBridgedMethod().invoke(getBean(), args);
|
||||||
}
|
}
|
||||||
catch (IllegalArgumentException e) {
|
catch (IllegalArgumentException ex) {
|
||||||
throw new IllegalArgumentException(getInvocationErrorMessage(e.getMessage(), args), e);
|
assertTargetBean(getBridgedMethod(), getBean(), args);
|
||||||
|
throw new IllegalStateException(getInvocationErrorMessage(ex.getMessage(), args), ex);
|
||||||
}
|
}
|
||||||
catch (InvocationTargetException e) {
|
catch (InvocationTargetException ex) {
|
||||||
// Unwrap for HandlerExceptionResolvers ...
|
// Unwrap for HandlerExceptionResolvers ...
|
||||||
Throwable targetException = e.getTargetException();
|
Throwable targetException = ex.getTargetException();
|
||||||
if (targetException instanceof RuntimeException) {
|
if (targetException instanceof RuntimeException) {
|
||||||
throw (RuntimeException) targetException;
|
throw (RuntimeException) targetException;
|
||||||
}
|
}
|
||||||
@@ -247,11 +248,11 @@ public class InvocableHandlerMethod extends HandlerMethod {
|
|||||||
Class<?> methodDeclaringClass = method.getDeclaringClass();
|
Class<?> methodDeclaringClass = method.getDeclaringClass();
|
||||||
Class<?> targetBeanClass = targetBean.getClass();
|
Class<?> targetBeanClass = targetBean.getClass();
|
||||||
if (!methodDeclaringClass.isAssignableFrom(targetBeanClass)) {
|
if (!methodDeclaringClass.isAssignableFrom(targetBeanClass)) {
|
||||||
String message = "The mapped controller method class '" + methodDeclaringClass.getName() +
|
String msg = "The mapped controller method class '" + methodDeclaringClass.getName() +
|
||||||
"' is not an instance of the actual controller bean instance '" +
|
"' is not an instance of the actual controller bean instance '" +
|
||||||
targetBeanClass.getName() + "'. If the controller requires proxying " +
|
targetBeanClass.getName() + "'. If the controller requires proxying " +
|
||||||
"(e.g. due to @Transactional), please use class-based proxying.";
|
"(e.g. due to @Transactional), please use class-based proxying.";
|
||||||
throw new IllegalArgumentException(getInvocationErrorMessage(message, args));
|
throw new IllegalStateException(getInvocationErrorMessage(msg, args));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user