33
33
import org .springframework .web .method .HandlerMethodSelector ;
34
34
35
35
/**
36
- * Discovers {@linkplain ExceptionHandler @ExceptionHandler} methods in a given class
37
- * type, including all super types, and helps to resolve an Exception to the method
38
- * its mapped to. Exception mappings are defined through {@code @ExceptionHandler}
39
- * annotation or by looking at the signature of an {@code @ExceptionHandler} method.
36
+ * Discovers {@linkplain ExceptionHandler @ExceptionHandler} methods in a given class,
37
+ * including all of its superclasses, and helps to resolve a given {@link Exception}
38
+ * to the exception types supported by a given {@link Method}.
40
39
*
41
40
* @author Rossen Stoyanchev
42
41
* @since 3.1
@@ -46,12 +45,15 @@ public class ExceptionHandlerMethodResolver {
46
45
/**
47
46
* A filter for selecting {@code @ExceptionHandler} methods.
48
47
*/
49
- public final static MethodFilter EXCEPTION_HANDLER_METHODS = new MethodFilter () {
48
+ public static final MethodFilter EXCEPTION_HANDLER_METHODS = new MethodFilter () {
50
49
public boolean matches (Method method ) {
51
50
return (AnnotationUtils .findAnnotation (method , ExceptionHandler .class ) != null );
52
51
}
53
52
};
54
53
54
+ /**
55
+ * Arbitrary {@link Method} reference, indicating no method found in the cache.
56
+ */
55
57
private static final Method NO_METHOD_FOUND = ClassUtils .getMethodIfAvailable (System .class , "currentTimeMillis" );
56
58
57
59
@@ -76,8 +78,8 @@ public ExceptionHandlerMethodResolver(Class<?> handlerType) {
76
78
77
79
78
80
/**
79
- * Extract exception mappings from the {@code @ExceptionHandler} annotation
80
- * first and as a fall-back from the method signature.
81
+ * Extract exception mappings from the {@code @ExceptionHandler} annotation first,
82
+ * and then as a fallback from the method signature itself .
81
83
*/
82
84
@ SuppressWarnings ("unchecked" )
83
85
private List <Class <? extends Throwable >> detectExceptionMappings (Method method ) {
@@ -112,36 +114,36 @@ private void addExceptionMapping(Class<? extends Throwable> exceptionType, Metho
112
114
* Whether the contained type has any exception mappings.
113
115
*/
114
116
public boolean hasExceptionMappings () {
115
- return ( this .mappedMethods .size () > 0 );
117
+ return ! this .mappedMethods .isEmpty ( );
116
118
}
117
119
118
120
/**
119
- * Find a method to handle the given exception.
121
+ * Find a {@link Method} to handle the given exception.
120
122
* Use {@link ExceptionDepthComparator} if more than one match is found.
121
123
* @param exception the exception
122
- * @return a method to handle the exception or {@code null}
124
+ * @return a Method to handle the exception, or {@code null} if none found
123
125
*/
124
126
public Method resolveMethod (Exception exception ) {
125
127
return resolveMethodByExceptionType (exception .getClass ());
126
128
}
127
129
128
130
/**
129
- * Find a method to handle the given exception type. This can be useful if
130
- * an Exception instance is not available (example for tools).
131
+ * Find a {@link Method} to handle the given exception type. This can be
132
+ * useful if an {@link Exception} instance is not available (e.g. for tools).
131
133
* @param exceptionType the exception type
132
- * @return a method to handle the exception or {@code null}
134
+ * @return a Method to handle the exception, or {@code null} if none found
133
135
*/
134
136
public Method resolveMethodByExceptionType (Class <? extends Exception > exceptionType ) {
135
137
Method method = this .exceptionLookupCache .get (exceptionType );
136
138
if (method == null ) {
137
139
method = getMappedMethod (exceptionType );
138
- this .exceptionLookupCache .put (exceptionType , method != null ? method : NO_METHOD_FOUND );
140
+ this .exceptionLookupCache .put (exceptionType , ( method != null ? method : NO_METHOD_FOUND ) );
139
141
}
140
- return method != NO_METHOD_FOUND ? method : null ;
142
+ return ( method != NO_METHOD_FOUND ? method : null ) ;
141
143
}
142
144
143
145
/**
144
- * Return the method mapped to the given exception type or {@code null}.
146
+ * Return the {@link Method} mapped to the given exception type, or {@code null} if none .
145
147
*/
146
148
private Method getMappedMethod (Class <? extends Exception > exceptionType ) {
147
149
List <Class <? extends Throwable >> matches = new ArrayList <Class <? extends Throwable >>();
0 commit comments