diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/config/ResourceHandlerRegistry.java b/spring-webflux/src/main/java/org/springframework/web/reactive/config/ResourceHandlerRegistry.java index 86f3729f71..3c1475ee24 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/config/ResourceHandlerRegistry.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/config/ResourceHandlerRegistry.java @@ -148,8 +148,8 @@ public class ResourceHandlerRegistry { private ResourceWebHandler getRequestHandler(ResourceHandlerRegistration registration) { ResourceWebHandler handler = registration.getRequestHandler(); for (ResourceTransformer transformer : handler.getResourceTransformers()) { - if (transformer instanceof ResourceTransformerSupport) { - ((ResourceTransformerSupport) transformer).setResourceUrlProvider(this.resourceUrlProvider); + if (transformer instanceof ResourceTransformerSupport resourceTransformerSupport) { + resourceTransformerSupport.setResourceUrlProvider(this.resourceUrlProvider); } } try { diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/BodyInserters.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/BodyInserters.java index 5afeba0f8a..7822ff79eb 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/BodyInserters.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/BodyInserters.java @@ -363,8 +363,8 @@ public abstract class BodyInserters { M outputMessage, BodyInserter.Context context, Object body, ResolvableType bodyType, @Nullable ReactiveAdapter adapter) { Publisher publisher; - if (body instanceof Publisher) { - publisher = (Publisher) body; + if (body instanceof Publisher publisherBody) { + publisher = publisherBody; } else if (adapter != null) { publisher = adapter.toPublisher(body); diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultClientResponse.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultClientResponse.java index a9012deb14..67f8a81644 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultClientResponse.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/client/DefaultClientResponse.java @@ -133,11 +133,11 @@ class DefaultClientResponse implements ClientResponse { public T body(BodyExtractor extractor) { T result = extractor.extract(this.response, this.bodyExtractorContext); String description = "Body from " + this.requestDescription + " [DefaultClientResponse]"; - if (result instanceof Mono) { - return (T) ((Mono) result).checkpoint(description); + if (result instanceof Mono monoResult) { + return (T) monoResult.checkpoint(description); } - else if (result instanceof Flux) { - return (T) ((Flux) result).checkpoint(description); + else if (result instanceof Flux fluxResult) { + return (T) fluxResult.checkpoint(description); } else { return result; diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultRenderingResponseBuilder.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultRenderingResponseBuilder.java index 0bb73fc68f..59886ba6a8 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultRenderingResponseBuilder.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/DefaultRenderingResponseBuilder.java @@ -107,7 +107,7 @@ final class DefaultRenderingResponseBuilder implements RenderingResponse.Builder @Override public RenderingResponse.Builder modelAttribute(Object attribute) { Assert.notNull(attribute, "Attribute must not be null"); - if (attribute instanceof Collection && ((Collection) attribute).isEmpty()) { + if (attribute instanceof Collection attrCollection && attrCollection.isEmpty()) { return this; } return modelAttribute(Conventions.getVariableName(attribute), attribute); diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/PathResourceLookupFunction.java b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/PathResourceLookupFunction.java index 60caf7c19e..f302076bc3 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/PathResourceLookupFunction.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/function/server/PathResourceLookupFunction.java @@ -129,8 +129,8 @@ class PathResourceLookupFunction implements Function) value).toArray() : value); + value = (value instanceof List valueList ? valueList.toArray() : value); MethodParameter methodParam = new MethodParameter(ctor, i); if (value == null && methodParam.isOptional()) { args[i] = (methodParam.getParameterType() == Optional.class ? Optional.empty() : null); diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/RequestMappingHandlerMapping.java b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/RequestMappingHandlerMapping.java index ad9335ecc2..f4873ea0c9 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/RequestMappingHandlerMapping.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/RequestMappingHandlerMapping.java @@ -177,8 +177,8 @@ public class RequestMappingHandlerMapping extends RequestMappingInfoHandlerMappi @Nullable private RequestMappingInfo createRequestMappingInfo(AnnotatedElement element) { RequestMapping requestMapping = AnnotatedElementUtils.findMergedAnnotation(element, RequestMapping.class); - RequestCondition condition = (element instanceof Class ? - getCustomTypeCondition((Class) element) : getCustomMethodCondition((Method) element)); + RequestCondition condition = (element instanceof Class typeElement ? + getCustomTypeCondition(typeElement) : getCustomMethodCondition((Method) element)); return (requestMapping != null ? createRequestMappingInfo(requestMapping, condition) : null); } diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/ResponseEntityResultHandler.java b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/ResponseEntityResultHandler.java index 3021273dc3..b520d952c0 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/ResponseEntityResultHandler.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/ResponseEntityResultHandler.java @@ -137,8 +137,8 @@ public class ResponseEntityResultHandler extends AbstractMessageWriterResultHand return returnValueMono.flatMap(returnValue -> { HttpEntity httpEntity; - if (returnValue instanceof HttpEntity) { - httpEntity = (HttpEntity) returnValue; + if (returnValue instanceof HttpEntity he) { + httpEntity = he; } else if (returnValue instanceof ErrorResponse response) { httpEntity = new ResponseEntity<>(response.getBody(), response.getHeaders(), response.getStatusCode()); @@ -146,8 +146,8 @@ public class ResponseEntityResultHandler extends AbstractMessageWriterResultHand else if (returnValue instanceof ProblemDetail detail) { httpEntity = ResponseEntity.of(detail).build(); } - else if (returnValue instanceof HttpHeaders) { - httpEntity = new ResponseEntity<>((HttpHeaders) returnValue, HttpStatus.OK); + else if (returnValue instanceof HttpHeaders headers) { + httpEntity = new ResponseEntity<>(headers, HttpStatus.OK); } else { throw new IllegalArgumentException( diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/ServerWebExchangeMethodArgumentResolver.java b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/ServerWebExchangeMethodArgumentResolver.java index 953f1a0dcb..81bcbcceaf 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/ServerWebExchangeMethodArgumentResolver.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/result/method/annotation/ServerWebExchangeMethodArgumentResolver.java @@ -124,8 +124,8 @@ public class ServerWebExchangeMethodArgumentResolver extends HandlerMethodArgume @Nullable private TimeZone getTimeZone(LocaleContext localeContext) { TimeZone timeZone = null; - if (localeContext instanceof TimeZoneAwareLocaleContext) { - timeZone = ((TimeZoneAwareLocaleContext) localeContext).getTimeZone(); + if (localeContext instanceof TimeZoneAwareLocaleContext timeZoneAwareLocaleContext) { + timeZone = timeZoneAwareLocaleContext.getTimeZone(); } return timeZone; } diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/BindStatus.java b/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/BindStatus.java index 4d9b75518d..5d8a40c718 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/BindStatus.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/BindStatus.java @@ -126,8 +126,8 @@ public class BindStatus { this.objectErrors = this.errors.getFieldErrors(this.expression); this.value = this.errors.getFieldValue(this.expression); this.valueType = this.errors.getFieldType(this.expression); - if (this.errors instanceof BindingResult) { - this.bindingResult = (BindingResult) this.errors; + if (this.errors instanceof BindingResult bindingResultError) { + this.bindingResult = bindingResultError; this.actualValue = this.bindingResult.getRawFieldValue(this.expression); this.editor = this.bindingResult.findEditor(this.expression, null); } @@ -162,8 +162,8 @@ public class BindStatus { this.errorMessages = new String[0]; } - if (htmlEscape && this.value instanceof String) { - this.value = HtmlUtils.htmlEscape((String) this.value); + if (htmlEscape && this.value instanceof String valueString) { + this.value = HtmlUtils.htmlEscape(valueString); } } @@ -238,8 +238,8 @@ public class BindStatus { * will get HTML-escaped. */ public String getDisplayValue() { - if (this.value instanceof String) { - return (String) this.value; + if (this.value instanceof String stringValue) { + return stringValue; } if (this.value != null) { return (this.htmlEscape ? diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/UrlBasedViewResolver.java b/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/UrlBasedViewResolver.java index 98220c067d..d4acf00e94 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/UrlBasedViewResolver.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/UrlBasedViewResolver.java @@ -324,8 +324,8 @@ public class UrlBasedViewResolver extends ViewResolverSupport ApplicationContext context = getApplicationContext(); if (context != null) { Object initialized = context.getAutowireCapableBeanFactory().initializeBean(view, viewName); - if (initialized instanceof View) { - return (View) initialized; + if (initialized instanceof View initializedView) { + return initializedView; } } return view; diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/ViewResolutionResultHandler.java b/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/ViewResolutionResultHandler.java index 281746e65e..a7b99ffb66 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/ViewResolutionResultHandler.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/result/view/ViewResolutionResultHandler.java @@ -223,7 +223,7 @@ public class ViewResolutionResultHandler extends HandlerResultHandlerSupport imp if (view == null) { view = getDefaultViewName(exchange); } - viewsMono = (view instanceof String ? resolveViews((String) view, locale) : + viewsMono = (view instanceof String stringView? resolveViews(stringView, locale) : Mono.just(Collections.singletonList((View) view))); } else if (Model.class.isAssignableFrom(clazz)) { diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/socket/server/support/HandshakeWebSocketService.java b/spring-webflux/src/main/java/org/springframework/web/reactive/socket/server/support/HandshakeWebSocketService.java index 0d20bfa5b7..e29b696ccc 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/socket/server/support/HandshakeWebSocketService.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/socket/server/support/HandshakeWebSocketService.java @@ -164,8 +164,8 @@ public class HandshakeWebSocketService implements WebSocketService, Lifecycle { } protected void doStart() { - if (getUpgradeStrategy() instanceof Lifecycle) { - ((Lifecycle) getUpgradeStrategy()).start(); + if (getUpgradeStrategy() instanceof Lifecycle lifecycle) { + lifecycle.start(); } } @@ -178,8 +178,8 @@ public class HandshakeWebSocketService implements WebSocketService, Lifecycle { } protected void doStop() { - if (getUpgradeStrategy() instanceof Lifecycle) { - ((Lifecycle) getUpgradeStrategy()).stop(); + if (getUpgradeStrategy() instanceof Lifecycle lifecycle) { + lifecycle.stop(); } } diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/WebClientDataBufferAllocatingTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/WebClientDataBufferAllocatingTests.java index 57357d0854..f13fac8e6d 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/WebClientDataBufferAllocatingTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/function/client/WebClientDataBufferAllocatingTests.java @@ -84,8 +84,8 @@ class WebClientDataBufferAllocatingTests extends AbstractDataBufferAllocatingTes private ReactorClientHttpConnector initConnector() { assertThat(super.bufferFactory).isNotNull(); - if (super.bufferFactory instanceof NettyDataBufferFactory) { - ByteBufAllocator allocator = ((NettyDataBufferFactory) super.bufferFactory).getByteBufAllocator(); + if (super.bufferFactory instanceof NettyDataBufferFactory nettyDataBufferFactory) { + ByteBufAllocator allocator = nettyDataBufferFactory.getByteBufAllocator(); return new ReactorClientHttpConnector(this.factory, client -> client.option(ChannelOption.ALLOCATOR, allocator)); } diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/protobuf/Msg.java b/spring-webflux/src/test/java/org/springframework/web/reactive/protobuf/Msg.java index f57c3ca4de..234b44b52c 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/protobuf/Msg.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/protobuf/Msg.java @@ -144,8 +144,8 @@ public final class Msg extends */ public String getFoo() { Object ref = foo_; - if (ref instanceof String) { - return (String) ref; + if (ref instanceof String stringRef) { + return stringRef; } else { com.google.protobuf.ByteString bs = (com.google.protobuf.ByteString) ref; @@ -162,10 +162,9 @@ public final class Msg extends public com.google.protobuf.ByteString getFooBytes() { Object ref = foo_; - if (ref instanceof String) { + if (ref instanceof String stringRef) { com.google.protobuf.ByteString b = - com.google.protobuf.ByteString.copyFromUtf8( - (String) ref); + com.google.protobuf.ByteString.copyFromUtf8(stringRef); foo_ = b; return b; } else { @@ -405,8 +404,8 @@ public final class Msg extends } public Builder mergeFrom(com.google.protobuf.Message other) { - if (other instanceof Msg) { - return mergeFrom((Msg)other); + if (other instanceof Msg msg) { + return mergeFrom(msg); } else { super.mergeFrom(other); return this; @@ -463,13 +462,13 @@ public final class Msg extends */ public String getFoo() { Object ref = foo_; - if (!(ref instanceof String)) { + if (!(ref instanceof String stringRef)) { String s = ((com.google.protobuf.ByteString) ref) .toStringUtf8(); foo_ = s; return s; } else { - return (String) ref; + return stringRef; } } /** @@ -478,10 +477,9 @@ public final class Msg extends public com.google.protobuf.ByteString getFooBytes() { Object ref = foo_; - if (ref instanceof String) { + if (ref instanceof String stringRef) { com.google.protobuf.ByteString b = - com.google.protobuf.ByteString.copyFromUtf8( - (String) ref); + com.google.protobuf.ByteString.copyFromUtf8(stringRef); foo_ = b; return b; } else { diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/protobuf/SecondMsg.java b/spring-webflux/src/test/java/org/springframework/web/reactive/protobuf/SecondMsg.java index 480798a0a2..67b5a9a066 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/protobuf/SecondMsg.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/protobuf/SecondMsg.java @@ -304,8 +304,8 @@ public final class SecondMsg extends } public Builder mergeFrom(com.google.protobuf.Message other) { - if (other instanceof SecondMsg) { - return mergeFrom((SecondMsg)other); + if (other instanceof SecondMsg secondMsg) { + return mergeFrom(secondMsg); } else { super.mergeFrom(other); return this; diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/MultipartIntegrationTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/MultipartIntegrationTests.java index 098a16ab7f..9ca1ccb704 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/MultipartIntegrationTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/result/method/annotation/MultipartIntegrationTests.java @@ -354,7 +354,7 @@ class MultipartIntegrationTests extends AbstractHttpHandlerIntegrationTests { } private static String partDescription(Part part) { - return part instanceof FilePart ? part.name() + ":" + ((FilePart) part).filename() : part.name(); + return part instanceof FilePart filePart? part.name() + ":" + filePart.filename() : part.name(); } static class FormBean { diff --git a/spring-webflux/src/test/java/org/springframework/web/reactive/socket/AbstractReactiveWebSocketIntegrationTests.java b/spring-webflux/src/test/java/org/springframework/web/reactive/socket/AbstractReactiveWebSocketIntegrationTests.java index 971e16d47b..601642c3c5 100644 --- a/spring-webflux/src/test/java/org/springframework/web/reactive/socket/AbstractReactiveWebSocketIntegrationTests.java +++ b/spring-webflux/src/test/java/org/springframework/web/reactive/socket/AbstractReactiveWebSocketIntegrationTests.java @@ -142,15 +142,15 @@ abstract class AbstractReactiveWebSocketIntegrationTests { // Set dynamically chosen port this.port = this.server.getPort(); - if (this.client instanceof Lifecycle) { - ((Lifecycle) this.client).start(); + if (this.client instanceof Lifecycle lifecycle) { + lifecycle.start(); } } @AfterEach void stopServer() { - if (this.client instanceof Lifecycle) { - ((Lifecycle) this.client).stop(); + if (this.client instanceof Lifecycle lifecycle) { + lifecycle.stop(); } this.server.stop(); }