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 // We've already added a version, we need to extend it
org.springframework.core.env.PropertySource<?> existing = propertySources.get(name); org.springframework.core.env.PropertySource<?> existing = propertySources.get(name);
if (existing != null) { if (existing != null) {
PropertySource<?> newSource = (propertySource instanceof ResourcePropertySource ? PropertySource<?> newSource = (propertySource instanceof ResourcePropertySource rps ?
((ResourcePropertySource) propertySource).withResourceName() : propertySource); rps.withResourceName() : propertySource);
if (existing instanceof CompositePropertySource) { if (existing instanceof CompositePropertySource cps) {
((CompositePropertySource) existing).addFirstPropertySource(newSource); cps.addFirstPropertySource(newSource);
} }
else { else {
if (existing instanceof ResourcePropertySource) { if (existing instanceof ResourcePropertySource rps) {
existing = ((ResourcePropertySource) existing).withResourceName(); existing = rps.withResourceName();
} }
CompositePropertySource composite = new CompositePropertySource(name); CompositePropertySource composite = new CompositePropertySource(name);
composite.addPropertySource(newSource); 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( throw new MessageConversionException(
"No MessageConverter specified - cannot handle message [" + content + "]"); "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) private Destination getResponseDestination(Message request, Message response, Session session, Object result)
throws JMSException { throws JMSException {
if (result instanceof JmsResponse) { if (result instanceof JmsResponse<?> jmsResponse) {
JmsResponse<?> jmsResponse = (JmsResponse<?>) result;
Destination destination = jmsResponse.resolveDestination(getDestinationResolver(), session); Destination destination = jmsResponse.resolveDestination(getDestinationResolver(), session);
if (destination != null) { if (destination != null) {
return destination; return destination;
@ -408,8 +407,8 @@ public abstract class AbstractAdaptableMessageListener
*/ */
@Nullable @Nullable
protected Destination resolveDefaultResponseDestination(Session session) throws JMSException { protected Destination resolveDefaultResponseDestination(Session session) throws JMSException {
if (this.defaultResponseDestination instanceof Destination) { if (this.defaultResponseDestination instanceof Destination destination) {
return (Destination) this.defaultResponseDestination; return destination;
} }
if (this.defaultResponseDestination instanceof DestinationNameHolder nameHolder) { if (this.defaultResponseDestination instanceof DestinationNameHolder nameHolder) {
return getDestinationResolver().resolveDestinationName(session, nameHolder.name, nameHolder.isTopic); return getDestinationResolver().resolveDestinationName(session, nameHolder.name, nameHolder.isTopic);
@ -471,11 +470,11 @@ public abstract class AbstractAdaptableMessageListener
@Override @Override
protected Object extractPayload(Message message) throws JMSException { protected Object extractPayload(Message message) throws JMSException {
Object payload = extractMessage(message); Object payload = extractMessage(message);
if (message instanceof BytesMessage) { if (message instanceof BytesMessage bytesMessage) {
try { try {
// In case the BytesMessage is going to be received as a user argument: // In case the BytesMessage is going to be received as a user argument:
// reset it, otherwise it would appear empty to such processing code... // reset it, otherwise it would appear empty to such processing code...
((BytesMessage) message).reset(); bytesMessage.reset();
} }
catch (JMSException ex) { catch (JMSException ex) {
// Continue since the BytesMessage typically won't be used any further. // Continue since the BytesMessage typically won't be used any further.
@ -493,8 +492,8 @@ public abstract class AbstractAdaptableMessageListener
if (converter == null) { if (converter == null) {
throw new IllegalStateException("No message converter, cannot handle '" + payload + "'"); throw new IllegalStateException("No message converter, cannot handle '" + payload + "'");
} }
if (converter instanceof SmartMessageConverter) { if (converter instanceof SmartMessageConverter smartMessageConverter) {
return ((SmartMessageConverter) converter).toMessage(payload, session, conversionHint); return smartMessageConverter.toMessage(payload, session, conversionHint);
} }
return converter.toMessage(payload, session); return converter.toMessage(payload, session);
@ -538,8 +537,8 @@ public abstract class AbstractAdaptableMessageListener
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
private Object unwrapPayload() throws JMSException { private Object unwrapPayload() throws JMSException {
Object payload = extractPayload(this.message); Object payload = extractPayload(this.message);
if (payload instanceof org.springframework.messaging.Message) { if (payload instanceof org.springframework.messaging.Message springMessage) {
return ((org.springframework.messaging.Message) payload).getPayload(); return springMessage.getPayload();
} }
return payload; return payload;
} }

View File

@ -91,8 +91,8 @@ public class JmsResponse<T> {
public Destination resolveDestination(DestinationResolver destinationResolver, Session session) public Destination resolveDestination(DestinationResolver destinationResolver, Session session)
throws JMSException { throws JMSException {
if (this.destination instanceof Destination) { if (this.destination instanceof Destination dest) {
return (Destination) this.destination; return dest;
} }
if (this.destination instanceof DestinationNameHolder nameHolder) { if (this.destination instanceof DestinationNameHolder nameHolder) {
return destinationResolver.resolveDestinationName(session, 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"); * 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.
@ -208,8 +208,8 @@ public class MessageListenerAdapter extends AbstractAdaptableMessageListener imp
((SessionAwareMessageListener<Message>) delegate).onMessage(message, session); ((SessionAwareMessageListener<Message>) delegate).onMessage(message, session);
return; return;
} }
if (delegate instanceof MessageListener) { if (delegate instanceof MessageListener listener) {
((MessageListener) delegate).onMessage(message); listener.onMessage(message);
return; return;
} }
} }
@ -232,8 +232,8 @@ public class MessageListenerAdapter extends AbstractAdaptableMessageListener imp
@Override @Override
public String getSubscriptionName() { public String getSubscriptionName() {
Object delegate = getDelegate(); Object delegate = getDelegate();
if (delegate != this && delegate instanceof SubscriptionNameProvider) { if (delegate != this && delegate instanceof SubscriptionNameProvider provider) {
return ((SubscriptionNameProvider) delegate).getSubscriptionName(); return provider.getSubscriptionName();
} }
else { else {
return delegate.getClass().getName(); return delegate.getClass().getName();
@ -296,8 +296,8 @@ public class MessageListenerAdapter extends AbstractAdaptableMessageListener imp
} }
catch (InvocationTargetException ex) { catch (InvocationTargetException ex) {
Throwable targetEx = ex.getTargetException(); Throwable targetEx = ex.getTargetException();
if (targetEx instanceof JMSException) { if (targetEx instanceof JMSException jmsException) {
throw (JMSException) targetEx; throw jmsException;
} }
else { else {
throw new ListenerExecutionFailedException( 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"); * 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.
@ -86,9 +86,10 @@ public class MessagingMessageListenerAdapter extends AbstractAdaptableMessageLis
@Override @Override
protected Object preProcessResponse(Object result) { protected Object preProcessResponse(Object result) {
MethodParameter returnType = getHandlerMethod().getReturnType(); MethodParameter returnType = getHandlerMethod().getReturnType();
if (result instanceof Message) { if (result instanceof Message<?> message) {
return MessageBuilder.fromMessage((Message<?>) result) return MessageBuilder.fromMessage(message)
.setHeader(AbstractMessageSendingTemplate.CONVERSION_HINT_HEADER, returnType).build(); .setHeader(AbstractMessageSendingTemplate.CONVERSION_HINT_HEADER, returnType)
.build();
} }
return MessageBuilder.withPayload(result).setHeader( return MessageBuilder.withPayload(result).setHeader(
AbstractMessageSendingTemplate.CONVERSION_HINT_HEADER, returnType).build(); 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"); * 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.
@ -101,11 +101,10 @@ public class MessagingMessageConverter implements MessageConverter, Initializing
@Override @Override
public jakarta.jms.Message toMessage(Object object, Session session) throws JMSException, MessageConversionException { 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 [" + throw new IllegalArgumentException("Could not convert [" + object + "] - only [" +
Message.class.getName() + "] is handled by this converter"); Message.class.getName() + "] is handled by this converter");
} }
Message<?> input = (Message<?>) object;
MessageHeaders headers = input.getHeaders(); MessageHeaders headers = input.getHeaders();
Object conversionHint = headers.get(AbstractMessageSendingTemplate.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);