Skip to content

Commit 2b44e6e

Browse files
committed
Strong references to mapped exception handler methods
Issue: SPR-15907
1 parent b122bc6 commit 2b44e6e

File tree

4 files changed

+19
-9
lines changed

4 files changed

+19
-9
lines changed

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.lang.reflect.Method;
2020
import java.util.ArrayList;
2121
import java.util.Collections;
22+
import java.util.HashMap;
2223
import java.util.List;
2324
import java.util.Map;
2425

@@ -38,7 +39,7 @@
3839
*/
3940
public abstract class AbstractExceptionHandlerMethodResolver {
4041

41-
private final Map<Class<? extends Throwable>, Method> mappedMethods = new ConcurrentReferenceHashMap<>(16);
42+
private final Map<Class<? extends Throwable>, Method> mappedMethods = new HashMap<>(16);
4243

4344
private final Map<Class<? extends Throwable>, Method> exceptionLookupCache = new ConcurrentReferenceHashMap<>(16);
4445

@@ -64,7 +65,9 @@ protected static List<Class<? extends Throwable>> getExceptionsFromMethodSignatu
6465
result.add((Class<? extends Throwable>) paramType);
6566
}
6667
}
67-
Assert.notEmpty(result, "No exception types mapped to {" + method + "}");
68+
if (result.isEmpty()) {
69+
throw new IllegalStateException("No exception types mapped to " + method);
70+
}
6871
return result;
6972
}
7073

@@ -73,7 +76,7 @@ protected static List<Class<? extends Throwable>> getExceptionsFromMethodSignatu
7376
* Whether the contained type has any exception mappings.
7477
*/
7578
public boolean hasExceptionMappings() {
76-
return (this.mappedMethods.size() > 0);
79+
return !this.mappedMethods.isEmpty();
7780
}
7881

7982
/**

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -94,7 +94,7 @@ public void ambiguousExceptionMapping() {
9494
new AnnotationExceptionHandlerMethodResolver(AmbiguousController.class);
9595
}
9696

97-
@Test(expected = IllegalArgumentException.class)
97+
@Test(expected = IllegalStateException.class)
9898
public void noExceptionMapping() {
9999
new AnnotationExceptionHandlerMethodResolver(NoExceptionController.class);
100100
}

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.ArrayList;
2121
import java.util.Arrays;
2222
import java.util.Collections;
23+
import java.util.HashMap;
2324
import java.util.List;
2425
import java.util.Map;
2526

@@ -50,7 +51,7 @@ public class ExceptionHandlerMethodResolver {
5051
(AnnotationUtils.findAnnotation(method, ExceptionHandler.class) != null);
5152

5253

53-
private final Map<Class<? extends Throwable>, Method> mappedMethods = new ConcurrentReferenceHashMap<>(16);
54+
private final Map<Class<? extends Throwable>, Method> mappedMethods = new HashMap<>(16);
5455

5556
private final Map<Class<? extends Throwable>, Method> exceptionLookupCache = new ConcurrentReferenceHashMap<>(16);
5657

@@ -83,7 +84,9 @@ private List<Class<? extends Throwable>> detectExceptionMappings(Method method)
8384
}
8485
}
8586
}
86-
Assert.notEmpty(result, "No exception types mapped to {" + method + "}");
87+
if (result.isEmpty()) {
88+
throw new IllegalStateException("No exception types mapped to " + method);
89+
}
8790
return result;
8891
}
8992

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -88,11 +88,12 @@ public void ambiguousExceptionMapping() {
8888
new ExceptionHandlerMethodResolver(AmbiguousController.class);
8989
}
9090

91-
@Test(expected = IllegalArgumentException.class)
91+
@Test(expected = IllegalStateException.class)
9292
public void noExceptionMapping() {
9393
new ExceptionHandlerMethodResolver(NoExceptionController.class);
9494
}
9595

96+
9697
@Controller
9798
static class ExceptionController {
9899

@@ -111,6 +112,7 @@ public void handleIllegalArgumentException(IllegalArgumentException exception) {
111112
}
112113
}
113114

115+
114116
@Controller
115117
static class InheritedController extends ExceptionController {
116118

@@ -119,6 +121,7 @@ public void handleIOException() {
119121
}
120122
}
121123

124+
122125
@Controller
123126
static class AmbiguousController {
124127

@@ -136,6 +139,7 @@ public String handle2(IllegalArgumentException ex) {
136139
}
137140
}
138141

142+
139143
@Controller
140144
static class NoExceptionController {
141145

0 commit comments

Comments
 (0)