Skip to content

Commit 0e68cae

Browse files
krzykphilwebb
authored andcommitted
Use instanceof patterns
See gh-33987
1 parent a9c547e commit 0e68cae

File tree

9 files changed

+24
-42
lines changed

9 files changed

+24
-42
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/endpoint/web/reactive/WebFluxEndpointManagementContextConfiguration.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,7 @@ private void process(ServerCodecConfigurer configurer) {
175175
}
176176

177177
private void process(Encoder<?> encoder) {
178-
if (encoder instanceof Jackson2JsonEncoder) {
179-
Jackson2JsonEncoder jackson2JsonEncoder = (Jackson2JsonEncoder) encoder;
178+
if (encoder instanceof Jackson2JsonEncoder jackson2JsonEncoder) {
180179
jackson2JsonEncoder.registerObjectMappersForType(OperationResponseBody.class, (associations) -> {
181180
ObjectMapper objectMapper = this.endpointObjectMapper.get().get();
182181
MEDIA_TYPES.forEach((mimeType) -> associations.put(mimeType, objectMapper));

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/web/jersey/JerseyEndpointResourceFactory.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 the original author or authors.
2+
* Copyright 2012-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -240,10 +240,9 @@ private Response convertToJaxRsResponse(Object response, String httpMethod) {
240240
Status status = isGet ? Status.NOT_FOUND : Status.NO_CONTENT;
241241
return Response.status(status).build();
242242
}
243-
if (!(response instanceof WebEndpointResponse)) {
243+
if (!(response instanceof WebEndpointResponse<?> webEndpointResponse)) {
244244
return Response.status(Status.OK).entity(convertIfNecessary(response)).build();
245245
}
246-
WebEndpointResponse<?> webEndpointResponse = (WebEndpointResponse<?>) response;
247246
return Response.status(webEndpointResponse.getStatus())
248247
.header("Content-Type", webEndpointResponse.getContentType())
249248
.entity(convertIfNecessary(webEndpointResponse.getBody())).build();

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/web/reactive/AbstractWebFluxEndpointHandlerMapping.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 the original author or authors.
2+
* Copyright 2012-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -379,10 +379,9 @@ private Mono<ResponseEntity<Object>> handleResult(Publisher<?> result, HttpMetho
379379
}
380380

381381
private ResponseEntity<Object> toResponseEntity(Object response) {
382-
if (!(response instanceof WebEndpointResponse)) {
382+
if (!(response instanceof WebEndpointResponse<?> webEndpointResponse)) {
383383
return new ResponseEntity<>(response, HttpStatus.OK);
384384
}
385-
WebEndpointResponse<?> webEndpointResponse = (WebEndpointResponse<?>) response;
386385
MediaType contentType = (webEndpointResponse.getContentType() != null)
387386
? new MediaType(webEndpointResponse.getContentType()) : null;
388387
return ResponseEntity.status(webEndpointResponse.getStatus()).contentType(contentType)

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/web/servlet/AbstractWebMvcEndpointHandlerMapping.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 the original author or authors.
2+
* Copyright 2012-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -375,10 +375,9 @@ private Object handleResult(Object result, HttpMethod httpMethod) {
375375
return new ResponseEntity<>(
376376
(httpMethod != HttpMethod.GET) ? HttpStatus.NO_CONTENT : HttpStatus.NOT_FOUND);
377377
}
378-
if (!(result instanceof WebEndpointResponse)) {
378+
if (!(result instanceof WebEndpointResponse<?> response)) {
379379
return convertIfNecessary(result);
380380
}
381-
WebEndpointResponse<?> response = (WebEndpointResponse<?>) result;
382381
MediaType contentType = (response.getContentType() != null) ? new MediaType(response.getContentType())
383382
: null;
384383
return ResponseEntity.status(response.getStatus()).contentType(contentType)

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/embedded/TomcatWebServerFactoryCustomizer.java

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 the original author or authors.
2+
* Copyright 2012-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -132,8 +132,7 @@ private boolean isPositive(int value) {
132132
private void customizeAcceptCount(ConfigurableTomcatWebServerFactory factory, int acceptCount) {
133133
factory.addConnectorCustomizers((connector) -> {
134134
ProtocolHandler handler = connector.getProtocolHandler();
135-
if (handler instanceof AbstractProtocol) {
136-
AbstractProtocol<?> protocol = (AbstractProtocol<?>) handler;
135+
if (handler instanceof AbstractProtocol<?> protocol) {
137136
protocol.setAcceptCount(acceptCount);
138137
}
139138
});
@@ -156,8 +155,7 @@ private void customizeKeepAliveTimeout(ConfigurableTomcatWebServerFactory factor
156155
protocol.setKeepAliveTimeout(keepAliveTimeout.toMillis());
157156
}
158157
}
159-
if (handler instanceof AbstractProtocol) {
160-
AbstractProtocol<?> protocol = (AbstractProtocol<?>) handler;
158+
if (handler instanceof AbstractProtocol<?> protocol) {
161159
protocol.setKeepAliveTimeout((int) keepAliveTimeout.toMillis());
162160
}
163161
});
@@ -166,8 +164,7 @@ private void customizeKeepAliveTimeout(ConfigurableTomcatWebServerFactory factor
166164
private void customizeMaxKeepAliveRequests(ConfigurableTomcatWebServerFactory factory, int maxKeepAliveRequests) {
167165
factory.addConnectorCustomizers((connector) -> {
168166
ProtocolHandler handler = connector.getProtocolHandler();
169-
if (handler instanceof AbstractHttp11Protocol) {
170-
AbstractHttp11Protocol<?> protocol = (AbstractHttp11Protocol<?>) handler;
167+
if (handler instanceof AbstractHttp11Protocol<?> protocol) {
171168
protocol.setMaxKeepAliveRequests(maxKeepAliveRequests);
172169
}
173170
});
@@ -176,8 +173,7 @@ private void customizeMaxKeepAliveRequests(ConfigurableTomcatWebServerFactory fa
176173
private void customizeMaxConnections(ConfigurableTomcatWebServerFactory factory, int maxConnections) {
177174
factory.addConnectorCustomizers((connector) -> {
178175
ProtocolHandler handler = connector.getProtocolHandler();
179-
if (handler instanceof AbstractProtocol) {
180-
AbstractProtocol<?> protocol = (AbstractProtocol<?>) handler;
176+
if (handler instanceof AbstractProtocol<?> protocol) {
181177
protocol.setMaxConnections(maxConnections);
182178
}
183179
});
@@ -186,8 +182,7 @@ private void customizeMaxConnections(ConfigurableTomcatWebServerFactory factory,
186182
private void customizeConnectionTimeout(ConfigurableTomcatWebServerFactory factory, Duration connectionTimeout) {
187183
factory.addConnectorCustomizers((connector) -> {
188184
ProtocolHandler handler = connector.getProtocolHandler();
189-
if (handler instanceof AbstractProtocol) {
190-
AbstractProtocol<?> protocol = (AbstractProtocol<?>) handler;
185+
if (handler instanceof AbstractProtocol<?> protocol) {
191186
protocol.setConnectionTimeout((int) connectionTimeout.toMillis());
192187
}
193188
});
@@ -204,8 +199,7 @@ private void customizeRelaxedQueryChars(ConfigurableTomcatWebServerFactory facto
204199
private void customizeRejectIllegalHeader(ConfigurableTomcatWebServerFactory factory, boolean rejectIllegalHeader) {
205200
factory.addConnectorCustomizers((connector) -> {
206201
ProtocolHandler handler = connector.getProtocolHandler();
207-
if (handler instanceof AbstractHttp11Protocol) {
208-
AbstractHttp11Protocol<?> protocol = (AbstractHttp11Protocol<?>) handler;
202+
if (handler instanceof AbstractHttp11Protocol<?> protocol) {
209203
protocol.setRejectIllegalHeader(rejectIllegalHeader);
210204
}
211205
});
@@ -249,15 +243,14 @@ private boolean getOrDeduceUseForwardHeaders() {
249243
CloudPlatform platform = CloudPlatform.getActive(this.environment);
250244
return platform != null && platform.isUsingForwardHeaders();
251245
}
252-
return this.serverProperties.getForwardHeadersStrategy().equals(ServerProperties.ForwardHeadersStrategy.NATIVE);
246+
return this.serverProperties.getForwardHeadersStrategy() == ServerProperties.ForwardHeadersStrategy.NATIVE;
253247
}
254248

255249
@SuppressWarnings("rawtypes")
256250
private void customizeMaxThreads(ConfigurableTomcatWebServerFactory factory, int maxThreads) {
257251
factory.addConnectorCustomizers((connector) -> {
258252
ProtocolHandler handler = connector.getProtocolHandler();
259-
if (handler instanceof AbstractProtocol) {
260-
AbstractProtocol protocol = (AbstractProtocol) handler;
253+
if (handler instanceof AbstractProtocol protocol) {
261254
protocol.setMaxThreads(maxThreads);
262255
}
263256
});
@@ -267,8 +260,7 @@ private void customizeMaxThreads(ConfigurableTomcatWebServerFactory factory, int
267260
private void customizeMinThreads(ConfigurableTomcatWebServerFactory factory, int minSpareThreads) {
268261
factory.addConnectorCustomizers((connector) -> {
269262
ProtocolHandler handler = connector.getProtocolHandler();
270-
if (handler instanceof AbstractProtocol) {
271-
AbstractProtocol protocol = (AbstractProtocol) handler;
263+
if (handler instanceof AbstractProtocol protocol) {
272264
protocol.setMinSpareThreads(minSpareThreads);
273265
}
274266
});
@@ -279,8 +271,7 @@ private void customizeMaxHttpRequestHeaderSize(ConfigurableTomcatWebServerFactor
279271
int maxHttpRequestHeaderSize) {
280272
factory.addConnectorCustomizers((connector) -> {
281273
ProtocolHandler handler = connector.getProtocolHandler();
282-
if (handler instanceof AbstractHttp11Protocol) {
283-
AbstractHttp11Protocol protocol = (AbstractHttp11Protocol) handler;
274+
if (handler instanceof AbstractHttp11Protocol protocol) {
284275
protocol.setMaxHttpRequestHeaderSize(maxHttpRequestHeaderSize);
285276
}
286277
});
@@ -289,8 +280,7 @@ private void customizeMaxHttpRequestHeaderSize(ConfigurableTomcatWebServerFactor
289280
private void customizeMaxSwallowSize(ConfigurableTomcatWebServerFactory factory, int maxSwallowSize) {
290281
factory.addConnectorCustomizers((connector) -> {
291282
ProtocolHandler handler = connector.getProtocolHandler();
292-
if (handler instanceof AbstractHttp11Protocol) {
293-
AbstractHttp11Protocol<?> protocol = (AbstractHttp11Protocol<?>) handler;
283+
if (handler instanceof AbstractHttp11Protocol<?> protocol) {
294284
protocol.setMaxSwallowSize(maxSwallowSize);
295285
}
296286
});

spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/web/client/TestRestTemplate.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -962,8 +962,7 @@ private URI applyRootUriIfNecessary(URI uri) {
962962
}
963963

964964
private URI resolveUri(RequestEntity<?> entity) {
965-
if (entity instanceof UriTemplateRequestEntity) {
966-
UriTemplateRequestEntity<?> templatedUriEntity = (UriTemplateRequestEntity<?>) entity;
965+
if (entity instanceof UriTemplateRequestEntity<?> templatedUriEntity) {
967966
if (templatedUriEntity.getVars() != null) {
968967
return this.restTemplate.getUriTemplateHandler().expand(templatedUriEntity.getUriTemplate(),
969968
templatedUriEntity.getVars());

spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/json-shade/java/org/springframework/boot/configurationprocessor/json/JSON.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ static Boolean toBoolean(Object value) {
2929
if (value instanceof Boolean) {
3030
return (Boolean) value;
3131
}
32-
if (value instanceof String) {
33-
String stringValue = (String) value;
32+
if (value instanceof String stringValue) {
3433
if ("true".equalsIgnoreCase(stringValue)) {
3534
return true;
3635
}

spring-boot-project/spring-boot-tools/spring-boot-configuration-processor/src/json-shade/java/org/springframework/boot/configurationprocessor/json/JSONObject.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -311,8 +311,7 @@ public JSONObject accumulate(String name, Object value) throws JSONException {
311311
JSON.checkDouble(((Number) value).doubleValue());
312312
}
313313

314-
if (current instanceof JSONArray) {
315-
JSONArray array = (JSONArray) current;
314+
if (current instanceof JSONArray array) {
316315
array.put(value);
317316
}
318317
else {

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/properties/NotConstructorBoundInjectionFailureAnalyzer.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2022 the original author or authors.
2+
* Copyright 2012-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -61,8 +61,7 @@ protected FailureAnalysis analyze(Throwable rootFailure, NoSuchBeanDefinitionExc
6161
}
6262

6363
private boolean isConstructorBindingConfigurationProperties(InjectionPoint injectionPoint) {
64-
if (injectionPoint != null && injectionPoint.getMember() instanceof Constructor) {
65-
Constructor<?> constructor = (Constructor<?>) injectionPoint.getMember();
64+
if (injectionPoint != null && injectionPoint.getMember() instanceof Constructor<?> constructor) {
6665
Class<?> declaringClass = constructor.getDeclaringClass();
6766
MergedAnnotation<ConfigurationProperties> configurationProperties = MergedAnnotations.from(declaringClass)
6867
.get(ConfigurationProperties.class);

0 commit comments

Comments
 (0)