Browse Source

Strong references to mapped exception handler methods

Issue: SPR-15907
pull/1510/head
Juergen Hoeller 8 years ago
parent
commit
2b44e6e21c
  1. 9
      spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/AbstractExceptionHandlerMethodResolver.java
  2. 4
      spring-messaging/src/test/java/org/springframework/messaging/handler/annotation/support/AnnotationExceptionHandlerMethodResolverTests.java
  3. 7
      spring-web/src/main/java/org/springframework/web/method/annotation/ExceptionHandlerMethodResolver.java
  4. 8
      spring-web/src/test/java/org/springframework/web/method/annotation/ExceptionHandlerMethodResolverTests.java

9
spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/AbstractExceptionHandlerMethodResolver.java

@ -19,6 +19,7 @@ package org.springframework.messaging.handler.invocation; @@ -19,6 +19,7 @@ package org.springframework.messaging.handler.invocation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -38,7 +39,7 @@ import org.springframework.util.ConcurrentReferenceHashMap; @@ -38,7 +39,7 @@ import org.springframework.util.ConcurrentReferenceHashMap;
*/
public abstract class AbstractExceptionHandlerMethodResolver {
private final Map<Class<? extends Throwable>, Method> mappedMethods = new ConcurrentReferenceHashMap<>(16);
private final Map<Class<? extends Throwable>, Method> mappedMethods = new HashMap<>(16);
private final Map<Class<? extends Throwable>, Method> exceptionLookupCache = new ConcurrentReferenceHashMap<>(16);
@ -64,7 +65,9 @@ public abstract class AbstractExceptionHandlerMethodResolver { @@ -64,7 +65,9 @@ public abstract class AbstractExceptionHandlerMethodResolver {
result.add((Class<? extends Throwable>) paramType);
}
}
Assert.notEmpty(result, "No exception types mapped to {" + method + "}");
if (result.isEmpty()) {
throw new IllegalStateException("No exception types mapped to " + method);
}
return result;
}
@ -73,7 +76,7 @@ public abstract class AbstractExceptionHandlerMethodResolver { @@ -73,7 +76,7 @@ public abstract class AbstractExceptionHandlerMethodResolver {
* Whether the contained type has any exception mappings.
*/
public boolean hasExceptionMappings() {
return (this.mappedMethods.size() > 0);
return !this.mappedMethods.isEmpty();
}
/**

4
spring-messaging/src/test/java/org/springframework/messaging/handler/annotation/support/AnnotationExceptionHandlerMethodResolverTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2017 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.
@ -94,7 +94,7 @@ public class AnnotationExceptionHandlerMethodResolverTests { @@ -94,7 +94,7 @@ public class AnnotationExceptionHandlerMethodResolverTests {
new AnnotationExceptionHandlerMethodResolver(AmbiguousController.class);
}
@Test(expected = IllegalArgumentException.class)
@Test(expected = IllegalStateException.class)
public void noExceptionMapping() {
new AnnotationExceptionHandlerMethodResolver(NoExceptionController.class);
}

7
spring-web/src/main/java/org/springframework/web/method/annotation/ExceptionHandlerMethodResolver.java

@ -20,6 +20,7 @@ import java.lang.reflect.Method; @@ -20,6 +20,7 @@ import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -50,7 +51,7 @@ public class ExceptionHandlerMethodResolver { @@ -50,7 +51,7 @@ public class ExceptionHandlerMethodResolver {
(AnnotationUtils.findAnnotation(method, ExceptionHandler.class) != null);
private final Map<Class<? extends Throwable>, Method> mappedMethods = new ConcurrentReferenceHashMap<>(16);
private final Map<Class<? extends Throwable>, Method> mappedMethods = new HashMap<>(16);
private final Map<Class<? extends Throwable>, Method> exceptionLookupCache = new ConcurrentReferenceHashMap<>(16);
@ -83,7 +84,9 @@ public class ExceptionHandlerMethodResolver { @@ -83,7 +84,9 @@ public class ExceptionHandlerMethodResolver {
}
}
}
Assert.notEmpty(result, "No exception types mapped to {" + method + "}");
if (result.isEmpty()) {
throw new IllegalStateException("No exception types mapped to " + method);
}
return result;
}

8
spring-web/src/test/java/org/springframework/web/method/annotation/ExceptionHandlerMethodResolverTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2017 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.
@ -88,11 +88,12 @@ public class ExceptionHandlerMethodResolverTests { @@ -88,11 +88,12 @@ public class ExceptionHandlerMethodResolverTests {
new ExceptionHandlerMethodResolver(AmbiguousController.class);
}
@Test(expected = IllegalArgumentException.class)
@Test(expected = IllegalStateException.class)
public void noExceptionMapping() {
new ExceptionHandlerMethodResolver(NoExceptionController.class);
}
@Controller
static class ExceptionController {
@ -111,6 +112,7 @@ public class ExceptionHandlerMethodResolverTests { @@ -111,6 +112,7 @@ public class ExceptionHandlerMethodResolverTests {
}
}
@Controller
static class InheritedController extends ExceptionController {
@ -119,6 +121,7 @@ public class ExceptionHandlerMethodResolverTests { @@ -119,6 +121,7 @@ public class ExceptionHandlerMethodResolverTests {
}
}
@Controller
static class AmbiguousController {
@ -136,6 +139,7 @@ public class ExceptionHandlerMethodResolverTests { @@ -136,6 +139,7 @@ public class ExceptionHandlerMethodResolverTests {
}
}
@Controller
static class NoExceptionController {

Loading…
Cancel
Save