Browse Source

Fix NPE in InvocableHandlerMethod

Issue: SPR-13917
pull/959/head
Rossen Stoyanchev 9 years ago
parent
commit
902a7287f7
  1. 3
      spring-web/src/main/java/org/springframework/web/method/support/InvocableHandlerMethod.java
  2. 28
      spring-web/src/test/java/org/springframework/web/method/support/InvocableHandlerMethodTests.java

3
spring-web/src/main/java/org/springframework/web/method/support/InvocableHandlerMethod.java

@ -223,7 +223,8 @@ public class InvocableHandlerMethod extends HandlerMethod { @@ -223,7 +223,8 @@ public class InvocableHandlerMethod extends HandlerMethod {
}
catch (IllegalArgumentException ex) {
assertTargetBean(getBridgedMethod(), getBean(), args);
throw new IllegalStateException(getInvocationErrorMessage(ex.getMessage(), args), ex);
String message = (ex.getMessage() != null ? ex.getMessage() : "Illegal argument");
throw new IllegalStateException(getInvocationErrorMessage(message, args), ex);
}
catch (InvocationTargetException ex) {
// Unwrap for HandlerExceptionResolvers ...

28
spring-web/src/test/java/org/springframework/web/method/support/InvocableHandlerMethodTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2014 the original author or authors.
* Copyright 2002-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -29,6 +29,7 @@ import org.springframework.web.bind.support.WebDataBinderFactory; @@ -29,6 +29,7 @@ import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.context.request.ServletWebRequest;
import static org.hamcrest.Matchers.containsString;
import static org.junit.Assert.*;
/**
@ -195,6 +196,28 @@ public class InvocableHandlerMethodTests { @@ -195,6 +196,28 @@ public class InvocableHandlerMethodTests {
}
}
// SPR-13917
@Test
public void invocationErrorMessage() throws Exception {
HandlerMethodArgumentResolverComposite composite = new HandlerMethodArgumentResolverComposite();
composite.addResolver(new StubArgumentResolver(double.class, null));
Method method = Handler.class.getDeclaredMethod("handle", double.class);
Object handler = new Handler();
InvocableHandlerMethod hm = new InvocableHandlerMethod(handler, method);
hm.setHandlerMethodArgumentResolvers(composite);
try {
hm.invokeForRequest(this.webRequest, new ModelAndViewContainer());
fail();
}
catch (IllegalStateException ex) {
assertThat(ex.getMessage(), containsString("Illegal argument"));
}
}
private void invokeExceptionRaisingHandler(Throwable expected) throws Exception {
Method method = ExceptionRaisingHandler.class.getDeclaredMethod("raiseException");
Object handler = new ExceptionRaisingHandler(expected);
@ -209,6 +232,9 @@ public class InvocableHandlerMethodTests { @@ -209,6 +232,9 @@ public class InvocableHandlerMethodTests {
public String handle(Integer intArg, String stringArg) {
return intArg + "-" + stringArg;
}
public void handle(double amount) {
}
}

Loading…
Cancel
Save