Skip to content

Commit c9f4883

Browse files
committed
Fine-tuned assertions and related polishing
1 parent e17fc8d commit c9f4883

File tree

13 files changed

+85
-124
lines changed

13 files changed

+85
-124
lines changed

Diff for: spring-expression/src/main/java/org/springframework/expression/spel/standard/Tokenizer.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ else if (isTwoCharToken(TokenKind.SAFE_NAVI)) {
265265
raiseParseException(this.pos, SpelMessage.UNEXPECTED_ESCAPE_CHAR);
266266
break;
267267
default:
268-
throw new IllegalStateException("Cannot handle ('" + ch + "')");
268+
throw new IllegalStateException("Cannot handle (" + (int) ch + ") '" + ch + "'");
269269
}
270270
}
271271
}

Diff for: spring-web/src/main/java/org/springframework/http/HttpHeaders.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,6 @@ public HttpHeaders() {
414414
* Private constructor that can create read-only {@code HttpHeader} instances.
415415
*/
416416
private HttpHeaders(Map<String, List<String>> headers, boolean readOnly) {
417-
Assert.notNull(headers, "'headers' must not be null");
418417
if (readOnly) {
419418
Map<String, List<String>> map = new LinkedCaseInsensitiveMap<>(headers.size(), Locale.ENGLISH);
420419
headers.forEach((key, valueList) -> map.put(key, Collections.unmodifiableList(valueList)));
@@ -1556,6 +1555,7 @@ public String toString() {
15561555
* Return a {@code HttpHeaders} object that can only be read, not written to.
15571556
*/
15581557
public static HttpHeaders readOnlyHttpHeaders(HttpHeaders headers) {
1558+
Assert.notNull(headers, "HttpHeaders must not be null");
15591559
return (headers.readOnly ? headers : new HttpHeaders(headers, true));
15601560
}
15611561

Diff for: spring-webflux/src/main/java/org/springframework/web/reactive/function/client/ClientRequest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ static Builder from(ClientRequest other) {
114114
/**
115115
* Create a builder with the given method and url.
116116
* @param method the HTTP method (GET, POST, etc)
117-
* @param url the URL
117+
* @param url the url (as a URI instance)
118118
* @return the created builder
119119
* @deprecated in favor of {@link #create(HttpMethod, URI)}
120120
*/
@@ -126,7 +126,7 @@ static Builder method(HttpMethod method, URI url) {
126126
/**
127127
* Create a request builder with the given method and url.
128128
* @param method the HTTP method (GET, POST, etc)
129-
* @param url the URL
129+
* @param url the url (as a URI instance)
130130
* @return the created builder
131131
*/
132132
static Builder create(HttpMethod method, URI url) {

Diff for: spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultClientRequestBuilder.java

+20-22
Original file line numberDiff line numberDiff line change
@@ -49,28 +49,30 @@
4949
*/
5050
final class DefaultClientRequestBuilder implements ClientRequest.Builder {
5151

52+
private HttpMethod method;
53+
54+
private URI url;
55+
5256
private final HttpHeaders headers = new HttpHeaders();
5357

5458
private final MultiValueMap<String, String> cookies = new LinkedMultiValueMap<>();
5559

5660
private final Map<String, Object> attributes = new LinkedHashMap<>();
5761

58-
private HttpMethod method;
59-
60-
private URI url;
61-
62-
private BodyInserter<?, ? super ClientHttpRequest> inserter = BodyInserters.empty();
62+
private BodyInserter<?, ? super ClientHttpRequest> body = BodyInserters.empty();
6363

6464

6565
public DefaultClientRequestBuilder(HttpMethod method, URI url) {
66-
method(method);
67-
url(url);
66+
Assert.notNull(method, "HttpMethod must not be null");
67+
Assert.notNull(url, "URI must not be null");
68+
this.method = method;
69+
this.url = url;
6870
}
6971

7072
public DefaultClientRequestBuilder(ClientRequest other) {
7173
Assert.notNull(other, "ClientRequest must not be null");
72-
method(other.method());
73-
url(other.url());
74+
this.method = other.method();
75+
this.url = other.url();
7476
headers(headers -> headers.addAll(other.headers()));
7577
cookies(cookies -> cookies.addAll(other.cookies()));
7678
attributes(attributes -> attributes.putAll(other.attributes()));
@@ -125,7 +127,7 @@ public <S, P extends Publisher<S>> ClientRequest.Builder body(P publisher, Class
125127
Assert.notNull(publisher, "'publisher' must not be null");
126128
Assert.notNull(elementClass, "'elementClass' must not be null");
127129

128-
this.inserter = BodyInserters.fromPublisher(publisher, elementClass);
130+
this.body = BodyInserters.fromPublisher(publisher, elementClass);
129131
return this;
130132
}
131133

@@ -136,7 +138,7 @@ public <S, P extends Publisher<S>> ClientRequest.Builder body(
136138
Assert.notNull(publisher, "'publisher' must not be null");
137139
Assert.notNull(typeReference, "'typeReference' must not be null");
138140

139-
this.inserter = BodyInserters.fromPublisher(publisher, typeReference);
141+
this.body = BodyInserters.fromPublisher(publisher, typeReference);
140142
return this;
141143
}
142144

@@ -154,14 +156,13 @@ public ClientRequest.Builder attributes(Consumer<Map<String, Object>> attributes
154156

155157
@Override
156158
public ClientRequest.Builder body(BodyInserter<?, ? super ClientHttpRequest> inserter) {
157-
this.inserter = inserter;
159+
this.body = inserter;
158160
return this;
159161
}
160162

161163
@Override
162164
public ClientRequest build() {
163-
return new BodyInserterRequest(this.method, this.url, this.headers, this.cookies,
164-
this.inserter, this.attributes);
165+
return new BodyInserterRequest(this.method, this.url, this.headers, this.cookies, this.body, this.attributes);
165166
}
166167

167168

@@ -175,20 +176,19 @@ private static class BodyInserterRequest implements ClientRequest {
175176

176177
private final MultiValueMap<String, String> cookies;
177178

178-
private final BodyInserter<?, ? super ClientHttpRequest> inserter;
179+
private final BodyInserter<?, ? super ClientHttpRequest> body;
179180

180181
private final Map<String, Object> attributes;
181182

182183
public BodyInserterRequest(HttpMethod method, URI url, HttpHeaders headers,
183-
MultiValueMap<String, String> cookies,
184-
BodyInserter<?, ? super ClientHttpRequest> inserter,
184+
MultiValueMap<String, String> cookies, BodyInserter<?, ? super ClientHttpRequest> body,
185185
Map<String, Object> attributes) {
186186

187187
this.method = method;
188188
this.url = url;
189189
this.headers = HttpHeaders.readOnlyHttpHeaders(headers);
190190
this.cookies = CollectionUtils.unmodifiableMultiValueMap(cookies);
191-
this.inserter = inserter;
191+
this.body = body;
192192
this.attributes = Collections.unmodifiableMap(attributes);
193193
}
194194

@@ -214,7 +214,7 @@ public MultiValueMap<String, String> cookies() {
214214

215215
@Override
216216
public BodyInserter<?, ? super ClientHttpRequest> body() {
217-
return this.inserter;
217+
return this.body;
218218
}
219219

220220
@Override
@@ -240,17 +240,15 @@ public Mono<Void> writeTo(ClientHttpRequest request, ExchangeStrategies strategi
240240
}));
241241
}
242242

243-
return this.inserter.insert(request, new BodyInserter.Context() {
243+
return this.body.insert(request, new BodyInserter.Context() {
244244
@Override
245245
public List<HttpMessageWriter<?>> messageWriters() {
246246
return strategies.messageWriters();
247247
}
248-
249248
@Override
250249
public Optional<ServerHttpRequest> serverRequest() {
251250
return Optional.empty();
252251
}
253-
254252
@Override
255253
public Map<String, Object> hints() {
256254
return Collections.emptyMap();

Diff for: spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultClientResponseBuilder.java

+4-6
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,16 @@
4242
*/
4343
final class DefaultClientResponseBuilder implements ClientResponse.Builder {
4444

45+
private ExchangeStrategies strategies;
46+
47+
private HttpStatus statusCode = HttpStatus.OK;
48+
4549
private final HttpHeaders headers = new HttpHeaders();
4650

4751
private final MultiValueMap<String, ResponseCookie> cookies = new LinkedMultiValueMap<>();
4852

49-
private HttpStatus statusCode = HttpStatus.OK;
50-
5153
private Flux<DataBuffer> body = Flux.empty();
5254

53-
private ExchangeStrategies strategies;
54-
5555

5656
public DefaultClientResponseBuilder(ExchangeStrategies strategies) {
5757
Assert.notNull(strategies, "ExchangeStrategies must not be null");
@@ -84,7 +84,6 @@ public ClientResponse.Builder header(String headerName, String... headerValues)
8484

8585
@Override
8686
public ClientResponse.Builder headers(Consumer<HttpHeaders> headersConsumer) {
87-
Assert.notNull(headersConsumer, "Consumer must not be null");
8887
headersConsumer.accept(this.headers);
8988
return this;
9089
}
@@ -99,7 +98,6 @@ public DefaultClientResponseBuilder cookie(String name, String... values) {
9998

10099
@Override
101100
public ClientResponse.Builder cookies(Consumer<MultiValueMap<String, ResponseCookie>> cookiesConsumer) {
102-
Assert.notNull(cookiesConsumer, "Consumer must not be null");
103101
cookiesConsumer.accept(this.cookies);
104102
return this;
105103
}

Diff for: spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultWebClientBuilder.java

+1-3
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,7 @@ public WebClient build() {
208208

209209
private static @Nullable HttpHeaders unmodifiableCopy(@Nullable HttpHeaders original) {
210210
if (original != null) {
211-
HttpHeaders copy = new HttpHeaders();
212-
copy.putAll(original);
213-
return HttpHeaders.readOnlyHttpHeaders(copy);
211+
return HttpHeaders.readOnlyHttpHeaders(original);
214212
}
215213
else {
216214
return null;

Diff for: spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultEntityResponseBuilder.java

-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,6 @@ public EntityResponse.Builder<T> cookie(ResponseCookie cookie) {
9696

9797
@Override
9898
public EntityResponse.Builder<T> cookies(Consumer<MultiValueMap<String, ResponseCookie>> cookiesConsumer) {
99-
Assert.notNull(cookiesConsumer, "Consumer must not be null");
10099
cookiesConsumer.accept(this.cookies);
101100
return this;
102101
}

Diff for: spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultRenderingResponseBuilder.java

-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,6 @@ public RenderingResponse.Builder cookie(ResponseCookie cookie) {
9999

100100
@Override
101101
public RenderingResponse.Builder cookies(Consumer<MultiValueMap<String, ResponseCookie>> cookiesConsumer) {
102-
Assert.notNull(cookiesConsumer, "Consumer must not be null");
103102
cookiesConsumer.accept(this.cookies);
104103
return this;
105104
}

Diff for: spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultServerRequest.java

+1-5
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,10 @@ class DefaultServerRequest implements ServerRequest {
7676

7777
DefaultServerRequest(ServerWebExchange exchange, List<HttpMessageReader<?>> messageReaders) {
7878
this.exchange = exchange;
79-
this.messageReaders = unmodifiableCopy(messageReaders);
79+
this.messageReaders = Collections.unmodifiableList(new ArrayList<>(messageReaders));
8080
this.headers = new DefaultHeaders();
8181
}
8282

83-
private static <T> List<T> unmodifiableCopy(List<? extends T> list) {
84-
return Collections.unmodifiableList(new ArrayList<>(list));
85-
}
86-
8783

8884
@Override
8985
public String methodName() {

0 commit comments

Comments
 (0)