Skip to content

Commit 99ae209

Browse files
divconsbrannen
authored andcommitted
Apply 'instanceof pattern matching' in spring-web
Closes gh-29530
1 parent 64c6a97 commit 99ae209

File tree

50 files changed

+216
-227
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+216
-227
lines changed

spring-web/src/main/java/org/springframework/http/client/HttpComponentsClientHttpRequestFactory.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,8 +202,8 @@ public ClientHttpRequest createRequest(URI uri, HttpMethod httpMethod) throws IO
202202
if (context.getAttribute(HttpClientContext.REQUEST_CONFIG) == null) {
203203
// Use request configuration given by the user, when available
204204
RequestConfig config = null;
205-
if (httpRequest instanceof Configurable) {
206-
config = ((Configurable) httpRequest).getConfig();
205+
if (httpRequest instanceof Configurable configurable) {
206+
config = configurable.getConfig();
207207
}
208208
if (config == null) {
209209
config = createRequestConfig(client);
@@ -328,8 +328,8 @@ protected HttpContext createHttpContext(HttpMethod httpMethod, URI uri) {
328328
@Override
329329
public void destroy() throws Exception {
330330
HttpClient httpClient = getHttpClient();
331-
if (httpClient instanceof Closeable) {
332-
((Closeable) httpClient).close();
331+
if (httpClient instanceof Closeable closeable) {
332+
closeable.close();
333333
}
334334
}
335335

spring-web/src/main/java/org/springframework/http/client/reactive/HttpComponentsClientHttpConnector.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,8 @@ public void completed(Message<HttpResponse, Publisher<ByteBuffer>> result) {
159159
@Override
160160
public void failed(Exception ex) {
161161
Throwable t = ex;
162-
if (t instanceof HttpStreamResetException) {
163-
HttpStreamResetException httpStreamResetException = (HttpStreamResetException) ex;
164-
t = httpStreamResetException.getCause();
162+
if (t instanceof HttpStreamResetException hsre) {
163+
t = hsre.getCause();
165164
}
166165
this.sink.error(t);
167166
}

spring-web/src/main/java/org/springframework/http/client/reactive/JettyResourceFactory.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -131,25 +131,25 @@ public void afterPropertiesSet() throws Exception {
131131
}
132132
if (this.byteBufferPool == null) {
133133
this.byteBufferPool = new MappedByteBufferPool(2048,
134-
this.executor instanceof ThreadPool.SizedThreadPool
135-
? ((ThreadPool.SizedThreadPool) this.executor).getMaxThreads() / 2
134+
this.executor instanceof ThreadPool.SizedThreadPool sizedThreadPool
135+
? sizedThreadPool.getMaxThreads() / 2
136136
: ProcessorUtils.availableProcessors() * 2);
137137
}
138138
if (this.scheduler == null) {
139139
this.scheduler = new ScheduledExecutorScheduler(name + "-scheduler", false);
140140
}
141141

142-
if (this.executor instanceof LifeCycle) {
143-
((LifeCycle)this.executor).start();
142+
if (this.executor instanceof LifeCycle lifeCycle) {
143+
lifeCycle.start();
144144
}
145145
this.scheduler.start();
146146
}
147147

148148
@Override
149149
public void destroy() throws Exception {
150150
try {
151-
if (this.executor instanceof LifeCycle) {
152-
((LifeCycle)this.executor).stop();
151+
if (this.executor instanceof LifeCycle lifeCycle) {
152+
lifeCycle.stop();
153153
}
154154
}
155155
catch (Throwable ex) {

spring-web/src/main/java/org/springframework/http/codec/DecoderHttpMessageReader.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,10 @@ public DecoderHttpMessageReader(Decoder<T> decoder) {
6767
}
6868

6969
private static void initLogger(Decoder<?> decoder) {
70-
if (decoder instanceof AbstractDecoder &&
70+
if (decoder instanceof AbstractDecoder<?> abstractDecoder &&
7171
decoder.getClass().getName().startsWith("org.springframework.core.codec")) {
72-
Log logger = HttpLogging.forLog(((AbstractDecoder<?>) decoder).getLogger());
73-
((AbstractDecoder<?>) decoder).setLogger(logger);
72+
Log logger = HttpLogging.forLog(abstractDecoder.getLogger());
73+
abstractDecoder.setLogger(logger);
7474
}
7575
}
7676

@@ -163,9 +163,8 @@ public Mono<T> readMono(ResolvableType actualType, ResolvableType elementType,
163163
protected Map<String, Object> getReadHints(ResolvableType actualType,
164164
ResolvableType elementType, ServerHttpRequest request, ServerHttpResponse response) {
165165

166-
if (this.decoder instanceof HttpMessageDecoder) {
167-
HttpMessageDecoder<?> decoder = (HttpMessageDecoder<?>) this.decoder;
168-
return decoder.getDecodeHints(actualType, elementType, request, response);
166+
if (this.decoder instanceof HttpMessageDecoder<?> httpMethodDecoder) {
167+
return httpMethodDecoder.getDecodeHints(actualType, elementType, request, response);
169168
}
170169
return Hints.none();
171170
}

spring-web/src/main/java/org/springframework/http/codec/EncoderHttpMessageWriter.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,10 @@ public EncoderHttpMessageWriter(Encoder<T> encoder) {
7979
}
8080

8181
private static void initLogger(Encoder<?> encoder) {
82-
if (encoder instanceof AbstractEncoder &&
82+
if (encoder instanceof AbstractEncoder<?> abstractEncoder &&
8383
encoder.getClass().getName().startsWith("org.springframework.core.codec")) {
84-
Log logger = HttpLogging.forLog(((AbstractEncoder<?>) encoder).getLogger());
85-
((AbstractEncoder<?>) encoder).setLogger(logger);
84+
Log logger = HttpLogging.forLog(abstractEncoder.getLogger());
85+
abstractEncoder.setLogger(logger);
8686
}
8787
}
8888

@@ -224,9 +224,8 @@ public Mono<Void> write(Publisher<? extends T> inputStream, ResolvableType actua
224224
protected Map<String, Object> getWriteHints(ResolvableType streamType, ResolvableType elementType,
225225
@Nullable MediaType mediaType, ServerHttpRequest request, ServerHttpResponse response) {
226226

227-
if (this.encoder instanceof HttpMessageEncoder) {
228-
HttpMessageEncoder<?> encoder = (HttpMessageEncoder<?>) this.encoder;
229-
return encoder.getEncodeHints(streamType, elementType, mediaType, request, response);
227+
if (this.encoder instanceof HttpMessageEncoder<?> httpMessageEncoder) {
228+
return httpMessageEncoder.getEncodeHints(streamType, elementType, mediaType, request, response);
230229
}
231230
return Hints.none();
232231
}

spring-web/src/main/java/org/springframework/http/codec/ResourceHttpMessageWriter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ private static long lengthOf(Resource resource) {
179179
private static Optional<Mono<Void>> zeroCopy(Resource resource, @Nullable ResourceRegion region,
180180
ReactiveHttpOutputMessage message, Map<String, Object> hints) {
181181

182-
if (message instanceof ZeroCopyHttpOutputMessage && resource.isFile()) {
182+
if (message instanceof ZeroCopyHttpOutputMessage zeroCopyHttpOutputMessage && resource.isFile()) {
183183
try {
184184
File file = resource.getFile();
185185
long pos = region != null ? region.getPosition() : 0;
@@ -188,7 +188,7 @@ private static Optional<Mono<Void>> zeroCopy(Resource resource, @Nullable Resour
188188
String formatted = region != null ? "region " + pos + "-" + (count) + " of " : "";
189189
logger.debug(Hints.getLogPrefix(hints) + "Zero-copy " + formatted + "[" + resource + "]");
190190
}
191-
return Optional.of(((ZeroCopyHttpOutputMessage) message).writeWith(file, pos, count));
191+
return Optional.of(zeroCopyHttpOutputMessage.writeWith(file, pos, count));
192192
}
193193
catch (IOException ex) {
194194
// should not happen

spring-web/src/main/java/org/springframework/http/codec/ServerSentEventHttpMessageWriter.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ private Flux<Publisher<DataBuffer>> encode(Publisher<?> input, ResolvableType el
121121

122122
return Flux.from(input).map(element -> {
123123

124-
ServerSentEvent<?> sse = (element instanceof ServerSentEvent ?
125-
(ServerSentEvent<?>) element : ServerSentEvent.builder().data(element).build());
124+
ServerSentEvent<?> sse = (element instanceof ServerSentEvent<?> serverSentEvent ?
125+
serverSentEvent : ServerSentEvent.builder().data(element).build());
126126

127127
StringBuilder sb = new StringBuilder();
128128
String id = sse.id();
@@ -150,9 +150,9 @@ private Flux<Publisher<DataBuffer>> encode(Publisher<?> input, ResolvableType el
150150
if (data == null) {
151151
result = Flux.just(encodeText(sb + "\n", mediaType, factory));
152152
}
153-
else if (data instanceof String) {
154-
data = StringUtils.replace((String) data, "\n", "\ndata:");
155-
result = Flux.just(encodeText(sb + (String) data + "\n\n", mediaType, factory));
153+
else if (data instanceof String dataString) {
154+
dataString = StringUtils.replace(dataString, "\n", "\ndata:");
155+
result = Flux.just(encodeText(sb + dataString + "\n\n", mediaType, factory));
156156
}
157157
else {
158158
result = encodeEvent(sb, data, dataType, mediaType, factory, hints);
@@ -203,9 +203,8 @@ public Mono<Void> write(Publisher<?> input, ResolvableType actualType, Resolvabl
203203
private Map<String, Object> getEncodeHints(ResolvableType actualType, ResolvableType elementType,
204204
@Nullable MediaType mediaType, ServerHttpRequest request, ServerHttpResponse response) {
205205

206-
if (this.encoder instanceof HttpMessageEncoder) {
207-
HttpMessageEncoder<?> encoder = (HttpMessageEncoder<?>) this.encoder;
208-
return encoder.getEncodeHints(actualType, elementType, mediaType, request, response);
206+
if (this.encoder instanceof HttpMessageEncoder<?> httpMessageEncoder) {
207+
return httpMessageEncoder.getEncodeHints(actualType, elementType, mediaType, request, response);
209208
}
210209
return Hints.none();
211210
}

spring-web/src/main/java/org/springframework/http/codec/json/AbstractJackson2Decoder.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -264,12 +264,12 @@ private void logValue(@Nullable Object value, @Nullable Map<String, Object> hint
264264
}
265265

266266
private CodecException processException(IOException ex) {
267-
if (ex instanceof InvalidDefinitionException) {
268-
JavaType type = ((InvalidDefinitionException) ex).getType();
267+
if (ex instanceof InvalidDefinitionException ide) {
268+
JavaType type = ide.getType();
269269
return new CodecException("Type definition error: " + type, ex);
270270
}
271-
if (ex instanceof JsonProcessingException) {
272-
String originalMessage = ((JsonProcessingException) ex).getOriginalMessage();
271+
if (ex instanceof JsonProcessingException jpe) {
272+
String originalMessage = jpe.getOriginalMessage();
273273
return new DecodingException("JSON decoding error: " + originalMessage, ex);
274274
}
275275
return new DecodingException("I/O error while parsing input stream", ex);

spring-web/src/main/java/org/springframework/http/codec/json/Jackson2CodecSupport.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ protected Map<String, Object> getHints(ResolvableType resolvableType) {
255255

256256
@Nullable
257257
protected MethodParameter getParameter(ResolvableType type) {
258-
return (type.getSource() instanceof MethodParameter ? (MethodParameter) type.getSource() : null);
258+
return (type.getSource() instanceof MethodParameter methodParameter ? methodParameter : null);
259259
}
260260

261261
@Nullable

spring-web/src/main/java/org/springframework/http/codec/multipart/MultipartHttpMessageReader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ private LinkedMultiValueMap<String, Part> toMultiValueMap(Map<String, Collection
129129
}
130130

131131
private List<Part> toList(Collection<Part> collection) {
132-
return collection instanceof List ? (List<Part>) collection : new ArrayList<>(collection);
132+
return collection instanceof List<Part> partList ? partList : new ArrayList<>(collection);
133133
}
134134

135135
}

0 commit comments

Comments
 (0)