diff --git a/spring-core/src/main/java/org/springframework/core/io/support/PropertySourceProcessor.java b/spring-core/src/main/java/org/springframework/core/io/support/PropertySourceProcessor.java index e89169d0cf..0374bbab9e 100644 --- a/spring-core/src/main/java/org/springframework/core/io/support/PropertySourceProcessor.java +++ b/spring-core/src/main/java/org/springframework/core/io/support/PropertySourceProcessor.java @@ -108,14 +108,14 @@ public class PropertySourceProcessor { // We've already added a version, we need to extend it org.springframework.core.env.PropertySource existing = propertySources.get(name); if (existing != null) { - PropertySource newSource = (propertySource instanceof ResourcePropertySource ? - ((ResourcePropertySource) propertySource).withResourceName() : propertySource); - if (existing instanceof CompositePropertySource) { - ((CompositePropertySource) existing).addFirstPropertySource(newSource); + PropertySource newSource = (propertySource instanceof ResourcePropertySource rps ? + rps.withResourceName() : propertySource); + if (existing instanceof CompositePropertySource cps) { + cps.addFirstPropertySource(newSource); } else { - if (existing instanceof ResourcePropertySource) { - existing = ((ResourcePropertySource) existing).withResourceName(); + if (existing instanceof ResourcePropertySource rps) { + existing = rps.withResourceName(); } CompositePropertySource composite = new CompositePropertySource(name); composite.addPropertySource(newSource); diff --git a/spring-jms/src/main/java/org/springframework/jms/listener/adapter/AbstractAdaptableMessageListener.java b/spring-jms/src/main/java/org/springframework/jms/listener/adapter/AbstractAdaptableMessageListener.java index 3a50c92dbd..3ca176ef05 100644 --- a/spring-jms/src/main/java/org/springframework/jms/listener/adapter/AbstractAdaptableMessageListener.java +++ b/spring-jms/src/main/java/org/springframework/jms/listener/adapter/AbstractAdaptableMessageListener.java @@ -316,11 +316,11 @@ public abstract class AbstractAdaptableMessageListener } } - if (!(content instanceof Message)) { + if (!(content instanceof Message message)) { throw new MessageConversionException( "No MessageConverter specified - cannot handle message [" + content + "]"); } - return (Message) content; + return message; } /** @@ -355,8 +355,7 @@ public abstract class AbstractAdaptableMessageListener private Destination getResponseDestination(Message request, Message response, Session session, Object result) throws JMSException { - if (result instanceof JmsResponse) { - JmsResponse jmsResponse = (JmsResponse) result; + if (result instanceof JmsResponse jmsResponse) { Destination destination = jmsResponse.resolveDestination(getDestinationResolver(), session); if (destination != null) { return destination; @@ -408,8 +407,8 @@ public abstract class AbstractAdaptableMessageListener */ @Nullable protected Destination resolveDefaultResponseDestination(Session session) throws JMSException { - if (this.defaultResponseDestination instanceof Destination) { - return (Destination) this.defaultResponseDestination; + if (this.defaultResponseDestination instanceof Destination destination) { + return destination; } if (this.defaultResponseDestination instanceof DestinationNameHolder nameHolder) { return getDestinationResolver().resolveDestinationName(session, nameHolder.name, nameHolder.isTopic); @@ -471,11 +470,11 @@ public abstract class AbstractAdaptableMessageListener @Override protected Object extractPayload(Message message) throws JMSException { Object payload = extractMessage(message); - if (message instanceof BytesMessage) { + if (message instanceof BytesMessage bytesMessage) { try { // In case the BytesMessage is going to be received as a user argument: // reset it, otherwise it would appear empty to such processing code... - ((BytesMessage) message).reset(); + bytesMessage.reset(); } catch (JMSException ex) { // Continue since the BytesMessage typically won't be used any further. @@ -493,8 +492,8 @@ public abstract class AbstractAdaptableMessageListener if (converter == null) { throw new IllegalStateException("No message converter, cannot handle '" + payload + "'"); } - if (converter instanceof SmartMessageConverter) { - return ((SmartMessageConverter) converter).toMessage(payload, session, conversionHint); + if (converter instanceof SmartMessageConverter smartMessageConverter) { + return smartMessageConverter.toMessage(payload, session, conversionHint); } return converter.toMessage(payload, session); @@ -538,8 +537,8 @@ public abstract class AbstractAdaptableMessageListener @SuppressWarnings("rawtypes") private Object unwrapPayload() throws JMSException { Object payload = extractPayload(this.message); - if (payload instanceof org.springframework.messaging.Message) { - return ((org.springframework.messaging.Message) payload).getPayload(); + if (payload instanceof org.springframework.messaging.Message springMessage) { + return springMessage.getPayload(); } return payload; } diff --git a/spring-jms/src/main/java/org/springframework/jms/listener/adapter/JmsResponse.java b/spring-jms/src/main/java/org/springframework/jms/listener/adapter/JmsResponse.java index caa6b4b573..0bd7175016 100644 --- a/spring-jms/src/main/java/org/springframework/jms/listener/adapter/JmsResponse.java +++ b/spring-jms/src/main/java/org/springframework/jms/listener/adapter/JmsResponse.java @@ -91,8 +91,8 @@ public class JmsResponse { public Destination resolveDestination(DestinationResolver destinationResolver, Session session) throws JMSException { - if (this.destination instanceof Destination) { - return (Destination) this.destination; + if (this.destination instanceof Destination dest) { + return dest; } if (this.destination instanceof DestinationNameHolder nameHolder) { return destinationResolver.resolveDestinationName(session, diff --git a/spring-jms/src/main/java/org/springframework/jms/listener/adapter/MessageListenerAdapter.java b/spring-jms/src/main/java/org/springframework/jms/listener/adapter/MessageListenerAdapter.java index 6adbf2a805..e361273c5a 100644 --- a/spring-jms/src/main/java/org/springframework/jms/listener/adapter/MessageListenerAdapter.java +++ b/spring-jms/src/main/java/org/springframework/jms/listener/adapter/MessageListenerAdapter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2022 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. @@ -208,8 +208,8 @@ public class MessageListenerAdapter extends AbstractAdaptableMessageListener imp ((SessionAwareMessageListener) delegate).onMessage(message, session); return; } - if (delegate instanceof MessageListener) { - ((MessageListener) delegate).onMessage(message); + if (delegate instanceof MessageListener listener) { + listener.onMessage(message); return; } } @@ -232,8 +232,8 @@ public class MessageListenerAdapter extends AbstractAdaptableMessageListener imp @Override public String getSubscriptionName() { Object delegate = getDelegate(); - if (delegate != this && delegate instanceof SubscriptionNameProvider) { - return ((SubscriptionNameProvider) delegate).getSubscriptionName(); + if (delegate != this && delegate instanceof SubscriptionNameProvider provider) { + return provider.getSubscriptionName(); } else { return delegate.getClass().getName(); @@ -296,8 +296,8 @@ public class MessageListenerAdapter extends AbstractAdaptableMessageListener imp } catch (InvocationTargetException ex) { Throwable targetEx = ex.getTargetException(); - if (targetEx instanceof JMSException) { - throw (JMSException) targetEx; + if (targetEx instanceof JMSException jmsException) { + throw jmsException; } else { throw new ListenerExecutionFailedException( diff --git a/spring-jms/src/main/java/org/springframework/jms/listener/adapter/MessagingMessageListenerAdapter.java b/spring-jms/src/main/java/org/springframework/jms/listener/adapter/MessagingMessageListenerAdapter.java index c858d2b151..f82a274fdd 100644 --- a/spring-jms/src/main/java/org/springframework/jms/listener/adapter/MessagingMessageListenerAdapter.java +++ b/spring-jms/src/main/java/org/springframework/jms/listener/adapter/MessagingMessageListenerAdapter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2002-2022 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. @@ -86,9 +86,10 @@ public class MessagingMessageListenerAdapter extends AbstractAdaptableMessageLis @Override protected Object preProcessResponse(Object result) { MethodParameter returnType = getHandlerMethod().getReturnType(); - if (result instanceof Message) { - return MessageBuilder.fromMessage((Message) result) - .setHeader(AbstractMessageSendingTemplate.CONVERSION_HINT_HEADER, returnType).build(); + if (result instanceof Message message) { + return MessageBuilder.fromMessage(message) + .setHeader(AbstractMessageSendingTemplate.CONVERSION_HINT_HEADER, returnType) + .build(); } return MessageBuilder.withPayload(result).setHeader( AbstractMessageSendingTemplate.CONVERSION_HINT_HEADER, returnType).build(); diff --git a/spring-jms/src/main/java/org/springframework/jms/support/converter/MessagingMessageConverter.java b/spring-jms/src/main/java/org/springframework/jms/support/converter/MessagingMessageConverter.java index 35fb73b198..fdf54e4f79 100644 --- a/spring-jms/src/main/java/org/springframework/jms/support/converter/MessagingMessageConverter.java +++ b/spring-jms/src/main/java/org/springframework/jms/support/converter/MessagingMessageConverter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2021 the original author or authors. + * Copyright 2002-2022 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. @@ -101,11 +101,10 @@ public class MessagingMessageConverter implements MessageConverter, Initializing @Override public jakarta.jms.Message toMessage(Object object, Session session) throws JMSException, MessageConversionException { - if (!(object instanceof Message)) { + if (!(object instanceof Message input)) { throw new IllegalArgumentException("Could not convert [" + object + "] - only [" + Message.class.getName() + "] is handled by this converter"); } - Message input = (Message) object; MessageHeaders headers = input.getHeaders(); Object conversionHint = headers.get(AbstractMessageSendingTemplate.CONVERSION_HINT_HEADER); jakarta.jms.Message reply = createMessageForPayload(input.getPayload(), session, conversionHint);