Apply "instanceof pattern matching" in spring-webflux

Closes gh-29635
This commit is contained in:
divcon 2022-12-04 22:33:45 +09:00 committed by Sam Brannen
parent 918edaba2e
commit f64397d822
22 changed files with 68 additions and 70 deletions

View File

@ -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 {

View File

@ -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);

View File

@ -133,11 +133,11 @@ class DefaultClientResponse implements ClientResponse {
public <T> T body(BodyExtractor<T, ? super ClientHttpResponse> 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;

View File

@ -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);

View File

@ -129,8 +129,8 @@ class PathResourceLookupFunction implements Function<ServerRequest, Mono<Resourc
resourcePath = resource.getURL().toExternalForm();
locationPath = StringUtils.cleanPath(this.location.getURL().toString());
}
else if (resource instanceof ClassPathResource) {
resourcePath = ((ClassPathResource) resource).getPath();
else if (resource instanceof ClassPathResource classPathResource) {
resourcePath = classPathResource.getPath();
locationPath = StringUtils.cleanPath(((ClassPathResource) this.location).getPath());
}
else {

View File

@ -795,11 +795,11 @@ public abstract class RequestPredicates {
@Override
public void changeParser(PathPatternParser parser) {
if (this.left instanceof ChangePathPatternParserVisitor.Target) {
((ChangePathPatternParserVisitor.Target) this.left).changeParser(parser);
if (this.left instanceof ChangePathPatternParserVisitor.Target leftTarget) {
leftTarget.changeParser(parser);
}
if (this.right instanceof ChangePathPatternParserVisitor.Target) {
((ChangePathPatternParserVisitor.Target) this.right).changeParser(parser);
if (this.right instanceof ChangePathPatternParserVisitor.Target rightTarget) {
rightTarget.changeParser(parser);
}
}
@ -841,8 +841,8 @@ public abstract class RequestPredicates {
@Override
public void changeParser(PathPatternParser parser) {
if (this.delegate instanceof ChangePathPatternParserVisitor.Target) {
((ChangePathPatternParserVisitor.Target) this.delegate).changeParser(parser);
if (this.delegate instanceof ChangePathPatternParserVisitor.Target target) {
target.changeParser(parser);
}
}
@ -909,11 +909,11 @@ public abstract class RequestPredicates {
@Override
public void changeParser(PathPatternParser parser) {
if (this.left instanceof ChangePathPatternParserVisitor.Target) {
((ChangePathPatternParserVisitor.Target) this.left).changeParser(parser);
if (this.left instanceof ChangePathPatternParserVisitor.Target leftTarget) {
leftTarget.changeParser(parser);
}
if (this.right instanceof ChangePathPatternParserVisitor.Target) {
((ChangePathPatternParserVisitor.Target) this.right).changeParser(parser);
if (this.right instanceof ChangePathPatternParserVisitor.Target rightTarget) {
rightTarget.changeParser(parser);
}
}

View File

@ -233,8 +233,8 @@ public abstract class AbstractHandlerMapping extends ApplicationObjectSupport
*/
@Nullable
protected CorsConfiguration getCorsConfiguration(Object handler, ServerWebExchange exchange) {
if (handler instanceof CorsConfigurationSource) {
return ((CorsConfigurationSource) handler).getCorsConfiguration(exchange);
if (handler instanceof CorsConfigurationSource ccs) {
return ccs.getCorsConfiguration(exchange);
}
return null;
}

View File

@ -153,8 +153,8 @@ public class SimpleUrlHandlerMapping extends AbstractUrlHandlerMapping {
url = "/" + url;
}
// Remove whitespace from handler bean name.
if (handler instanceof String) {
handler = ((String) handler).trim();
if (handler instanceof String handlerName) {
handler = handlerName.trim();
}
registerHandler(url, handler);
}

View File

@ -272,8 +272,8 @@ public class EncodedResourceResolver extends AbstractResourceResolver {
@Override
public HttpHeaders getResponseHeaders() {
HttpHeaders headers;
if (this.original instanceof HttpResource) {
headers = ((HttpResource) this.original).getResponseHeaders();
if (this.original instanceof HttpResource httpResource) {
headers = httpResource.getResponseHeaders();
}
else {
headers = new HttpHeaders();

View File

@ -247,7 +247,7 @@ public class ModelAttributeMethodArgumentResolver extends HandlerMethodArgumentR
}
}
}
value = (value instanceof List ? ((List<?>) 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);

View File

@ -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);
}

View File

@ -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(

View File

@ -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;
}

View File

@ -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 ?

View File

@ -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;

View File

@ -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)) {

View File

@ -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();
}
}

View File

@ -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));
}

View File

@ -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 {

View File

@ -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;

View File

@ -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 {

View File

@ -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();
}