Skip to content

Commit 5da79eb

Browse files
committed
Related polishing
Issue. SPR-11428 (cherry picked from commit 9c6df76)
1 parent bb7a137 commit 5da79eb

File tree

15 files changed

+198
-197
lines changed

15 files changed

+198
-197
lines changed

spring-web/src/main/java/org/springframework/web/method/HandlerMethodSelector.java

Lines changed: 3 additions & 4 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-2014 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.
@@ -37,9 +37,8 @@
3737
public abstract class HandlerMethodSelector {
3838

3939
/**
40-
* Selects handler methods for the given handler type. Callers of this method define handler methods
41-
* of interest through the {@link MethodFilter} parameter.
42-
*
40+
* Select handler methods for the given handler type.
41+
* <p>Callers define handler methods of interest through the {@link MethodFilter} parameter.
4342
* @param handlerType the handler type to search handler methods on
4443
* @param handlerMethodFilter a {@link MethodFilter} to help recognize handler methods of interest
4544
* @return the selected methods, or an empty set

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

Lines changed: 30 additions & 19 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-2014 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.
@@ -43,14 +43,25 @@
4343
*/
4444
public class ExceptionHandlerMethodResolver {
4545

46+
/**
47+
* A filter for selecting {@code @ExceptionHandler} methods.
48+
*/
49+
public final static MethodFilter EXCEPTION_HANDLER_METHODS = new MethodFilter() {
50+
public boolean matches(Method method) {
51+
return (AnnotationUtils.findAnnotation(method, ExceptionHandler.class) != null);
52+
}
53+
};
54+
4655
private static final Method NO_METHOD_FOUND = ClassUtils.getMethodIfAvailable(System.class, "currentTimeMillis");
4756

57+
4858
private final Map<Class<? extends Throwable>, Method> mappedMethods =
4959
new ConcurrentHashMap<Class<? extends Throwable>, Method>(16);
5060

5161
private final Map<Class<? extends Throwable>, Method> exceptionLookupCache =
5262
new ConcurrentHashMap<Class<? extends Throwable>, Method>(16);
5363

64+
5465
/**
5566
* A constructor that finds {@link ExceptionHandler} methods in the given type.
5667
* @param handlerType the type to introspect
@@ -63,30 +74,31 @@ public ExceptionHandlerMethodResolver(Class<?> handlerType) {
6374
}
6475
}
6576

77+
6678
/**
6779
* Extract exception mappings from the {@code @ExceptionHandler} annotation
6880
* first and as a fall-back from the method signature.
6981
*/
7082
@SuppressWarnings("unchecked")
7183
private List<Class<? extends Throwable>> detectExceptionMappings(Method method) {
7284
List<Class<? extends Throwable>> result = new ArrayList<Class<? extends Throwable>>();
73-
74-
ExceptionHandler annotation = AnnotationUtils.findAnnotation(method, ExceptionHandler.class);
75-
result.addAll(Arrays.asList(annotation.value()));
76-
85+
detectAnnotationExceptionMappings(method, result);
7786
if (result.isEmpty()) {
7887
for (Class<?> paramType : method.getParameterTypes()) {
7988
if (Throwable.class.isAssignableFrom(paramType)) {
8089
result.add((Class<? extends Throwable>) paramType);
8190
}
8291
}
8392
}
84-
8593
Assert.notEmpty(result, "No exception types mapped to {" + method + "}");
86-
8794
return result;
8895
}
8996

97+
protected void detectAnnotationExceptionMappings(Method method, List<Class<? extends Throwable>> result) {
98+
ExceptionHandler annot = AnnotationUtils.findAnnotation(method, ExceptionHandler.class);
99+
result.addAll(Arrays.asList(annot.value()));
100+
}
101+
90102
private void addExceptionMapping(Class<? extends Throwable> exceptionType, Method method) {
91103
Method oldMethod = this.mappedMethods.put(exceptionType, method);
92104
if (oldMethod != null && !oldMethod.equals(method)) {
@@ -110,7 +122,16 @@ public boolean hasExceptionMappings() {
110122
* @return a method to handle the exception or {@code null}
111123
*/
112124
public Method resolveMethod(Exception exception) {
113-
Class<? extends Exception> exceptionType = exception.getClass();
125+
return resolveMethodByExceptionType(exception.getClass());
126+
}
127+
128+
/**
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+
* @param exceptionType the exception type
132+
* @return a method to handle the exception or {@code null}
133+
*/
134+
public Method resolveMethodByExceptionType(Class<? extends Exception> exceptionType) {
114135
Method method = this.exceptionLookupCache.get(exceptionType);
115136
if (method == null) {
116137
method = getMappedMethod(exceptionType);
@@ -131,21 +152,11 @@ private Method getMappedMethod(Class<? extends Exception> exceptionType) {
131152
}
132153
if (!matches.isEmpty()) {
133154
Collections.sort(matches, new ExceptionDepthComparator(exceptionType));
134-
return mappedMethods.get(matches.get(0));
155+
return this.mappedMethods.get(matches.get(0));
135156
}
136157
else {
137158
return null;
138159
}
139160
}
140161

141-
/**
142-
* A filter for selecting {@code @ExceptionHandler} methods.
143-
*/
144-
public final static MethodFilter EXCEPTION_HANDLER_METHODS = new MethodFilter() {
145-
146-
public boolean matches(Method method) {
147-
return AnnotationUtils.findAnnotation(method, ExceptionHandler.class) != null;
148-
}
149-
};
150-
151162
}

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/AbstractRequestCondition.java

Lines changed: 13 additions & 12 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-2014 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.
@@ -28,20 +28,13 @@
2828
*/
2929
public abstract class AbstractRequestCondition<T extends AbstractRequestCondition<T>> implements RequestCondition<T> {
3030

31-
/**
32-
* Return the discrete items a request condition is composed of.
33-
* For example URL patterns, HTTP request methods, param expressions, etc.
34-
* @return a collection of objects, never {@code null}
35-
*/
36-
protected abstract Collection<?> getContent();
37-
3831
@Override
39-
public boolean equals(Object o) {
40-
if (this == o) {
32+
public boolean equals(Object obj) {
33+
if (this == obj) {
4134
return true;
4235
}
43-
if (o != null && getClass().equals(o.getClass())) {
44-
AbstractRequestCondition<?> other = (AbstractRequestCondition<?>) o;
36+
if (obj != null && getClass().equals(obj.getClass())) {
37+
AbstractRequestCondition<?> other = (AbstractRequestCondition<?>) obj;
4538
return getContent().equals(other.getContent());
4639
}
4740
return false;
@@ -66,6 +59,14 @@ public String toString() {
6659
return builder.toString();
6760
}
6861

62+
63+
/**
64+
* Return the discrete items a request condition is composed of.
65+
* For example URL patterns, HTTP request methods, param expressions, etc.
66+
* @return a collection of objects, never {@code null}
67+
*/
68+
protected abstract Collection<?> getContent();
69+
6970
/**
7071
* The notation to use when printing discrete items of content.
7172
* For example " || " for URL patterns or " && " for param expressions.

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/CompositeRequestCondition.java

Lines changed: 10 additions & 8 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-2014 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.
@@ -20,7 +20,6 @@
2020
import java.util.Collection;
2121
import java.util.Collections;
2222
import java.util.List;
23-
2423
import javax.servlet.http.HttpServletRequest;
2524

2625
import org.springframework.util.Assert;
@@ -43,6 +42,7 @@ public class CompositeRequestCondition extends AbstractRequestCondition<Composit
4342

4443
private final RequestConditionHolder[] requestConditions;
4544

45+
4646
/**
4747
* Create an instance with 0 or more {@code RequestCondition} types. It is
4848
* important to create {@code CompositeRequestCondition} instances with the
@@ -53,6 +53,11 @@ public CompositeRequestCondition(RequestCondition<?>... requestConditions) {
5353
this.requestConditions = wrap(requestConditions);
5454
}
5555

56+
private CompositeRequestCondition(RequestConditionHolder[] requestConditions) {
57+
this.requestConditions = requestConditions;
58+
}
59+
60+
5661
private RequestConditionHolder[] wrap(RequestCondition<?>... rawConditions) {
5762
RequestConditionHolder[] wrappedConditions = new RequestConditionHolder[rawConditions.length];
5863
for (int i = 0; i < rawConditions.length; i++) {
@@ -61,10 +66,6 @@ private RequestConditionHolder[] wrap(RequestCondition<?>... rawConditions) {
6166
return wrappedConditions;
6267
}
6368

64-
private CompositeRequestCondition(RequestConditionHolder[] requestConditions) {
65-
this.requestConditions = requestConditions;
66-
}
67-
6869
/**
6970
* Whether this instance contains 0 conditions or not.
7071
*/
@@ -128,8 +129,9 @@ else if (isEmpty()) {
128129

129130
private void assertNumberOfConditions(CompositeRequestCondition other) {
130131
Assert.isTrue(getLength() == other.getLength(),
131-
"Cannot combine CompositeRequestConditions with a different number of conditions. "
132-
+ this.requestConditions + " and " + other.requestConditions);
132+
"Cannot combine CompositeRequestConditions with a different number of conditions. " +
133+
ObjectUtils.nullSafeToString(this.requestConditions) + " and " +
134+
ObjectUtils.nullSafeToString(other.requestConditions));
133135
}
134136

135137
/**

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/ConsumesRequestCondition.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2014 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.
@@ -23,7 +23,6 @@
2323
import java.util.LinkedHashSet;
2424
import java.util.List;
2525
import java.util.Set;
26-
2726
import javax.servlet.http.HttpServletRequest;
2827

2928
import org.springframework.http.MediaType;
@@ -48,11 +47,12 @@ public final class ConsumesRequestCondition extends AbstractRequestCondition<Con
4847

4948
private final List<ConsumeMediaTypeExpression> expressions;
5049

50+
5151
/**
5252
* Creates a new instance from 0 or more "consumes" expressions.
5353
* @param consumes expressions with the syntax described in
5454
* {@link RequestMapping#consumes()}; if 0 expressions are provided,
55-
* the condition will match to every request.
55+
* the condition will match to every request
5656
*/
5757
public ConsumesRequestCondition(String... consumes) {
5858
this(consumes, null);
@@ -78,6 +78,7 @@ private ConsumesRequestCondition(Collection<ConsumeMediaTypeExpression> expressi
7878
Collections.sort(this.expressions);
7979
}
8080

81+
8182
private static Set<ConsumeMediaTypeExpression> parseExpressions(String[] consumes, String[] headers) {
8283
Set<ConsumeMediaTypeExpression> result = new LinkedHashSet<ConsumeMediaTypeExpression>();
8384
if (headers != null) {
@@ -98,6 +99,7 @@ private static Set<ConsumeMediaTypeExpression> parseExpressions(String[] consume
9899
return result;
99100
}
100101

102+
101103
/**
102104
* Return the contained MediaType expressions.
103105
*/
@@ -149,12 +151,10 @@ public ConsumesRequestCondition combine(ConsumesRequestCondition other) {
149151
* request 'Content-Type' header and returns an instance that is guaranteed
150152
* to contain matching expressions only. The match is performed via
151153
* {@link MediaType#includes(MediaType)}.
152-
*
153154
* @param request the current request
154-
*
155155
* @return the same instance if the condition contains no expressions;
156-
* or a new condition with matching expressions only;
157-
* or {@code null} if no expressions match.
156+
* or a new condition with matching expressions only;
157+
* or {@code null} if no expressions match.
158158
*/
159159
public ConsumesRequestCondition getMatchingCondition(HttpServletRequest request) {
160160
if (isEmpty()) {
@@ -173,11 +173,10 @@ public ConsumesRequestCondition getMatchingCondition(HttpServletRequest request)
173173
/**
174174
* Returns:
175175
* <ul>
176-
* <li>0 if the two conditions have the same number of expressions
177-
* <li>Less than 0 if "this" has more or more specific media type expressions
178-
* <li>Greater than 0 if "other" has more or more specific media type expressions
176+
* <li>0 if the two conditions have the same number of expressions
177+
* <li>Less than 0 if "this" has more or more specific media type expressions
178+
* <li>Greater than 0 if "other" has more or more specific media type expressions
179179
* </ul>
180-
*
181180
* <p>It is assumed that both instances have been obtained via
182181
* {@link #getMatchingCondition(HttpServletRequest)} and each instance contains
183182
* the matching consumable media type expression only or is otherwise empty.
@@ -197,6 +196,7 @@ else if (other.expressions.isEmpty()) {
197196
}
198197
}
199198

199+
200200
/**
201201
* Parses and matches a single media type expression to a request's 'Content-Type' header.
202202
*/

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/condition/HeadersRequestCondition.java

Lines changed: 9 additions & 7 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-2014 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.
@@ -20,7 +20,6 @@
2020
import java.util.Collections;
2121
import java.util.LinkedHashSet;
2222
import java.util.Set;
23-
2423
import javax.servlet.http.HttpServletRequest;
2524

2625
import org.springframework.web.bind.annotation.RequestMapping;
@@ -41,12 +40,13 @@ public final class HeadersRequestCondition extends AbstractRequestCondition<Head
4140

4241
private final Set<HeaderExpression> expressions;
4342

43+
4444
/**
4545
* Create a new instance from the given header expressions. Expressions with
4646
* header names 'Accept' or 'Content-Type' are ignored. See {@link ConsumesRequestCondition}
4747
* and {@link ProducesRequestCondition} for those.
4848
* @param headers media type expressions with syntax defined in {@link RequestMapping#headers()};
49-
* if 0, the condition will match to every request.
49+
* if 0, the condition will match to every request
5050
*/
5151
public HeadersRequestCondition(String... headers) {
5252
this(parseExpressions(headers));
@@ -56,6 +56,7 @@ private HeadersRequestCondition(Collection<HeaderExpression> conditions) {
5656
this.expressions = Collections.unmodifiableSet(new LinkedHashSet<HeaderExpression>(conditions));
5757
}
5858

59+
5960
private static Collection<HeaderExpression> parseExpressions(String... headers) {
6061
Set<HeaderExpression> expressions = new LinkedHashSet<HeaderExpression>();
6162
if (headers != null) {
@@ -113,11 +114,10 @@ public HeadersRequestCondition getMatchingCondition(HttpServletRequest request)
113114
/**
114115
* Returns:
115116
* <ul>
116-
* <li>0 if the two conditions have the same number of header expressions
117-
* <li>Less than 0 if "this" instance has more header expressions
118-
* <li>Greater than 0 if the "other" instance has more header expressions
117+
* <li>0 if the two conditions have the same number of header expressions
118+
* <li>Less than 0 if "this" instance has more header expressions
119+
* <li>Greater than 0 if the "other" instance has more header expressions
119120
* </ul>
120-
*
121121
* <p>It is assumed that both instances have been obtained via
122122
* {@link #getMatchingCondition(HttpServletRequest)} and each instance
123123
* contains the matching header expression only or is otherwise empty.
@@ -126,6 +126,7 @@ public int compareTo(HeadersRequestCondition other, HttpServletRequest request)
126126
return other.expressions.size() - this.expressions.size();
127127
}
128128

129+
129130
/**
130131
* Parses and matches a single header expression to a request.
131132
*/
@@ -158,4 +159,5 @@ public int hashCode() {
158159
return result;
159160
}
160161
}
162+
161163
}

0 commit comments

Comments
 (0)