Apply "instanceof pattern matching"

This commit is contained in:
Sam Brannen 2022-11-29 17:56:54 +01:00
parent a88dbbec98
commit fdec9f0adc
6 changed files with 33 additions and 34 deletions

View File

@ -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);

View File

@ -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;
}

View File

@ -91,8 +91,8 @@ public class JmsResponse<T> {
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,

View File

@ -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<Message>) 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(

View File

@ -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();

View File

@ -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);