Apply "instanceof pattern matching" in remainder of spring-web module

See gh-30067
This commit is contained in:
Sam Brannen
2023-03-07 14:33:13 +01:00
parent 29fe0a3c6a
commit 9011ce9c68
13 changed files with 45 additions and 65 deletions

View File

@@ -90,7 +90,7 @@ final class DefaultHttpStatusCode
@Override
public boolean equals(Object obj) {
return (obj instanceof HttpStatusCode other && this.value == other.value());
return (this == obj) || (obj instanceof HttpStatusCode that && this.value == that.value());
}
@Override

View File

@@ -170,14 +170,8 @@ public final class HttpMethod implements Comparable<HttpMethod>, Serializable {
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
else if (o instanceof HttpMethod otherMethod) {
return this.name.equals(otherMethod.name);
}
return false;
public boolean equals(Object obj) {
return (this == obj) || (obj instanceof HttpMethod that && this.name.equals(that.name));
}
@Override

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -149,11 +149,11 @@ public class SimpleClientHttpRequestFactory implements ClientHttpRequestFactory
*/
protected HttpURLConnection openConnection(URL url, @Nullable Proxy proxy) throws IOException {
URLConnection urlConnection = (proxy != null ? url.openConnection(proxy) : url.openConnection());
if (!(urlConnection instanceof HttpURLConnection)) {
if (!(urlConnection instanceof HttpURLConnection httpUrlConnection)) {
throw new IllegalStateException(
"HttpURLConnection required for [" + url + "] but got: " + urlConnection);
}
return (HttpURLConnection) urlConnection;
return httpUrlConnection;
}
/**

View File

@@ -251,9 +251,9 @@ final class Jackson2Tokenizer {
parser = jsonFactory.createNonBlockingByteBufferParser();
}
DeserializationContext context = objectMapper.getDeserializationContext();
if (context instanceof DefaultDeserializationContext) {
context = ((DefaultDeserializationContext) context).createInstance(
objectMapper.getDeserializationConfig(), parser, objectMapper.getInjectableValues());
if (context instanceof DefaultDeserializationContext ddc) {
context = ddc.createInstance(objectMapper.getDeserializationConfig(),
parser, objectMapper.getInjectableValues());
}
Jackson2Tokenizer tokenizer =
new Jackson2Tokenizer(parser, context, tokenizeArrays, forceUseOfBigDecimal, maxInMemorySize);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -241,20 +241,19 @@ public class MultipartHttpMessageWriter extends MultipartWriterSupport
.concatMap(value -> encodePart(boundary, name, value, bufferFactory));
}
@SuppressWarnings("unchecked")
@SuppressWarnings({ "unchecked", "rawtypes" })
private <T> Flux<DataBuffer> encodePart(byte[] boundary, String name, T value, DataBufferFactory factory) {
MultipartHttpOutputMessage message = new MultipartHttpOutputMessage(factory);
HttpHeaders headers = message.getHeaders();
T body;
ResolvableType resolvableType = null;
if (value instanceof HttpEntity) {
HttpEntity<T> httpEntity = (HttpEntity<T>) value;
if (value instanceof HttpEntity httpEntity) {
headers.putAll(httpEntity.getHeaders());
body = httpEntity.getBody();
body = (T) httpEntity.getBody();
Assert.state(body != null, "MultipartHttpMessageWriter only supports HttpEntity with body");
if (httpEntity instanceof ResolvableTypeProvider) {
resolvableType = ((ResolvableTypeProvider) httpEntity).getResolvableType();
if (httpEntity instanceof ResolvableTypeProvider resolvableTypeProvider) {
resolvableType = resolvableTypeProvider.getResolvableType();
}
}
else {
@@ -265,8 +264,8 @@ public class MultipartHttpMessageWriter extends MultipartWriterSupport
}
if (!headers.containsKey(HttpHeaders.CONTENT_DISPOSITION)) {
if (body instanceof Resource) {
headers.setContentDispositionFormData(name, ((Resource) body).getFilename());
if (body instanceof Resource resource) {
headers.setContentDispositionFormData(name, resource.getFilename());
}
else if (resolvableType.resolve() == Resource.class) {
body = (T) Mono.from((Publisher<?>) body).doOnNext(o -> headers
@@ -288,8 +287,7 @@ public class MultipartHttpMessageWriter extends MultipartWriterSupport
return Flux.error(new CodecException("No suitable writer found for part: " + name));
}
Publisher<T> bodyPublisher =
body instanceof Publisher ? (Publisher<T>) body : Mono.just(body);
Publisher<T> bodyPublisher = (body instanceof Publisher publisher ? publisher : Mono.just(body));
// The writer will call MultipartHttpOutputMessage#write which doesn't actually write
// but only stores the body Flux and returns Mono.empty().

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -74,14 +74,8 @@ final class DefaultPathContainer implements PathContainer {
@Override
public boolean equals(@Nullable Object other) {
if (this == other) {
return true;
}
if (!(other instanceof PathContainer)) {
return false;
}
return value().equals(((PathContainer) other).value());
public boolean equals(@Nullable Object obj) {
return (this == obj) || (obj instanceof PathContainer that && value().equals(that.value()));
}
@Override
@@ -287,14 +281,8 @@ final class DefaultPathContainer implements PathContainer {
}
@Override
public boolean equals(@Nullable Object other) {
if (this == other) {
return true;
}
if (!(other instanceof PathSegment)) {
return false;
}
return value().equals(((PathSegment) other).value());
public boolean equals(@Nullable Object obj) {
return (this == obj) || (obj instanceof PathSegment that && value().equals(that.value()));
}
@Override

View File

@@ -1006,7 +1006,7 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat
private Stream<MediaType> getSupportedMediaTypes(Type type, HttpMessageConverter<?> converter) {
Type rawType = (type instanceof ParameterizedType parameterizedType ? parameterizedType.getRawType() : type);
Class<?> clazz = (rawType instanceof Class ? (Class<?>) rawType : null);
Class<?> clazz = (rawType instanceof Class<?> rawClass ? rawClass : null);
return (clazz != null ? converter.getSupportedMediaTypes(clazz) : converter.getSupportedMediaTypes())
.stream()
.map(mediaType -> {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -46,8 +46,8 @@ public abstract class WebAsyncUtils {
public static WebAsyncManager getAsyncManager(ServletRequest servletRequest) {
WebAsyncManager asyncManager = null;
Object asyncManagerAttr = servletRequest.getAttribute(WEB_ASYNC_MANAGER_ATTRIBUTE);
if (asyncManagerAttr instanceof WebAsyncManager) {
asyncManager = (WebAsyncManager) asyncManagerAttr;
if (asyncManagerAttr instanceof WebAsyncManager wam) {
asyncManager = wam;
}
if (asyncManager == null) {
asyncManager = new WebAsyncManager();
@@ -64,8 +64,8 @@ public abstract class WebAsyncUtils {
int scope = RequestAttributes.SCOPE_REQUEST;
WebAsyncManager asyncManager = null;
Object asyncManagerAttr = webRequest.getAttribute(WEB_ASYNC_MANAGER_ATTRIBUTE, scope);
if (asyncManagerAttr instanceof WebAsyncManager) {
asyncManager = (WebAsyncManager) asyncManagerAttr;
if (asyncManagerAttr instanceof WebAsyncManager wam) {
asyncManager = wam;
}
if (asyncManager == null) {
asyncManager = new WebAsyncManager();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -125,8 +125,8 @@ public class MultipartFilter extends OncePerRequestFilter {
filterChain.doFilter(processedRequest, response);
}
finally {
if (processedRequest instanceof MultipartHttpServletRequest) {
multipartResolver.cleanupMultipart((MultipartHttpServletRequest) processedRequest);
if (processedRequest instanceof MultipartHttpServletRequest multipartRequest) {
multipartResolver.cleanupMultipart(multipartRequest);
}
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,7 +16,6 @@
package org.springframework.web.util.pattern;
import org.springframework.http.server.PathContainer;
import org.springframework.http.server.PathContainer.Element;
import org.springframework.http.server.PathContainer.PathSegment;
import org.springframework.web.util.pattern.PathPattern.MatchingContext;
@@ -61,10 +60,10 @@ class LiteralPathElement extends PathElement {
return false;
}
Element element = matchingContext.pathElements.get(pathIndex);
if (!(element instanceof PathContainer.PathSegment)) {
if (!(element instanceof PathSegment pathSegment)) {
return false;
}
String value = ((PathSegment)element).valueToMatch();
String value = pathSegment.valueToMatch();
if (value.length() != this.len) {
// Not enough data to match this path element
return false;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -25,6 +25,7 @@ import java.util.StringJoiner;
import org.springframework.http.server.PathContainer;
import org.springframework.http.server.PathContainer.Element;
import org.springframework.http.server.PathContainer.PathSegment;
import org.springframework.http.server.PathContainer.Separator;
import org.springframework.lang.Nullable;
import org.springframework.util.CollectionUtils;
@@ -726,7 +727,7 @@ public class PathPattern implements Comparable<PathPattern> {
*/
String pathElementValue(int pathIndex) {
Element element = (pathIndex < this.pathLength) ? this.pathElements.get(pathIndex) : null;
return (element instanceof PathContainer.PathSegment pathSegment ? pathSegment.valueToMatch() : "");
return (element instanceof PathSegment pathSegment ? pathSegment.valueToMatch() : "");
}
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -65,10 +65,10 @@ class SingleCharWildcardedPathElement extends PathElement {
}
Element element = matchingContext.pathElements.get(pathIndex);
if (!(element instanceof PathSegment)) {
if (!(element instanceof PathSegment pathSegment)) {
return false;
}
String value = ((PathSegment)element).valueToMatch();
String value = pathSegment.valueToMatch();
if (value.length() != this.len) {
// Not enough data to match this path element
return false;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -16,8 +16,8 @@
package org.springframework.web.util.pattern;
import org.springframework.http.server.PathContainer;
import org.springframework.http.server.PathContainer.Element;
import org.springframework.http.server.PathContainer.PathSegment;
import org.springframework.web.util.pattern.PathPattern.MatchingContext;
/**
@@ -46,11 +46,11 @@ class WildcardPathElement extends PathElement {
// Assert if it exists it is a segment
if (pathIndex < matchingContext.pathLength) {
Element element = matchingContext.pathElements.get(pathIndex);
if (!(element instanceof PathContainer.PathSegment)) {
if (!(element instanceof PathSegment pathSegment)) {
// Should not match a separator
return false;
}
segmentData = ((PathContainer.PathSegment)element).valueToMatch();
segmentData = pathSegment.valueToMatch();
pathIndex++;
}