Skip to content

Apply pattern matching #2808

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -447,12 +447,10 @@ public void setConsumerQueue(String consumerQueue) {
*/
public Long getDelayLong() {
Object delay = this.headers.get(X_DELAY);
if (delay instanceof Long) {
return (Long) delay;
}
else {
return null;
if (delay instanceof Long delayLong) {
return delayLong;
}
return null;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2019 the original author or authors.
* Copyright 2014-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -87,12 +87,10 @@ public Long getContentLength() {
@Override
public MimeType getContentType() {
Object value = getHeader(AmqpHeaders.CONTENT_TYPE);
if (value instanceof String) {
return MimeType.valueOf((String) value);
}
else {
return super.getContentType();
if (value instanceof String contentType) {
return MimeType.valueOf(contentType);
}
return super.getContentType();
}

public String getCorrelationId() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ public void fromHeaders(MessageHeaders headers, MessageProperties amqpMessagePro
amqpMessageProperties::setContentLength)
.acceptIfHasText(extractContentTypeAsString(headers), amqpMessageProperties::setContentType);
Object correlationId = headers.get(AmqpHeaders.CORRELATION_ID);
if (correlationId instanceof String) {
amqpMessageProperties.setCorrelationId((String) correlationId);
if (correlationId instanceof String string) {
amqpMessageProperties.setCorrelationId(string);
}
javaUtils
.acceptIfNotNull(getHeaderIfAvailable(headers, AmqpHeaders.DELAY, Long.class),
Expand Down Expand Up @@ -195,8 +195,8 @@ private String extractContentTypeAsString(Map<String, Object> headers) {
if (contentType instanceof MimeType) {
contentTypeStringValue = contentType.toString();
}
else if (contentType instanceof String) {
contentTypeStringValue = (String) contentType;
else if (contentType instanceof String string) {
contentTypeStringValue = string;
}
else {
if (logger.isWarnEnabled()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2018-2022 the original author or authors.
* Copyright 2018-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -246,8 +246,8 @@ public void setTypePrecedence(Jackson2JavaTypeMapper.TypePrecedence typePreceden
if (this.typeMapperSet) {
throw new IllegalStateException("When providing your own type mapper, you should set the precedence on it");
}
if (this.javaTypeMapper instanceof DefaultJackson2JavaTypeMapper) {
((DefaultJackson2JavaTypeMapper) this.javaTypeMapper).setTypePrecedence(typePrecedence);
if (this.javaTypeMapper instanceof DefaultJackson2JavaTypeMapper defaultJackson2JavaTypeMapper) {
defaultJackson2JavaTypeMapper.setTypePrecedence(typePrecedence);
}
else {
throw new IllegalStateException("Type precedence is available with the DefaultJackson2JavaTypeMapper");
Expand Down Expand Up @@ -384,10 +384,10 @@ else if (inferredType != null && this.alwaysConvertToInferredType) {
content = tryConverType(message, encoding, inferredType);
}
if (content == null) {
if (conversionHint instanceof ParameterizedTypeReference) {
if (conversionHint instanceof ParameterizedTypeReference<?> parameterizedTypeReference) {
content = convertBytesToObject(message.getBody(), encoding,
this.objectMapper.getTypeFactory().constructType(
((ParameterizedTypeReference<?>) conversionHint).getType()));
parameterizedTypeReference.getType()));
}
else if (getClassMapper() == null) {
JavaType targetJavaType = getJavaTypeMapper()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2022 the original author or authors.
* Copyright 2016-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -51,9 +51,9 @@ public Message toMessage(Object object, MessageProperties messageProperties) thr
@Override
public Object fromMessage(Message message) throws MessageConversionException {
Object result = this.delegate.fromMessage(message);
if (result instanceof RemoteInvocationResult) {
if (result instanceof RemoteInvocationResult remoteInvocationResult) {
try {
result = ((RemoteInvocationResult) result).recreate();
result = remoteInvocationResult.recreate();
if (result == null) {
throw new MessageConversionException("RemoteInvocationResult returned null");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021 the original author or authors.
* Copyright 2021-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -143,8 +143,8 @@ public boolean hasInvocationTargetException() {
public Object recreate() throws Throwable {
if (this.exception != null) {
Throwable exToThrow = this.exception;
if (this.exception instanceof InvocationTargetException) {
exToThrow = ((InvocationTargetException) this.exception).getTargetException();
if (this.exception instanceof InvocationTargetException invocationTargetException) {
exToThrow = invocationTargetException.getTargetException();
}
RemoteInvocationUtils.fillInClientStackTraceIfPossible(exToThrow);
throw exToThrow;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,18 +187,18 @@ protected Message createMessage(Object object, MessageProperties messageProperti
throws MessageConversionException {

byte[] bytes;
if (object instanceof String) {
if (object instanceof String string) {
try {
bytes = ((String) object).getBytes(this.defaultCharset);
bytes = string.getBytes(this.defaultCharset);
}
catch (UnsupportedEncodingException e) {
throw new MessageConversionException("failed to convert Message content", e);
}
messageProperties.setContentType(MessageProperties.CONTENT_TYPE_TEXT_PLAIN);
messageProperties.setContentEncoding(this.defaultCharset);
}
else if (object instanceof byte[]) {
bytes = (byte[]) object;
else if (object instanceof byte[] objectBytes) {
bytes = objectBytes;
messageProperties.setContentType(MessageProperties.CONTENT_TYPE_BYTES);
}
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,13 @@ protected Message createMessage(Object object, MessageProperties messageProperti
throws MessageConversionException {

byte[] bytes = null;
if (object instanceof byte[]) {
bytes = (byte[]) object;
if (object instanceof byte[] objectBytes) {
bytes = objectBytes;
messageProperties.setContentType(MessageProperties.CONTENT_TYPE_BYTES);
}
else if (object instanceof String) {
else if (object instanceof String string) {
try {
bytes = ((String) object).getBytes(this.defaultCharset);
bytes = string.getBytes(this.defaultCharset);
}
catch (UnsupportedEncodingException e) {
throw new MessageConversionException("failed to convert to Message content", e);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014-2020 the original author or authors.
* Copyright 2014-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -83,7 +83,7 @@ protected void setOrder(int order) {
public Message postProcessMessage(Message message) throws AmqpException {
Object autoDecompress = message.getMessageProperties().getHeaders()
.get(MessageProperties.SPRING_AUTO_DECOMPRESS);
if (this.alwaysDecompress || (autoDecompress instanceof Boolean && ((Boolean) autoDecompress))) {
if (this.alwaysDecompress || (autoDecompress instanceof Boolean isAutoDecompress && isAutoDecompress)) {
ByteArrayInputStream zipped = new ByteArrayInputStream(message.getBody());
try {
InputStream unzipper = getDecompressorStream(zipped);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2016-2023 the original author or authors.
* Copyright 2016-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -74,7 +74,7 @@ protected TestBean(String text) {

@Override
public boolean equals(Object other) {
return (other instanceof TestBean && this.text.equals(((TestBean) other).text));
return (other instanceof TestBean testBean && this.text.equals(testBean.text));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021-2023 the original author or authors.
* Copyright 2021-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -104,8 +104,8 @@ public void setStreamListenerObservationConvention(

@Override
public StreamListenerContainer createListenerContainer(RabbitListenerEndpoint endpoint) {
if (endpoint instanceof MethodRabbitListenerEndpoint && this.nativeListener) {
((MethodRabbitListenerEndpoint) endpoint).setAdapterProvider(
if (endpoint instanceof MethodRabbitListenerEndpoint methodRabbitListenerEndpoint && this.nativeListener) {
methodRabbitListenerEndpoint.setAdapterProvider(
(boolean batch, Object bean, Method method, boolean returnExceptions,
RabbitListenerErrorHandler errorHandler, @Nullable BatchingStrategy batchingStrategy) -> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,11 +338,11 @@ public void setupMessageListener(MessageListener messageListener) {
}
else {
Message message2 = this.streamConverter.toMessage(message, new StreamMessageProperties(context));
if (this.messageListener instanceof ChannelAwareMessageListener) {
if (this.messageListener instanceof ChannelAwareMessageListener channelAwareMessageListener) {
try {
observation.observe(() -> {
try {
((ChannelAwareMessageListener) this.messageListener).onMessage(message2, null);
channelAwareMessageListener.onMessage(message2, null);
if (finalSample != null) {
micrometerHolder.success(finalSample, this.streamName);
}
Expand Down Expand Up @@ -375,8 +375,8 @@ public void setupMessageListener(MessageListener messageListener) {

private void adviseIfNeeded(MessageListener messageListener) {
this.messageListener = messageListener;
if (messageListener instanceof StreamMessageListener) {
this.streamListener = (StreamMessageListener) messageListener;
if (messageListener instanceof StreamMessageListener streamMessageListener) {
this.streamListener = streamMessageListener;
}
if (this.adviceChain != null && this.adviceChain.length > 0) {
ProxyFactory factory = new ProxyFactory(messageListener);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021 the original author or authors.
* Copyright 2021-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -115,35 +115,35 @@ public com.rabbitmq.stream.Message fromMessage(Message message) throws MessageCo
}

private void mapProp(String key, Object val, ApplicationPropertiesBuilder builder) { // NOSONAR - complexity
if (val instanceof String) {
builder.entry(key, (String) val);
if (val instanceof String string) {
builder.entry(key, string);
}
else if (val instanceof Long) {
builder.entry(key, (Long) val);
else if (val instanceof Long longValue) {
builder.entry(key, longValue);
}
else if (val instanceof Integer) {
builder.entry(key, (Integer) val);
else if (val instanceof Integer intValue) {
builder.entry(key, intValue);
}
else if (val instanceof Short) {
builder.entry(key, (Short) val);
else if (val instanceof Short shortValue) {
builder.entry(key, shortValue);
}
else if (val instanceof Byte) {
builder.entry(key, (Byte) val);
else if (val instanceof Byte byteValue) {
builder.entry(key, byteValue);
}
else if (val instanceof Double) {
builder.entry(key, (Double) val);
else if (val instanceof Double doubleValue) {
builder.entry(key, doubleValue);
}
else if (val instanceof Float) {
builder.entry(key, (Float) val);
else if (val instanceof Float floatValue) {
builder.entry(key, floatValue);
}
else if (val instanceof Character) {
builder.entry(key, (Character) val);
else if (val instanceof Character character) {
builder.entry(key, character);
}
else if (val instanceof UUID) {
builder.entry(key, (UUID) val);
else if (val instanceof UUID uuid) {
builder.entry(key, uuid);
}
else if (val instanceof byte[]) {
builder.entry(key, (byte[]) val);
else if (val instanceof byte[] bytes) {
builder.entry(key, bytes);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2023 the original author or authors.
* Copyright 2017-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -143,9 +143,8 @@ protected Message doSendAndReceiveWithFixed(String exchange, String routingKey,
Channel channel = mock(Channel.class);
final AtomicReference<Message> reply = new AtomicReference<>();
Object listener = listenersForRoute.next();
if (listener instanceof AbstractAdaptableMessageListener) {
if (listener instanceof AbstractAdaptableMessageListener adapter) {
try {
AbstractAdaptableMessageListener adapter = (AbstractAdaptableMessageListener) listener;
willAnswer(i -> {
Envelope envelope = new Envelope(1, false, "", REPLY_QUEUE);
reply.set(MessageBuilder.withBody(i.getArgument(4)) // NOSONAR magic #
Expand All @@ -170,16 +169,16 @@ protected Message doSendAndReceiveWithFixed(String exchange, String routingKey,
}

private void invoke(Object listener, Message message, Channel channel) {
if (listener instanceof ChannelAwareMessageListener) {
if (listener instanceof ChannelAwareMessageListener channelAwareMessageListener) {
try {
((ChannelAwareMessageListener) listener).onMessage(message, channel);
channelAwareMessageListener.onMessage(message, channel);
}
catch (Exception e) {
throw RabbitExceptionTranslator.convertRabbitAccessException(e);
}
}
else if (listener instanceof MessageListener) {
((MessageListener) listener).onMessage(message);
else if (listener instanceof MessageListener messageListener) {
messageListener.onMessage(message);
}
else {
// Not really necessary since the container doesn't allow it, but no hurt
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2021-2023 the original author or authors.
* Copyright 2021-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -140,8 +140,7 @@ protected void applyCommonOverrides(@Nullable RabbitListenerEndpoint endpoint, C
endpoint.setupListenerContainer(instance);
}
Object iml = instance.getMessageListener();
if (iml instanceof AbstractAdaptableMessageListener) {
AbstractAdaptableMessageListener messageListener = (AbstractAdaptableMessageListener) iml;
if (iml instanceof AbstractAdaptableMessageListener messageListener) {
JavaUtils.INSTANCE // NOSONAR
.acceptIfNotNull(this.beforeSendReplyPostProcessors,
messageListener::setBeforeSendReplyPostProcessors)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -74,11 +74,11 @@ protected Object recover(Object[] args, Throwable cause) {
if (messageRecoverer == null) {
this.logger.warn("Message(s) dropped on recovery: " + arg, cause);
}
else if (arg instanceof Message) {
messageRecoverer.recover((Message) arg, cause);
else if (arg instanceof Message message) {
messageRecoverer.recover(message, cause);
}
else if (arg instanceof List && messageRecoverer instanceof MessageBatchRecoverer) {
((MessageBatchRecoverer) messageRecoverer).recover((List<Message>) arg, cause);
else if (arg instanceof List && messageRecoverer instanceof MessageBatchRecoverer messageBatchRecoverer) {
messageBatchRecoverer.recover((List<Message>) arg, cause);
}
return null;
}
Expand Down
Loading