Skip to content

Commit 9011ce9

Browse files
committed
Apply "instanceof pattern matching" in remainder of spring-web module
See gh-30067
1 parent 29fe0a3 commit 9011ce9

File tree

13 files changed

+45
-65
lines changed

13 files changed

+45
-65
lines changed

spring-web/src/main/java/org/springframework/http/DefaultHttpStatusCode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public int hashCode() {
9090

9191
@Override
9292
public boolean equals(Object obj) {
93-
return (obj instanceof HttpStatusCode other && this.value == other.value());
93+
return (this == obj) || (obj instanceof HttpStatusCode that && this.value == that.value());
9494
}
9595

9696
@Override

spring-web/src/main/java/org/springframework/http/HttpMethod.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -170,14 +170,8 @@ public int hashCode() {
170170
}
171171

172172
@Override
173-
public boolean equals(Object o) {
174-
if (this == o) {
175-
return true;
176-
}
177-
else if (o instanceof HttpMethod otherMethod) {
178-
return this.name.equals(otherMethod.name);
179-
}
180-
return false;
173+
public boolean equals(Object obj) {
174+
return (this == obj) || (obj instanceof HttpMethod that && this.name.equals(that.name));
181175
}
182176

183177
@Override

spring-web/src/main/java/org/springframework/http/client/SimpleClientHttpRequestFactory.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2023 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.
@@ -149,11 +149,11 @@ public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IO
149149
*/
150150
protected HttpURLConnection openConnection(URL url, @Nullable Proxy proxy) throws IOException {
151151
URLConnection urlConnection = (proxy != null ? url.openConnection(proxy) : url.openConnection());
152-
if (!(urlConnection instanceof HttpURLConnection)) {
152+
if (!(urlConnection instanceof HttpURLConnection httpUrlConnection)) {
153153
throw new IllegalStateException(
154154
"HttpURLConnection required for [" + url + "] but got: " + urlConnection);
155155
}
156-
return (HttpURLConnection) urlConnection;
156+
return httpUrlConnection;
157157
}
158158

159159
/**

spring-web/src/main/java/org/springframework/http/codec/json/Jackson2Tokenizer.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,9 +251,9 @@ public static Flux<TokenBuffer> tokenize(Flux<DataBuffer> dataBuffers, JsonFacto
251251
parser = jsonFactory.createNonBlockingByteBufferParser();
252252
}
253253
DeserializationContext context = objectMapper.getDeserializationContext();
254-
if (context instanceof DefaultDeserializationContext) {
255-
context = ((DefaultDeserializationContext) context).createInstance(
256-
objectMapper.getDeserializationConfig(), parser, objectMapper.getInjectableValues());
254+
if (context instanceof DefaultDeserializationContext ddc) {
255+
context = ddc.createInstance(objectMapper.getDeserializationConfig(),
256+
parser, objectMapper.getInjectableValues());
257257
}
258258
Jackson2Tokenizer tokenizer =
259259
new Jackson2Tokenizer(parser, context, tokenizeArrays, forceUseOfBigDecimal, maxInMemorySize);

spring-web/src/main/java/org/springframework/http/codec/multipart/MultipartHttpMessageWriter.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 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.
@@ -241,20 +241,19 @@ private Flux<DataBuffer> encodePartValues(
241241
.concatMap(value -> encodePart(boundary, name, value, bufferFactory));
242242
}
243243

244-
@SuppressWarnings("unchecked")
244+
@SuppressWarnings({ "unchecked", "rawtypes" })
245245
private <T> Flux<DataBuffer> encodePart(byte[] boundary, String name, T value, DataBufferFactory factory) {
246246
MultipartHttpOutputMessage message = new MultipartHttpOutputMessage(factory);
247247
HttpHeaders headers = message.getHeaders();
248248

249249
T body;
250250
ResolvableType resolvableType = null;
251-
if (value instanceof HttpEntity) {
252-
HttpEntity<T> httpEntity = (HttpEntity<T>) value;
251+
if (value instanceof HttpEntity httpEntity) {
253252
headers.putAll(httpEntity.getHeaders());
254-
body = httpEntity.getBody();
253+
body = (T) httpEntity.getBody();
255254
Assert.state(body != null, "MultipartHttpMessageWriter only supports HttpEntity with body");
256-
if (httpEntity instanceof ResolvableTypeProvider) {
257-
resolvableType = ((ResolvableTypeProvider) httpEntity).getResolvableType();
255+
if (httpEntity instanceof ResolvableTypeProvider resolvableTypeProvider) {
256+
resolvableType = resolvableTypeProvider.getResolvableType();
258257
}
259258
}
260259
else {
@@ -265,8 +264,8 @@ private <T> Flux<DataBuffer> encodePart(byte[] boundary, String name, T value, D
265264
}
266265

267266
if (!headers.containsKey(HttpHeaders.CONTENT_DISPOSITION)) {
268-
if (body instanceof Resource) {
269-
headers.setContentDispositionFormData(name, ((Resource) body).getFilename());
267+
if (body instanceof Resource resource) {
268+
headers.setContentDispositionFormData(name, resource.getFilename());
270269
}
271270
else if (resolvableType.resolve() == Resource.class) {
272271
body = (T) Mono.from((Publisher<?>) body).doOnNext(o -> headers
@@ -288,8 +287,7 @@ else if (resolvableType.resolve() == Resource.class) {
288287
return Flux.error(new CodecException("No suitable writer found for part: " + name));
289288
}
290289

291-
Publisher<T> bodyPublisher =
292-
body instanceof Publisher ? (Publisher<T>) body : Mono.just(body);
290+
Publisher<T> bodyPublisher = (body instanceof Publisher publisher ? publisher : Mono.just(body));
293291

294292
// The writer will call MultipartHttpOutputMessage#write which doesn't actually write
295293
// but only stores the body Flux and returns Mono.empty().

spring-web/src/main/java/org/springframework/http/server/DefaultPathContainer.java

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2023 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.
@@ -74,14 +74,8 @@ public List<Element> elements() {
7474

7575

7676
@Override
77-
public boolean equals(@Nullable Object other) {
78-
if (this == other) {
79-
return true;
80-
}
81-
if (!(other instanceof PathContainer)) {
82-
return false;
83-
}
84-
return value().equals(((PathContainer) other).value());
77+
public boolean equals(@Nullable Object obj) {
78+
return (this == obj) || (obj instanceof PathContainer that && value().equals(that.value()));
8579
}
8680

8781
@Override
@@ -287,14 +281,8 @@ public MultiValueMap<String, String> parameters() {
287281
}
288282

289283
@Override
290-
public boolean equals(@Nullable Object other) {
291-
if (this == other) {
292-
return true;
293-
}
294-
if (!(other instanceof PathSegment)) {
295-
return false;
296-
}
297-
return value().equals(((PathSegment) other).value());
284+
public boolean equals(@Nullable Object obj) {
285+
return (this == obj) || (obj instanceof PathSegment that && value().equals(that.value()));
298286
}
299287

300288
@Override

spring-web/src/main/java/org/springframework/web/client/RestTemplate.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1006,7 +1006,7 @@ else if (converter instanceof GenericHttpMessageConverter<?> genericConverter) {
10061006

10071007
private Stream<MediaType> getSupportedMediaTypes(Type type, HttpMessageConverter<?> converter) {
10081008
Type rawType = (type instanceof ParameterizedType parameterizedType ? parameterizedType.getRawType() : type);
1009-
Class<?> clazz = (rawType instanceof Class ? (Class<?>) rawType : null);
1009+
Class<?> clazz = (rawType instanceof Class<?> rawClass ? rawClass : null);
10101010
return (clazz != null ? converter.getSupportedMediaTypes(clazz) : converter.getSupportedMediaTypes())
10111011
.stream()
10121012
.map(mediaType -> {

spring-web/src/main/java/org/springframework/web/context/request/async/WebAsyncUtils.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2023 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.
@@ -46,8 +46,8 @@ public abstract class WebAsyncUtils {
4646
public static WebAsyncManager getAsyncManager(ServletRequest servletRequest) {
4747
WebAsyncManager asyncManager = null;
4848
Object asyncManagerAttr = servletRequest.getAttribute(WEB_ASYNC_MANAGER_ATTRIBUTE);
49-
if (asyncManagerAttr instanceof WebAsyncManager) {
50-
asyncManager = (WebAsyncManager) asyncManagerAttr;
49+
if (asyncManagerAttr instanceof WebAsyncManager wam) {
50+
asyncManager = wam;
5151
}
5252
if (asyncManager == null) {
5353
asyncManager = new WebAsyncManager();
@@ -64,8 +64,8 @@ public static WebAsyncManager getAsyncManager(WebRequest webRequest) {
6464
int scope = RequestAttributes.SCOPE_REQUEST;
6565
WebAsyncManager asyncManager = null;
6666
Object asyncManagerAttr = webRequest.getAttribute(WEB_ASYNC_MANAGER_ATTRIBUTE, scope);
67-
if (asyncManagerAttr instanceof WebAsyncManager) {
68-
asyncManager = (WebAsyncManager) asyncManagerAttr;
67+
if (asyncManagerAttr instanceof WebAsyncManager wam) {
68+
asyncManager = wam;
6969
}
7070
if (asyncManager == null) {
7171
asyncManager = new WebAsyncManager();

spring-web/src/main/java/org/springframework/web/multipart/support/MultipartFilter.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2021 the original author or authors.
2+
* Copyright 2002-2023 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.
@@ -125,8 +125,8 @@ protected void doFilterInternal(
125125
filterChain.doFilter(processedRequest, response);
126126
}
127127
finally {
128-
if (processedRequest instanceof MultipartHttpServletRequest) {
129-
multipartResolver.cleanupMultipart((MultipartHttpServletRequest) processedRequest);
128+
if (processedRequest instanceof MultipartHttpServletRequest multipartRequest) {
129+
multipartResolver.cleanupMultipart(multipartRequest);
130130
}
131131
}
132132
}

spring-web/src/main/java/org/springframework/web/util/pattern/LiteralPathElement.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 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.
@@ -16,7 +16,6 @@
1616

1717
package org.springframework.web.util.pattern;
1818

19-
import org.springframework.http.server.PathContainer;
2019
import org.springframework.http.server.PathContainer.Element;
2120
import org.springframework.http.server.PathContainer.PathSegment;
2221
import org.springframework.web.util.pattern.PathPattern.MatchingContext;
@@ -61,10 +60,10 @@ public boolean matches(int pathIndex, MatchingContext matchingContext) {
6160
return false;
6261
}
6362
Element element = matchingContext.pathElements.get(pathIndex);
64-
if (!(element instanceof PathContainer.PathSegment)) {
63+
if (!(element instanceof PathSegment pathSegment)) {
6564
return false;
6665
}
67-
String value = ((PathSegment)element).valueToMatch();
66+
String value = pathSegment.valueToMatch();
6867
if (value.length() != this.len) {
6968
// Not enough data to match this path element
7069
return false;

0 commit comments

Comments
 (0)