Use instanceof patterns

See gh-33987
This commit is contained in:
Krzysztof Krason 2023-01-26 18:27:37 -08:00 committed by Phillip Webb
parent a9c547e767
commit 0e68cae57f
9 changed files with 24 additions and 42 deletions

View File

@ -175,8 +175,7 @@ public class WebFluxEndpointManagementContextConfiguration {
} }
private void process(Encoder<?> encoder) { private void process(Encoder<?> encoder) {
if (encoder instanceof Jackson2JsonEncoder) { if (encoder instanceof Jackson2JsonEncoder jackson2JsonEncoder) {
Jackson2JsonEncoder jackson2JsonEncoder = (Jackson2JsonEncoder) encoder;
jackson2JsonEncoder.registerObjectMappersForType(OperationResponseBody.class, (associations) -> { jackson2JsonEncoder.registerObjectMappersForType(OperationResponseBody.class, (associations) -> {
ObjectMapper objectMapper = this.endpointObjectMapper.get().get(); ObjectMapper objectMapper = this.endpointObjectMapper.get().get();
MEDIA_TYPES.forEach((mimeType) -> associations.put(mimeType, objectMapper)); MEDIA_TYPES.forEach((mimeType) -> associations.put(mimeType, objectMapper));

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2022 the original author or authors. * Copyright 2012-2023 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -240,10 +240,9 @@ public class JerseyEndpointResourceFactory {
Status status = isGet ? Status.NOT_FOUND : Status.NO_CONTENT; Status status = isGet ? Status.NOT_FOUND : Status.NO_CONTENT;
return Response.status(status).build(); return Response.status(status).build();
} }
if (!(response instanceof WebEndpointResponse)) { if (!(response instanceof WebEndpointResponse<?> webEndpointResponse)) {
return Response.status(Status.OK).entity(convertIfNecessary(response)).build(); return Response.status(Status.OK).entity(convertIfNecessary(response)).build();
} }
WebEndpointResponse<?> webEndpointResponse = (WebEndpointResponse<?>) response;
return Response.status(webEndpointResponse.getStatus()) return Response.status(webEndpointResponse.getStatus())
.header("Content-Type", webEndpointResponse.getContentType()) .header("Content-Type", webEndpointResponse.getContentType())
.entity(convertIfNecessary(webEndpointResponse.getBody())).build(); .entity(convertIfNecessary(webEndpointResponse.getBody())).build();

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2022 the original author or authors. * Copyright 2012-2023 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -379,10 +379,9 @@ public abstract class AbstractWebFluxEndpointHandlerMapping extends RequestMappi
} }
private ResponseEntity<Object> toResponseEntity(Object response) { private ResponseEntity<Object> toResponseEntity(Object response) {
if (!(response instanceof WebEndpointResponse)) { if (!(response instanceof WebEndpointResponse<?> webEndpointResponse)) {
return new ResponseEntity<>(response, HttpStatus.OK); return new ResponseEntity<>(response, HttpStatus.OK);
} }
WebEndpointResponse<?> webEndpointResponse = (WebEndpointResponse<?>) response;
MediaType contentType = (webEndpointResponse.getContentType() != null) MediaType contentType = (webEndpointResponse.getContentType() != null)
? new MediaType(webEndpointResponse.getContentType()) : null; ? new MediaType(webEndpointResponse.getContentType()) : null;
return ResponseEntity.status(webEndpointResponse.getStatus()).contentType(contentType) return ResponseEntity.status(webEndpointResponse.getStatus()).contentType(contentType)

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2022 the original author or authors. * Copyright 2012-2023 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -375,10 +375,9 @@ public abstract class AbstractWebMvcEndpointHandlerMapping extends RequestMappin
return new ResponseEntity<>( return new ResponseEntity<>(
(httpMethod != HttpMethod.GET) ? HttpStatus.NO_CONTENT : HttpStatus.NOT_FOUND); (httpMethod != HttpMethod.GET) ? HttpStatus.NO_CONTENT : HttpStatus.NOT_FOUND);
} }
if (!(result instanceof WebEndpointResponse)) { if (!(result instanceof WebEndpointResponse<?> response)) {
return convertIfNecessary(result); return convertIfNecessary(result);
} }
WebEndpointResponse<?> response = (WebEndpointResponse<?>) result;
MediaType contentType = (response.getContentType() != null) ? new MediaType(response.getContentType()) MediaType contentType = (response.getContentType() != null) ? new MediaType(response.getContentType())
: null; : null;
return ResponseEntity.status(response.getStatus()).contentType(contentType) return ResponseEntity.status(response.getStatus()).contentType(contentType)

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2022 the original author or authors. * Copyright 2012-2023 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -132,8 +132,7 @@ public class TomcatWebServerFactoryCustomizer
private void customizeAcceptCount(ConfigurableTomcatWebServerFactory factory, int acceptCount) { private void customizeAcceptCount(ConfigurableTomcatWebServerFactory factory, int acceptCount) {
factory.addConnectorCustomizers((connector) -> { factory.addConnectorCustomizers((connector) -> {
ProtocolHandler handler = connector.getProtocolHandler(); ProtocolHandler handler = connector.getProtocolHandler();
if (handler instanceof AbstractProtocol) { if (handler instanceof AbstractProtocol<?> protocol) {
AbstractProtocol<?> protocol = (AbstractProtocol<?>) handler;
protocol.setAcceptCount(acceptCount); protocol.setAcceptCount(acceptCount);
} }
}); });
@ -156,8 +155,7 @@ public class TomcatWebServerFactoryCustomizer
protocol.setKeepAliveTimeout(keepAliveTimeout.toMillis()); protocol.setKeepAliveTimeout(keepAliveTimeout.toMillis());
} }
} }
if (handler instanceof AbstractProtocol) { if (handler instanceof AbstractProtocol<?> protocol) {
AbstractProtocol<?> protocol = (AbstractProtocol<?>) handler;
protocol.setKeepAliveTimeout((int) keepAliveTimeout.toMillis()); protocol.setKeepAliveTimeout((int) keepAliveTimeout.toMillis());
} }
}); });
@ -166,8 +164,7 @@ public class TomcatWebServerFactoryCustomizer
private void customizeMaxKeepAliveRequests(ConfigurableTomcatWebServerFactory factory, int maxKeepAliveRequests) { private void customizeMaxKeepAliveRequests(ConfigurableTomcatWebServerFactory factory, int maxKeepAliveRequests) {
factory.addConnectorCustomizers((connector) -> { factory.addConnectorCustomizers((connector) -> {
ProtocolHandler handler = connector.getProtocolHandler(); ProtocolHandler handler = connector.getProtocolHandler();
if (handler instanceof AbstractHttp11Protocol) { if (handler instanceof AbstractHttp11Protocol<?> protocol) {
AbstractHttp11Protocol<?> protocol = (AbstractHttp11Protocol<?>) handler;
protocol.setMaxKeepAliveRequests(maxKeepAliveRequests); protocol.setMaxKeepAliveRequests(maxKeepAliveRequests);
} }
}); });
@ -176,8 +173,7 @@ public class TomcatWebServerFactoryCustomizer
private void customizeMaxConnections(ConfigurableTomcatWebServerFactory factory, int maxConnections) { private void customizeMaxConnections(ConfigurableTomcatWebServerFactory factory, int maxConnections) {
factory.addConnectorCustomizers((connector) -> { factory.addConnectorCustomizers((connector) -> {
ProtocolHandler handler = connector.getProtocolHandler(); ProtocolHandler handler = connector.getProtocolHandler();
if (handler instanceof AbstractProtocol) { if (handler instanceof AbstractProtocol<?> protocol) {
AbstractProtocol<?> protocol = (AbstractProtocol<?>) handler;
protocol.setMaxConnections(maxConnections); protocol.setMaxConnections(maxConnections);
} }
}); });
@ -186,8 +182,7 @@ public class TomcatWebServerFactoryCustomizer
private void customizeConnectionTimeout(ConfigurableTomcatWebServerFactory factory, Duration connectionTimeout) { private void customizeConnectionTimeout(ConfigurableTomcatWebServerFactory factory, Duration connectionTimeout) {
factory.addConnectorCustomizers((connector) -> { factory.addConnectorCustomizers((connector) -> {
ProtocolHandler handler = connector.getProtocolHandler(); ProtocolHandler handler = connector.getProtocolHandler();
if (handler instanceof AbstractProtocol) { if (handler instanceof AbstractProtocol<?> protocol) {
AbstractProtocol<?> protocol = (AbstractProtocol<?>) handler;
protocol.setConnectionTimeout((int) connectionTimeout.toMillis()); protocol.setConnectionTimeout((int) connectionTimeout.toMillis());
} }
}); });
@ -204,8 +199,7 @@ public class TomcatWebServerFactoryCustomizer
private void customizeRejectIllegalHeader(ConfigurableTomcatWebServerFactory factory, boolean rejectIllegalHeader) { private void customizeRejectIllegalHeader(ConfigurableTomcatWebServerFactory factory, boolean rejectIllegalHeader) {
factory.addConnectorCustomizers((connector) -> { factory.addConnectorCustomizers((connector) -> {
ProtocolHandler handler = connector.getProtocolHandler(); ProtocolHandler handler = connector.getProtocolHandler();
if (handler instanceof AbstractHttp11Protocol) { if (handler instanceof AbstractHttp11Protocol<?> protocol) {
AbstractHttp11Protocol<?> protocol = (AbstractHttp11Protocol<?>) handler;
protocol.setRejectIllegalHeader(rejectIllegalHeader); protocol.setRejectIllegalHeader(rejectIllegalHeader);
} }
}); });
@ -249,15 +243,14 @@ public class TomcatWebServerFactoryCustomizer
CloudPlatform platform = CloudPlatform.getActive(this.environment); CloudPlatform platform = CloudPlatform.getActive(this.environment);
return platform != null && platform.isUsingForwardHeaders(); return platform != null && platform.isUsingForwardHeaders();
} }
return this.serverProperties.getForwardHeadersStrategy().equals(ServerProperties.ForwardHeadersStrategy.NATIVE); return this.serverProperties.getForwardHeadersStrategy() == ServerProperties.ForwardHeadersStrategy.NATIVE;
} }
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
private void customizeMaxThreads(ConfigurableTomcatWebServerFactory factory, int maxThreads) { private void customizeMaxThreads(ConfigurableTomcatWebServerFactory factory, int maxThreads) {
factory.addConnectorCustomizers((connector) -> { factory.addConnectorCustomizers((connector) -> {
ProtocolHandler handler = connector.getProtocolHandler(); ProtocolHandler handler = connector.getProtocolHandler();
if (handler instanceof AbstractProtocol) { if (handler instanceof AbstractProtocol protocol) {
AbstractProtocol protocol = (AbstractProtocol) handler;
protocol.setMaxThreads(maxThreads); protocol.setMaxThreads(maxThreads);
} }
}); });
@ -267,8 +260,7 @@ public class TomcatWebServerFactoryCustomizer
private void customizeMinThreads(ConfigurableTomcatWebServerFactory factory, int minSpareThreads) { private void customizeMinThreads(ConfigurableTomcatWebServerFactory factory, int minSpareThreads) {
factory.addConnectorCustomizers((connector) -> { factory.addConnectorCustomizers((connector) -> {
ProtocolHandler handler = connector.getProtocolHandler(); ProtocolHandler handler = connector.getProtocolHandler();
if (handler instanceof AbstractProtocol) { if (handler instanceof AbstractProtocol protocol) {
AbstractProtocol protocol = (AbstractProtocol) handler;
protocol.setMinSpareThreads(minSpareThreads); protocol.setMinSpareThreads(minSpareThreads);
} }
}); });
@ -279,8 +271,7 @@ public class TomcatWebServerFactoryCustomizer
int maxHttpRequestHeaderSize) { int maxHttpRequestHeaderSize) {
factory.addConnectorCustomizers((connector) -> { factory.addConnectorCustomizers((connector) -> {
ProtocolHandler handler = connector.getProtocolHandler(); ProtocolHandler handler = connector.getProtocolHandler();
if (handler instanceof AbstractHttp11Protocol) { if (handler instanceof AbstractHttp11Protocol protocol) {
AbstractHttp11Protocol protocol = (AbstractHttp11Protocol) handler;
protocol.setMaxHttpRequestHeaderSize(maxHttpRequestHeaderSize); protocol.setMaxHttpRequestHeaderSize(maxHttpRequestHeaderSize);
} }
}); });
@ -289,8 +280,7 @@ public class TomcatWebServerFactoryCustomizer
private void customizeMaxSwallowSize(ConfigurableTomcatWebServerFactory factory, int maxSwallowSize) { private void customizeMaxSwallowSize(ConfigurableTomcatWebServerFactory factory, int maxSwallowSize) {
factory.addConnectorCustomizers((connector) -> { factory.addConnectorCustomizers((connector) -> {
ProtocolHandler handler = connector.getProtocolHandler(); ProtocolHandler handler = connector.getProtocolHandler();
if (handler instanceof AbstractHttp11Protocol) { if (handler instanceof AbstractHttp11Protocol<?> protocol) {
AbstractHttp11Protocol<?> protocol = (AbstractHttp11Protocol<?>) handler;
protocol.setMaxSwallowSize(maxSwallowSize); protocol.setMaxSwallowSize(maxSwallowSize);
} }
}); });

View File

@ -962,8 +962,7 @@ public class TestRestTemplate {
} }
private URI resolveUri(RequestEntity<?> entity) { private URI resolveUri(RequestEntity<?> entity) {
if (entity instanceof UriTemplateRequestEntity) { if (entity instanceof UriTemplateRequestEntity<?> templatedUriEntity) {
UriTemplateRequestEntity<?> templatedUriEntity = (UriTemplateRequestEntity<?>) entity;
if (templatedUriEntity.getVars() != null) { if (templatedUriEntity.getVars() != null) {
return this.restTemplate.getUriTemplateHandler().expand(templatedUriEntity.getUriTemplate(), return this.restTemplate.getUriTemplateHandler().expand(templatedUriEntity.getUriTemplate(),
templatedUriEntity.getVars()); templatedUriEntity.getVars());

View File

@ -29,8 +29,7 @@ class JSON {
if (value instanceof Boolean) { if (value instanceof Boolean) {
return (Boolean) value; return (Boolean) value;
} }
if (value instanceof String) { if (value instanceof String stringValue) {
String stringValue = (String) value;
if ("true".equalsIgnoreCase(stringValue)) { if ("true".equalsIgnoreCase(stringValue)) {
return true; return true;
} }

View File

@ -311,8 +311,7 @@ public class JSONObject {
JSON.checkDouble(((Number) value).doubleValue()); JSON.checkDouble(((Number) value).doubleValue());
} }
if (current instanceof JSONArray) { if (current instanceof JSONArray array) {
JSONArray array = (JSONArray) current;
array.put(value); array.put(value);
} }
else { else {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2022 the original author or authors. * Copyright 2012-2023 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -61,8 +61,7 @@ class NotConstructorBoundInjectionFailureAnalyzer
} }
private boolean isConstructorBindingConfigurationProperties(InjectionPoint injectionPoint) { private boolean isConstructorBindingConfigurationProperties(InjectionPoint injectionPoint) {
if (injectionPoint != null && injectionPoint.getMember() instanceof Constructor) { if (injectionPoint != null && injectionPoint.getMember() instanceof Constructor<?> constructor) {
Constructor<?> constructor = (Constructor<?>) injectionPoint.getMember();
Class<?> declaringClass = constructor.getDeclaringClass(); Class<?> declaringClass = constructor.getDeclaringClass();
MergedAnnotation<ConfigurationProperties> configurationProperties = MergedAnnotations.from(declaringClass) MergedAnnotation<ConfigurationProperties> configurationProperties = MergedAnnotations.from(declaringClass)
.get(ConfigurationProperties.class); .get(ConfigurationProperties.class);