diff --git a/spring-web/src/main/java/org/springframework/http/DefaultHttpStatusCode.java b/spring-web/src/main/java/org/springframework/http/DefaultHttpStatusCode.java index b20b31d88b..de6741965d 100644 --- a/spring-web/src/main/java/org/springframework/http/DefaultHttpStatusCode.java +++ b/spring-web/src/main/java/org/springframework/http/DefaultHttpStatusCode.java @@ -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 diff --git a/spring-web/src/main/java/org/springframework/http/HttpMethod.java b/spring-web/src/main/java/org/springframework/http/HttpMethod.java index 6b73824476..9cb4c1895b 100644 --- a/spring-web/src/main/java/org/springframework/http/HttpMethod.java +++ b/spring-web/src/main/java/org/springframework/http/HttpMethod.java @@ -170,14 +170,8 @@ public final class HttpMethod implements Comparable, 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 diff --git a/spring-web/src/main/java/org/springframework/http/client/SimpleClientHttpRequestFactory.java b/spring-web/src/main/java/org/springframework/http/client/SimpleClientHttpRequestFactory.java index 091b345a26..8af176ef10 100644 --- a/spring-web/src/main/java/org/springframework/http/client/SimpleClientHttpRequestFactory.java +++ b/spring-web/src/main/java/org/springframework/http/client/SimpleClientHttpRequestFactory.java @@ -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; } /** diff --git a/spring-web/src/main/java/org/springframework/http/codec/json/Jackson2Tokenizer.java b/spring-web/src/main/java/org/springframework/http/codec/json/Jackson2Tokenizer.java index 75f3ed8271..9b56435503 100644 --- a/spring-web/src/main/java/org/springframework/http/codec/json/Jackson2Tokenizer.java +++ b/spring-web/src/main/java/org/springframework/http/codec/json/Jackson2Tokenizer.java @@ -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); diff --git a/spring-web/src/main/java/org/springframework/http/codec/multipart/MultipartHttpMessageWriter.java b/spring-web/src/main/java/org/springframework/http/codec/multipart/MultipartHttpMessageWriter.java index b4fca60dd9..3cf6d797ad 100644 --- a/spring-web/src/main/java/org/springframework/http/codec/multipart/MultipartHttpMessageWriter.java +++ b/spring-web/src/main/java/org/springframework/http/codec/multipart/MultipartHttpMessageWriter.java @@ -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 Flux 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 httpEntity = (HttpEntity) 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 bodyPublisher = - body instanceof Publisher ? (Publisher) body : Mono.just(body); + Publisher 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(). diff --git a/spring-web/src/main/java/org/springframework/http/server/DefaultPathContainer.java b/spring-web/src/main/java/org/springframework/http/server/DefaultPathContainer.java index e3cadabe97..88e98a4d08 100644 --- a/spring-web/src/main/java/org/springframework/http/server/DefaultPathContainer.java +++ b/spring-web/src/main/java/org/springframework/http/server/DefaultPathContainer.java @@ -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 diff --git a/spring-web/src/main/java/org/springframework/web/client/RestTemplate.java b/spring-web/src/main/java/org/springframework/web/client/RestTemplate.java index 13fbe9ecf7..6cabd48626 100644 --- a/spring-web/src/main/java/org/springframework/web/client/RestTemplate.java +++ b/spring-web/src/main/java/org/springframework/web/client/RestTemplate.java @@ -1006,7 +1006,7 @@ public class RestTemplate extends InterceptingHttpAccessor implements RestOperat private Stream 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 -> { diff --git a/spring-web/src/main/java/org/springframework/web/context/request/async/WebAsyncUtils.java b/spring-web/src/main/java/org/springframework/web/context/request/async/WebAsyncUtils.java index 2798bb2158..7c948f1037 100644 --- a/spring-web/src/main/java/org/springframework/web/context/request/async/WebAsyncUtils.java +++ b/spring-web/src/main/java/org/springframework/web/context/request/async/WebAsyncUtils.java @@ -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(); diff --git a/spring-web/src/main/java/org/springframework/web/multipart/support/MultipartFilter.java b/spring-web/src/main/java/org/springframework/web/multipart/support/MultipartFilter.java index e1e799dcb7..bc39ea4259 100644 --- a/spring-web/src/main/java/org/springframework/web/multipart/support/MultipartFilter.java +++ b/spring-web/src/main/java/org/springframework/web/multipart/support/MultipartFilter.java @@ -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); } } } diff --git a/spring-web/src/main/java/org/springframework/web/util/pattern/LiteralPathElement.java b/spring-web/src/main/java/org/springframework/web/util/pattern/LiteralPathElement.java index ae3c2ea305..5a2de132b4 100644 --- a/spring-web/src/main/java/org/springframework/web/util/pattern/LiteralPathElement.java +++ b/spring-web/src/main/java/org/springframework/web/util/pattern/LiteralPathElement.java @@ -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; diff --git a/spring-web/src/main/java/org/springframework/web/util/pattern/PathPattern.java b/spring-web/src/main/java/org/springframework/web/util/pattern/PathPattern.java index ed3339c1df..597c0df057 100644 --- a/spring-web/src/main/java/org/springframework/web/util/pattern/PathPattern.java +++ b/spring-web/src/main/java/org/springframework/web/util/pattern/PathPattern.java @@ -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 { */ 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() : ""); } } diff --git a/spring-web/src/main/java/org/springframework/web/util/pattern/SingleCharWildcardedPathElement.java b/spring-web/src/main/java/org/springframework/web/util/pattern/SingleCharWildcardedPathElement.java index c763874163..3d83fc1df0 100644 --- a/spring-web/src/main/java/org/springframework/web/util/pattern/SingleCharWildcardedPathElement.java +++ b/spring-web/src/main/java/org/springframework/web/util/pattern/SingleCharWildcardedPathElement.java @@ -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; diff --git a/spring-web/src/main/java/org/springframework/web/util/pattern/WildcardPathElement.java b/spring-web/src/main/java/org/springframework/web/util/pattern/WildcardPathElement.java index 5413dedc79..0fa2be98b9 100644 --- a/spring-web/src/main/java/org/springframework/web/util/pattern/WildcardPathElement.java +++ b/spring-web/src/main/java/org/springframework/web/util/pattern/WildcardPathElement.java @@ -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++; }