Apply "instanceof pattern matching" in spring-jms

This commit also applies additional clean-up tasks such as the following.

- final fields

This has only been applied to `src/main/java`.
This commit is contained in:
Sam Brannen 2021-10-14 19:58:22 +02:00
parent 971f665eb1
commit d70a610a0d
9 changed files with 20 additions and 29 deletions

View File

@ -598,10 +598,9 @@ public class CachingConnectionFactory extends SingleConnectionFactory {
if (this == other) { if (this == other) {
return true; return true;
} }
if (!(other instanceof ConsumerCacheKey)) { if (!(other instanceof ConsumerCacheKey otherKey)) {
return false; return false;
} }
ConsumerCacheKey otherKey = (ConsumerCacheKey) other;
return (destinationEquals(otherKey) && return (destinationEquals(otherKey) &&
ObjectUtils.nullSafeEquals(this.selector, otherKey.selector) && ObjectUtils.nullSafeEquals(this.selector, otherKey.selector) &&
ObjectUtils.nullSafeEquals(this.noLocal, otherKey.noLocal) && ObjectUtils.nullSafeEquals(this.noLocal, otherKey.noLocal) &&

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2018 the original author or authors. * Copyright 2002-2021 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.
@ -232,10 +232,9 @@ public class UserCredentialsConnectionFactoryAdapter
@Nullable String username, @Nullable String password) throws JMSException { @Nullable String username, @Nullable String password) throws JMSException {
ConnectionFactory target = obtainTargetConnectionFactory(); ConnectionFactory target = obtainTargetConnectionFactory();
if (!(target instanceof QueueConnectionFactory)) { if (!(target instanceof QueueConnectionFactory queueFactory)) {
throw new jakarta.jms.IllegalStateException("'targetConnectionFactory' is not a QueueConnectionFactory"); throw new jakarta.jms.IllegalStateException("'targetConnectionFactory' is not a QueueConnectionFactory");
} }
QueueConnectionFactory queueFactory = (QueueConnectionFactory) target;
if (StringUtils.hasLength(username)) { if (StringUtils.hasLength(username)) {
return queueFactory.createQueueConnection(username, password); return queueFactory.createQueueConnection(username, password);
} }
@ -284,10 +283,9 @@ public class UserCredentialsConnectionFactoryAdapter
@Nullable String username, @Nullable String password) throws JMSException { @Nullable String username, @Nullable String password) throws JMSException {
ConnectionFactory target = obtainTargetConnectionFactory(); ConnectionFactory target = obtainTargetConnectionFactory();
if (!(target instanceof TopicConnectionFactory)) { if (!(target instanceof TopicConnectionFactory queueFactory)) {
throw new jakarta.jms.IllegalStateException("'targetConnectionFactory' is not a TopicConnectionFactory"); throw new jakarta.jms.IllegalStateException("'targetConnectionFactory' is not a TopicConnectionFactory");
} }
TopicConnectionFactory queueFactory = (TopicConnectionFactory) target;
if (StringUtils.hasLength(username)) { if (StringUtils.hasLength(username)) {
return queueFactory.createTopicConnection(username, password); return queueFactory.createTopicConnection(username, password);
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2017 the original author or authors. * Copyright 2002-2021 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.
@ -90,7 +90,7 @@ public abstract class AbstractPollingMessageListenerContainer extends AbstractMe
@Nullable @Nullable
private PlatformTransactionManager transactionManager; private PlatformTransactionManager transactionManager;
private DefaultTransactionDefinition transactionDefinition = new DefaultTransactionDefinition(); private final DefaultTransactionDefinition transactionDefinition = new DefaultTransactionDefinition();
private long receiveTimeout = DEFAULT_RECEIVE_TIMEOUT; private long receiveTimeout = DEFAULT_RECEIVE_TIMEOUT;

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2020 the original author or authors. * Copyright 2002-2021 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.
@ -411,8 +411,7 @@ public abstract class AbstractAdaptableMessageListener
if (this.defaultResponseDestination instanceof Destination) { if (this.defaultResponseDestination instanceof Destination) {
return (Destination) this.defaultResponseDestination; return (Destination) this.defaultResponseDestination;
} }
if (this.defaultResponseDestination instanceof DestinationNameHolder) { if (this.defaultResponseDestination instanceof DestinationNameHolder nameHolder) {
DestinationNameHolder nameHolder = (DestinationNameHolder) this.defaultResponseDestination;
return getDestinationResolver().resolveDestinationName(session, nameHolder.name, nameHolder.isTopic); return getDestinationResolver().resolveDestinationName(session, nameHolder.name, nameHolder.isTopic);
} }
return null; return null;

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2018 the original author or authors. * Copyright 2002-2021 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.
@ -94,8 +94,7 @@ public class JmsResponse<T> {
if (this.destination instanceof Destination) { if (this.destination instanceof Destination) {
return (Destination) this.destination; return (Destination) this.destination;
} }
if (this.destination instanceof DestinationNameHolder) { if (this.destination instanceof DestinationNameHolder nameHolder) {
DestinationNameHolder nameHolder = (DestinationNameHolder) this.destination;
return destinationResolver.resolveDestinationName(session, return destinationResolver.resolveDestinationName(session,
nameHolder.destinationName, nameHolder.pubSubDomain); nameHolder.destinationName, nameHolder.pubSubDomain);
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2017 the original author or authors. * Copyright 2002-2021 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.
@ -114,11 +114,10 @@ public class QosSettings {
if (this == other) { if (this == other) {
return true; return true;
} }
if (!(other instanceof QosSettings)) { if (!(other instanceof QosSettings otherSettings)) {
return false; return false;
} }
QosSettings otherSettings = (QosSettings) other;
return (this.deliveryMode == otherSettings.deliveryMode && return (this.deliveryMode == otherSettings.deliveryMode &&
this.priority == otherSettings.priority && this.priority == otherSettings.priority &&
this.timeToLive == otherSettings.timeToLive); this.timeToLive == otherSettings.timeToLive);

View File

@ -85,7 +85,7 @@ public class MappingJackson2MessageConverter implements SmartMessageConverter, B
private Map<String, Class<?>> idClassMappings = new HashMap<>(); private Map<String, Class<?>> idClassMappings = new HashMap<>();
private Map<Class<?>, String> classIdMappings = new HashMap<>(); private final Map<Class<?>, String> classIdMappings = new HashMap<>();
@Nullable @Nullable
private ClassLoader beanClassLoader; private ClassLoader beanClassLoader;
@ -476,8 +476,7 @@ public class MappingJackson2MessageConverter implements SmartMessageConverter, B
*/ */
@Nullable @Nullable
protected Class<?> getSerializationView(@Nullable Object conversionHint) { protected Class<?> getSerializationView(@Nullable Object conversionHint) {
if (conversionHint instanceof MethodParameter) { if (conversionHint instanceof MethodParameter methodParam) {
MethodParameter methodParam = (MethodParameter) conversionHint;
JsonView annotation = methodParam.getParameterAnnotation(JsonView.class); JsonView annotation = methodParam.getParameterAnnotation(JsonView.class);
if (annotation == null) { if (annotation == null) {
annotation = methodParam.getMethodAnnotation(JsonView.class); annotation = methodParam.getMethodAnnotation(JsonView.class);

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2020 the original author or authors. * Copyright 2002-2021 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.
@ -178,12 +178,10 @@ public class MarshallingMessageConverter implements MessageConverter, Initializi
public Object fromMessage(Message message) throws JMSException, MessageConversionException { public Object fromMessage(Message message) throws JMSException, MessageConversionException {
Assert.state(this.unmarshaller != null, "No Unmarshaller set"); Assert.state(this.unmarshaller != null, "No Unmarshaller set");
try { try {
if (message instanceof TextMessage) { if (message instanceof TextMessage textMessage) {
TextMessage textMessage = (TextMessage) message;
return unmarshalFromTextMessage(textMessage, this.unmarshaller); return unmarshalFromTextMessage(textMessage, this.unmarshaller);
} }
else if (message instanceof BytesMessage) { else if (message instanceof BytesMessage bytesMessage) {
BytesMessage bytesMessage = (BytesMessage) message;
return unmarshalFromBytesMessage(bytesMessage, this.unmarshaller); return unmarshalFromBytesMessage(bytesMessage, this.unmarshaller);
} }
else { else {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2018 the original author or authors. * Copyright 2002-2021 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.
@ -27,7 +27,7 @@ import org.springframework.jms.support.SimpleJmsHeaderMapper;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
import org.springframework.messaging.Message; import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders; import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.core.AbstractMessagingTemplate; import org.springframework.messaging.core.AbstractMessageSendingTemplate;
import org.springframework.messaging.support.MessageBuilder; import org.springframework.messaging.support.MessageBuilder;
import org.springframework.util.Assert; import org.springframework.util.Assert;
@ -107,7 +107,7 @@ public class MessagingMessageConverter implements MessageConverter, Initializing
} }
Message<?> input = (Message<?>) object; Message<?> input = (Message<?>) object;
MessageHeaders headers = input.getHeaders(); MessageHeaders headers = input.getHeaders();
Object conversionHint = headers.get(AbstractMessagingTemplate.CONVERSION_HINT_HEADER); Object conversionHint = headers.get(AbstractMessageSendingTemplate.CONVERSION_HINT_HEADER);
jakarta.jms.Message reply = createMessageForPayload(input.getPayload(), session, conversionHint); jakarta.jms.Message reply = createMessageForPayload(input.getPayload(), session, conversionHint);
this.headerMapper.fromHeaders(headers, reply); this.headerMapper.fromHeaders(headers, reply);
return reply; return reply;