Skip to content

Commit 9c6df76

Browse files
committed
Related polishing
Issue. SPR-11428
1 parent f913940 commit 9c6df76

File tree

15 files changed

+176
-186
lines changed

15 files changed

+176
-186
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: 13 additions & 15 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+
/** A filter for selecting annotated exception handling methods. */
47+
public final static MethodFilter EXCEPTION_HANDLER_METHODS = new MethodFilter() {
48+
49+
@Override
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,26 +74,23 @@ 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-
7485
detectAnnotationExceptionMappings(method, result);
75-
7686
if (result.isEmpty()) {
7787
for (Class<?> paramType : method.getParameterTypes()) {
7888
if (Throwable.class.isAssignableFrom(paramType)) {
7989
result.add((Class<? extends Throwable>) paramType);
8090
}
8191
}
8292
}
83-
8493
Assert.notEmpty(result, "No exception types mapped to {" + method + "}");
85-
8694
return result;
8795
}
8896

@@ -151,14 +159,4 @@ private Method getMappedMethod(Class<? extends Exception> exceptionType) {
151159
}
152160
}
153161

154-
155-
/** A filter for selecting annotated exception handling methods. */
156-
public final static MethodFilter EXCEPTION_HANDLER_METHODS = new MethodFilter() {
157-
158-
@Override
159-
public boolean matches(Method method) {
160-
return AnnotationUtils.findAnnotation(method, ExceptionHandler.class) != null;
161-
}
162-
};
163-
164162
}

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
*/
@@ -129,8 +130,9 @@ else if (isEmpty()) {
129130

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

136138
/**

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-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.
@@ -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.InvalidMediaTypeException;
@@ -49,11 +48,12 @@ public final class ConsumesRequestCondition extends AbstractRequestCondition<Con
4948

5049
private final List<ConsumeMediaTypeExpression> expressions;
5150

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

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

103+
102104
/**
103105
* Return the contained MediaType expressions.
104106
*/
@@ -151,12 +153,10 @@ public ConsumesRequestCondition combine(ConsumesRequestCondition other) {
151153
* request 'Content-Type' header and returns an instance that is guaranteed
152154
* to contain matching expressions only. The match is performed via
153155
* {@link MediaType#includes(MediaType)}.
154-
*
155156
* @param request the current request
156-
*
157157
* @return the same instance if the condition contains no expressions;
158-
* or a new condition with matching expressions only;
159-
* or {@code null} if no expressions match.
158+
* or a new condition with matching expressions only;
159+
* or {@code null} if no expressions match.
160160
*/
161161
@Override
162162
public ConsumesRequestCondition getMatchingCondition(HttpServletRequest request) {
@@ -176,11 +176,10 @@ public ConsumesRequestCondition getMatchingCondition(HttpServletRequest request)
176176
/**
177177
* Returns:
178178
* <ul>
179-
* <li>0 if the two conditions have the same number of expressions
180-
* <li>Less than 0 if "this" has more or more specific media type expressions
181-
* <li>Greater than 0 if "other" has more or more specific media type expressions
179+
* <li>0 if the two conditions have the same number of expressions
180+
* <li>Less than 0 if "this" has more or more specific media type expressions
181+
* <li>Greater than 0 if "other" has more or more specific media type expressions
182182
* </ul>
183-
*
184183
* <p>It is assumed that both instances have been obtained via
185184
* {@link #getMatchingCondition(HttpServletRequest)} and each instance contains
186185
* the matching consumable media type expression only or is otherwise empty.
@@ -201,6 +200,7 @@ else if (other.expressions.isEmpty()) {
201200
}
202201
}
203202

203+
204204
/**
205205
* Parses and matches a single media type expression to a request's 'Content-Type' header.
206206
*/

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) {
@@ -115,11 +116,10 @@ public HeadersRequestCondition getMatchingCondition(HttpServletRequest request)
115116
/**
116117
* Returns:
117118
* <ul>
118-
* <li>0 if the two conditions have the same number of header expressions
119-
* <li>Less than 0 if "this" instance has more header expressions
120-
* <li>Greater than 0 if the "other" instance has more header expressions
119+
* <li>0 if the two conditions have the same number of header expressions
120+
* <li>Less than 0 if "this" instance has more header expressions
121+
* <li>Greater than 0 if the "other" instance has more header expressions
121122
* </ul>
122-
*
123123
* <p>It is assumed that both instances have been obtained via
124124
* {@link #getMatchingCondition(HttpServletRequest)} and each instance
125125
* contains the matching header expression only or is otherwise empty.
@@ -129,6 +129,7 @@ public int compareTo(HeadersRequestCondition other, HttpServletRequest request)
129129
return other.expressions.size() - this.expressions.size();
130130
}
131131

132+
132133
/**
133134
* Parses and matches a single header expression to a request.
134135
*/
@@ -161,4 +162,5 @@ public int hashCode() {
161162
return result;
162163
}
163164
}
165+
164166
}

0 commit comments

Comments
 (0)