Skip to content

Commit d7058bb

Browse files
Apply Java pattern matching
1 parent 8f8e1d1 commit d7058bb

File tree

26 files changed

+107
-113
lines changed

26 files changed

+107
-113
lines changed

spring-amqp/src/main/java/org/springframework/amqp/core/MessageProperties.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -447,12 +447,10 @@ public void setConsumerQueue(String consumerQueue) {
447447
*/
448448
public Long getDelayLong() {
449449
Object delay = this.headers.get(X_DELAY);
450-
if (delay instanceof Long) {
451-
return (Long) delay;
452-
}
453-
else {
454-
return null;
450+
if (delay instanceof Long delayLong) {
451+
return delayLong;
455452
}
453+
return null;
456454
}
457455

458456
/**

spring-amqp/src/main/java/org/springframework/amqp/support/AmqpMessageHeaderAccessor.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2019 the original author or authors.
2+
* Copyright 2014-2024 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.
@@ -87,12 +87,10 @@ public Long getContentLength() {
8787
@Override
8888
public MimeType getContentType() {
8989
Object value = getHeader(AmqpHeaders.CONTENT_TYPE);
90-
if (value instanceof String) {
91-
return MimeType.valueOf((String) value);
92-
}
93-
else {
94-
return super.getContentType();
90+
if (value instanceof String contentType) {
91+
return MimeType.valueOf(contentType);
9592
}
93+
return super.getContentType();
9694
}
9795

9896
public String getCorrelationId() {

spring-amqp/src/main/java/org/springframework/amqp/support/SimpleAmqpHeaderMapper.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ public void fromHeaders(MessageHeaders headers, MessageProperties amqpMessagePro
6666
amqpMessageProperties::setContentLength)
6767
.acceptIfHasText(extractContentTypeAsString(headers), amqpMessageProperties::setContentType);
6868
Object correlationId = headers.get(AmqpHeaders.CORRELATION_ID);
69-
if (correlationId instanceof String) {
70-
amqpMessageProperties.setCorrelationId((String) correlationId);
69+
if (correlationId instanceof String string) {
70+
amqpMessageProperties.setCorrelationId(string);
7171
}
7272
javaUtils
7373
.acceptIfNotNull(getHeaderIfAvailable(headers, AmqpHeaders.DELAY, Long.class),
@@ -195,8 +195,8 @@ private String extractContentTypeAsString(Map<String, Object> headers) {
195195
if (contentType instanceof MimeType) {
196196
contentTypeStringValue = contentType.toString();
197197
}
198-
else if (contentType instanceof String) {
199-
contentTypeStringValue = (String) contentType;
198+
else if (contentType instanceof String string) {
199+
contentTypeStringValue = string;
200200
}
201201
else {
202202
if (logger.isWarnEnabled()) {

spring-amqp/src/main/java/org/springframework/amqp/support/converter/AbstractJackson2MessageConverter.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018-2022 the original author or authors.
2+
* Copyright 2018-2024 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.
@@ -246,8 +246,8 @@ public void setTypePrecedence(Jackson2JavaTypeMapper.TypePrecedence typePreceden
246246
if (this.typeMapperSet) {
247247
throw new IllegalStateException("When providing your own type mapper, you should set the precedence on it");
248248
}
249-
if (this.javaTypeMapper instanceof DefaultJackson2JavaTypeMapper) {
250-
((DefaultJackson2JavaTypeMapper) this.javaTypeMapper).setTypePrecedence(typePrecedence);
249+
if (this.javaTypeMapper instanceof DefaultJackson2JavaTypeMapper defaultJackson2JavaTypeMapper) {
250+
defaultJackson2JavaTypeMapper.setTypePrecedence(typePrecedence);
251251
}
252252
else {
253253
throw new IllegalStateException("Type precedence is available with the DefaultJackson2JavaTypeMapper");
@@ -384,10 +384,10 @@ else if (inferredType != null && this.alwaysConvertToInferredType) {
384384
content = tryConverType(message, encoding, inferredType);
385385
}
386386
if (content == null) {
387-
if (conversionHint instanceof ParameterizedTypeReference) {
387+
if (conversionHint instanceof ParameterizedTypeReference<?> parameterizedTypeReference) {
388388
content = convertBytesToObject(message.getBody(), encoding,
389389
this.objectMapper.getTypeFactory().constructType(
390-
((ParameterizedTypeReference<?>) conversionHint).getType()));
390+
parameterizedTypeReference.getType()));
391391
}
392392
else if (getClassMapper() == null) {
393393
JavaType targetJavaType = getJavaTypeMapper()

spring-amqp/src/main/java/org/springframework/amqp/support/converter/RemoteInvocationAwareMessageConverterAdapter.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2022 the original author or authors.
2+
* Copyright 2016-2024 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.
@@ -51,9 +51,9 @@ public Message toMessage(Object object, MessageProperties messageProperties) thr
5151
@Override
5252
public Object fromMessage(Message message) throws MessageConversionException {
5353
Object result = this.delegate.fromMessage(message);
54-
if (result instanceof RemoteInvocationResult) {
54+
if (result instanceof RemoteInvocationResult remoteInvocationResult) {
5555
try {
56-
result = ((RemoteInvocationResult) result).recreate();
56+
result = remoteInvocationResult.recreate();
5757
if (result == null) {
5858
throw new MessageConversionException("RemoteInvocationResult returned null");
5959
}

spring-amqp/src/main/java/org/springframework/amqp/support/converter/RemoteInvocationResult.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2021 the original author or authors.
2+
* Copyright 2021-2024 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.
@@ -143,8 +143,8 @@ public boolean hasInvocationTargetException() {
143143
public Object recreate() throws Throwable {
144144
if (this.exception != null) {
145145
Throwable exToThrow = this.exception;
146-
if (this.exception instanceof InvocationTargetException) {
147-
exToThrow = ((InvocationTargetException) this.exception).getTargetException();
146+
if (this.exception instanceof InvocationTargetException invocationTargetException) {
147+
exToThrow = invocationTargetException.getTargetException();
148148
}
149149
RemoteInvocationUtils.fillInClientStackTraceIfPossible(exToThrow);
150150
throw exToThrow;

spring-amqp/src/main/java/org/springframework/amqp/support/converter/SerializerMessageConverter.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,18 +187,18 @@ protected Message createMessage(Object object, MessageProperties messageProperti
187187
throws MessageConversionException {
188188

189189
byte[] bytes;
190-
if (object instanceof String) {
190+
if (object instanceof String string) {
191191
try {
192-
bytes = ((String) object).getBytes(this.defaultCharset);
192+
bytes = string.getBytes(this.defaultCharset);
193193
}
194194
catch (UnsupportedEncodingException e) {
195195
throw new MessageConversionException("failed to convert Message content", e);
196196
}
197197
messageProperties.setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN);
198198
messageProperties.setContentEncoding(this.defaultCharset);
199199
}
200-
else if (object instanceof byte[]) {
201-
bytes = (byte[]) object;
200+
else if (object instanceof byte[] objectBytes) {
201+
bytes = objectBytes;
202202
messageProperties.setContentType(MessageProperties.CONTENT_TYPE_BYTES);
203203
}
204204
else {

spring-amqp/src/main/java/org/springframework/amqp/support/converter/SimpleMessageConverter.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,13 +112,13 @@ protected Message createMessage(Object object, MessageProperties messageProperti
112112
throws MessageConversionException {
113113

114114
byte[] bytes = null;
115-
if (object instanceof byte[]) {
116-
bytes = (byte[]) object;
115+
if (object instanceof byte[] objectBytes) {
116+
bytes = objectBytes;
117117
messageProperties.setContentType(MessageProperties.CONTENT_TYPE_BYTES);
118118
}
119-
else if (object instanceof String) {
119+
else if (object instanceof String string) {
120120
try {
121-
bytes = ((String) object).getBytes(this.defaultCharset);
121+
bytes = string.getBytes(this.defaultCharset);
122122
}
123123
catch (UnsupportedEncodingException e) {
124124
throw new MessageConversionException("failed to convert to Message content", e);

spring-amqp/src/main/java/org/springframework/amqp/support/postprocessor/AbstractDecompressingPostProcessor.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2020 the original author or authors.
2+
* Copyright 2014-2024 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.
@@ -83,7 +83,7 @@ protected void setOrder(int order) {
8383
public Message postProcessMessage(Message message) throws AmqpException {
8484
Object autoDecompress = message.getMessageProperties().getHeaders()
8585
.get(MessageProperties.SPRING_AUTO_DECOMPRESS);
86-
if (this.alwaysDecompress || (autoDecompress instanceof Boolean && ((Boolean) autoDecompress))) {
86+
if (this.alwaysDecompress || (autoDecompress instanceof Boolean isAutoDecompress && isAutoDecompress)) {
8787
ByteArrayInputStream zipped = new ByteArrayInputStream(message.getBody());
8888
try {
8989
InputStream unzipper = getDecompressorStream(zipped);

spring-amqp/src/test/java/org/springframework/amqp/support/converter/AllowedListDeserializingMessageConverterTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2016-2023 the original author or authors.
2+
* Copyright 2016-2024 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.
@@ -74,7 +74,7 @@ protected TestBean(String text) {
7474

7575
@Override
7676
public boolean equals(Object other) {
77-
return (other instanceof TestBean && this.text.equals(((TestBean) other).text));
77+
return (other instanceof TestBean testBean && this.text.equals(testBean.text));
7878
}
7979

8080
@Override

0 commit comments

Comments
 (0)