Apply "instanceof pattern matching" in spring-messaging

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

- final fields
- diamond operator (<>) for anonymous inner classes
- for-each loop

This has only been applied to `src/main/java`.
This commit is contained in:
Sam Brannen 2021-10-14 20:15:10 +02:00
parent d70a610a0d
commit 1cd70bbe6b
22 changed files with 57 additions and 67 deletions

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");
* you may not use this file except in compliance with the License.
@ -314,8 +314,7 @@ public abstract class AbstractMessageConverter implements SmartMessageConverter
static Type getResolvedType(Class<?> targetClass, @Nullable Object conversionHint) {
if (conversionHint instanceof MethodParameter) {
MethodParameter param = (MethodParameter) conversionHint;
if (conversionHint instanceof MethodParameter param) {
param = param.nestedIfOptional();
if (Message.class.isAssignableFrom(param.getParameterType())) {
param = param.nested();

View File

@ -285,8 +285,7 @@ public class MappingJackson2MessageConverter extends AbstractMessageConverter {
*/
@Nullable
protected Class<?> getSerializationView(@Nullable Object conversionHint) {
if (conversionHint instanceof MethodParameter) {
MethodParameter param = (MethodParameter) conversionHint;
if (conversionHint instanceof MethodParameter param) {
JsonView annotation = (param.getParameterIndex() >= 0 ?
param.getParameterAnnotation(JsonView.class) : param.getMethodAnnotation(JsonView.class));
if (annotation != null) {

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2021 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.
@ -131,10 +131,9 @@ public class CompositeMessageCondition implements MessageCondition<CompositeMess
if (this == other) {
return true;
}
if (!(other instanceof CompositeMessageCondition)) {
if (!(other instanceof CompositeMessageCondition otherComposite)) {
return false;
}
CompositeMessageCondition otherComposite = (CompositeMessageCondition) other;
checkCompatible(otherComposite);
List<MessageCondition<?>> otherConditions = otherComposite.getMessageConditions();
for (int i = 0; i < this.messageConditions.size(); i++) {

View File

@ -308,10 +308,9 @@ public class HandlerMethod {
if (this == other) {
return true;
}
if (!(other instanceof HandlerMethod)) {
if (!(other instanceof HandlerMethod otherMethod)) {
return false;
}
HandlerMethod otherMethod = (HandlerMethod) other;
return (this.bean.equals(otherMethod.bean) && this.method.equals(otherMethod.method));
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2021 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.
@ -134,8 +134,7 @@ public class MessageMethodArgumentResolver implements HandlerMethodArgumentResol
private Object convertPayload(Message<?> message, MethodParameter parameter, Class<?> targetPayloadType) {
Object result = null;
if (this.converter instanceof SmartMessageConverter) {
SmartMessageConverter smartConverter = (SmartMessageConverter) this.converter;
if (this.converter instanceof SmartMessageConverter smartConverter) {
result = smartConverter.fromMessage(message, targetPayloadType, parameter);
}
else if (this.converter != null) {

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2021 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.
@ -133,8 +133,7 @@ public class PayloadMethodArgumentResolver implements HandlerMethodArgumentResol
return payload;
}
else {
if (this.converter instanceof SmartMessageConverter) {
SmartMessageConverter smartConverter = (SmartMessageConverter) this.converter;
if (this.converter instanceof SmartMessageConverter smartConverter) {
payload = smartConverter.fromMessage(message, targetClass, parameter);
}
else {

View File

@ -478,8 +478,7 @@ public abstract class AbstractMethodMessageHandler<T>
if (CollectionUtils.isEmpty(this.destinationPrefixes)) {
return destination;
}
for (int i = 0; i < this.destinationPrefixes.size(); i++) {
String prefix = this.destinationPrefixes.get(i);
for (String prefix : this.destinationPrefixes) {
if (destination.startsWith(prefix)) {
return destination.substring(prefix.length());
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2021 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.
@ -104,8 +104,7 @@ public class HandlerMethodReturnValueHandlerComposite implements AsyncHandlerMet
@SuppressWarnings("ForLoopReplaceableByForEach")
@Nullable
private HandlerMethodReturnValueHandler getReturnValueHandler(MethodParameter returnType) {
for (int i = 0; i < this.returnValueHandlers.size(); i++) {
HandlerMethodReturnValueHandler handler = this.returnValueHandlers.get(i);
for (HandlerMethodReturnValueHandler handler : this.returnValueHandlers) {
if (handler.supportsReturnType(returnType)) {
return handler;
}

View File

@ -512,7 +512,7 @@ public abstract class AbstractMethodMessageHandler<T>
for (T mapping : mappingsToCheck) {
T match = getMatchingMapping(mapping, message);
if (match != null) {
matches.add(new Match<T>(match, this.handlerMethods.get(mapping)));
matches.add(new Match<>(match, this.handlerMethods.get(mapping)));
}
}
}

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");
* you may not use this file except in compliance with the License.
@ -95,8 +95,7 @@ public class HandlerMethodReturnValueHandlerComposite implements HandlerMethodRe
@SuppressWarnings("ForLoopReplaceableByForEach")
@Nullable
private HandlerMethodReturnValueHandler getReturnValueHandler(MethodParameter returnType) {
for (int i = 0; i < this.returnValueHandlers.size(); i++) {
HandlerMethodReturnValueHandler handler = this.returnValueHandlers.get(i);
for (HandlerMethodReturnValueHandler handler : this.returnValueHandlers) {
if (handler.supportsReturnType(returnType)) {
return handler;
}

View File

@ -54,7 +54,7 @@ public class InvocableHandlerMethod extends HandlerMethod {
private static final Object NO_ARG_VALUE = new Object();
private HandlerMethodArgumentResolverComposite resolvers = new HandlerMethodArgumentResolverComposite();
private final HandlerMethodArgumentResolverComposite resolvers = new HandlerMethodArgumentResolverComposite();
private ParameterNameDiscoverer parameterNameDiscoverer = new DefaultParameterNameDiscoverer();
@ -206,8 +206,7 @@ public class InvocableHandlerMethod extends HandlerMethod {
return true;
}
Type parameterType = returnType.getGenericParameterType();
if (parameterType instanceof ParameterizedType) {
ParameterizedType type = (ParameterizedType) parameterType;
if (parameterType instanceof ParameterizedType type) {
if (type.getActualTypeArguments().length == 1) {
return Void.class.equals(type.getActualTypeArguments()[0]);
}

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");
* you may not use this file except in compliance with the License.
@ -90,9 +90,9 @@ final class DefaultRSocketRequesterBuilder implements RSocketRequester.Builder {
@Nullable
private RSocketStrategies strategies;
private List<Consumer<RSocketStrategies.Builder>> strategiesConfigurers = new ArrayList<>();
private final List<Consumer<RSocketStrategies.Builder>> strategiesConfigurers = new ArrayList<>();
private List<RSocketConnectorConfigurer> rsocketConnectorConfigurers = new ArrayList<>();
private final List<RSocketConnectorConfigurer> rsocketConnectorConfigurers = new ArrayList<>();
@Override

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");
* you may not use this file except in compliance with the License.
@ -28,6 +28,7 @@ import org.springframework.lang.Nullable;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.core.AbstractMessageSendingTemplate;
import org.springframework.messaging.handler.DestinationPatternsMessageCondition;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.handler.annotation.support.DestinationVariableMethodArgumentResolver;
@ -35,7 +36,6 @@ import org.springframework.messaging.handler.invocation.HandlerMethodReturnValue
import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
import org.springframework.messaging.simp.SimpMessageSendingOperations;
import org.springframework.messaging.simp.SimpMessageType;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.messaging.simp.annotation.SendToUser;
import org.springframework.messaging.simp.user.DestinationUserNameProvider;
import org.springframework.messaging.support.MessageHeaderInitializer;
@ -68,7 +68,7 @@ public class SendToMethodReturnValueHandler implements HandlerMethodReturnValueH
private String defaultUserDestinationPrefix = "/queue";
private PropertyPlaceholderHelper placeholderHelper = new PropertyPlaceholderHelper("{", "}", null, false);
private final PropertyPlaceholderHelper placeholderHelper = new PropertyPlaceholderHelper("{", "}", null, false);
@Nullable
private MessageHeaderInitializer headerInitializer;
@ -244,7 +244,7 @@ public class SendToMethodReturnValueHandler implements HandlerMethodReturnValueH
if (sessionId != null) {
headerAccessor.setSessionId(sessionId);
}
headerAccessor.setHeader(SimpMessagingTemplate.CONVERSION_HINT_HEADER, returnType);
headerAccessor.setHeader(AbstractMessageSendingTemplate.CONVERSION_HINT_HEADER, returnType);
headerAccessor.setLeaveMutable(true);
return headerAccessor.getMessageHeaders();
}

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");
* you may not use this file except in compliance with the License.
@ -22,13 +22,13 @@ import org.springframework.core.MethodParameter;
import org.springframework.lang.Nullable;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.core.AbstractMessageSendingTemplate;
import org.springframework.messaging.core.MessageSendingOperations;
import org.springframework.messaging.handler.annotation.SendTo;
import org.springframework.messaging.handler.invocation.HandlerMethodReturnValueHandler;
import org.springframework.messaging.simp.SimpLogging;
import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
import org.springframework.messaging.simp.SimpMessageType;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.messaging.simp.annotation.SendToUser;
import org.springframework.messaging.simp.annotation.SubscribeMapping;
import org.springframework.messaging.support.MessageHeaderInitializer;
@ -141,7 +141,7 @@ public class SubscriptionMethodReturnValueHandler implements HandlerMethodReturn
accessor.setSessionId(sessionId);
}
accessor.setSubscriptionId(subscriptionId);
accessor.setHeader(SimpMessagingTemplate.CONVERSION_HINT_HEADER, returnType);
accessor.setHeader(AbstractMessageSendingTemplate.CONVERSION_HINT_HEADER, returnType);
accessor.setLeaveMutable(true);
return accessor.getMessageHeaders();
}

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");
* you may not use this file except in compliance with the License.
@ -67,7 +67,7 @@ public abstract class AbstractBrokerMessageHandler
@Nullable
private ApplicationEventPublisher eventPublisher;
private AtomicBoolean brokerAvailable = new AtomicBoolean();
private final AtomicBoolean brokerAvailable = new AtomicBoolean();
private final BrokerAvailabilityEvent availableEvent = new BrokerAvailabilityEvent(true, this);

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");
* you may not use this file except in compliance with the License.
@ -41,6 +41,7 @@ import org.springframework.messaging.Message;
import org.springframework.messaging.MessageHeaders;
import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
import org.springframework.messaging.support.MessageHeaderAccessor;
import org.springframework.messaging.support.NativeMessageHeaderAccessor;
import org.springframework.util.AntPathMatcher;
import org.springframework.util.Assert;
import org.springframework.util.LinkedMultiValueMap;
@ -162,7 +163,7 @@ public class DefaultSubscriptionRegistry extends AbstractSubscriptionRegistry {
if (getSelectorHeaderName() == null) {
return null;
}
String selector = SimpMessageHeaderAccessor.getFirstNativeHeader(getSelectorHeaderName(), headers);
String selector = NativeMessageHeaderAccessor.getFirstNativeHeader(getSelectorHeaderName(), headers);
if (selector == null) {
return null;
}
@ -509,8 +510,7 @@ public class DefaultSubscriptionRegistry extends AbstractSubscriptionRegistry {
if (target instanceof Message) {
value = name.equals("headers") ? ((Message) target).getHeaders() : null;
}
else if (target instanceof MessageHeaders) {
MessageHeaders headers = (MessageHeaders) target;
else if (target instanceof MessageHeaders headers) {
SimpMessageHeaderAccessor accessor =
MessageHeaderAccessor.getAccessor(headers, SimpMessageHeaderAccessor.class);
Assert.state(accessor != null, "No SimpMessageHeaderAccessor");

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");
* you may not use this file except in compliance with the License.
@ -154,8 +154,7 @@ public class OrderedMessageChannelDecorator implements MessageChannel {
execChannel.addInterceptor(0, new CallbackInterceptor());
}
}
else if (channel instanceof ExecutorSubscribableChannel) {
ExecutorSubscribableChannel execChannel = (ExecutorSubscribableChannel) channel;
else if (channel instanceof ExecutorSubscribableChannel execChannel) {
execChannel.getInterceptors().stream().filter(i -> i instanceof CallbackInterceptor)
.findFirst().map(execChannel::removeInterceptor);

View File

@ -657,6 +657,7 @@ public class StompBrokerRelayMessageHandler extends AbstractBrokerMessageHandler
}
@Override
public String getSessionId() {
return this.sessionId;
}
@ -984,6 +985,7 @@ public class StompBrokerRelayMessageHandler extends AbstractBrokerMessageHandler
sendSystemSubscriptions();
}
@Override
protected void initHeartbeats(StompHeaderAccessor connectedHeaders) {
TcpConnection<byte[]> con = getTcpConnection();
Assert.state(con != null, "No TcpConnection available");

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");
* you may not use this file except in compliance with the License.
@ -58,7 +58,7 @@ public class StompEncoder {
@SuppressWarnings("serial")
private final Map<String, byte[]> headerKeyUpdateCache =
new LinkedHashMap<String, byte[]>(HEADER_KEY_CACHE_LIMIT, 0.75f, true) {
new LinkedHashMap<>(HEADER_KEY_CACHE_LIMIT, 0.75f, true) {
@Override
protected boolean removeEldestEntry(Map.Entry<String, byte[]> eldest) {
if (size() > HEADER_KEY_CACHE_LIMIT) {
@ -236,22 +236,24 @@ public class StompEncoder {
private int size;
@Override
public void add(byte[] bytes) {
this.size += bytes.length;
super.add(bytes);
}
@Override
public void add(byte b) {
this.size++;
super.add(b);
}
@Override
public byte[] toByteArray() {
byte[] result = new byte[this.size];
int position = 0;
for (Object o : this) {
if (o instanceof byte[]) {
byte[] src = (byte[]) o;
if (o instanceof byte[] src) {
System.arraycopy(src, 0, result, position, src.length);
position += src.length;
}

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");
* you may not use this file except in compliance with the License.
@ -275,7 +275,7 @@ public class MultiServerUserRegistry implements SimpUserRegistry, SmartApplicati
private String name = "";
// User sessions from "this" registry only (i.e. one server)
private Set<TransferSimpSession> sessions;
private final Set<TransferSimpSession> sessions;
// Cross-server session lookup (e.g. user connected to multiple servers)
@Nullable
@ -535,10 +535,9 @@ public class MultiServerUserRegistry implements SimpUserRegistry, SmartApplicati
if (this == other) {
return true;
}
if (!(other instanceof SimpSubscription)) {
if (!(other instanceof SimpSubscription otherSubscription)) {
return false;
}
SimpSubscription otherSubscription = (SimpSubscription) other;
return (getId().equals(otherSubscription.getId()) &&
ObjectUtils.nullSafeEquals(getSession(), otherSubscription.getSession()));
}

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");
* you may not use this file except in compliance with the License.
@ -34,7 +34,9 @@ import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
import org.springframework.messaging.simp.SimpMessageType;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.messaging.support.MessageHeaderAccessor;
import org.springframework.messaging.support.MessageHeaderInitializer;
import org.springframework.messaging.support.NativeMessageHeaderAccessor;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
@ -258,7 +260,7 @@ public class UserDestinationMessageHandler implements MessageHandler, SmartLifec
return message;
}
SimpMessageHeaderAccessor accessor =
SimpMessageHeaderAccessor.getAccessor(message, SimpMessageHeaderAccessor.class);
MessageHeaderAccessor.getAccessor(message, SimpMessageHeaderAccessor.class);
Assert.state(accessor != null, "No SimpMessageHeaderAccessor");
if (accessor.getSessionId() == null) {
// Our own broadcast
@ -284,7 +286,7 @@ public class UserDestinationMessageHandler implements MessageHandler, SmartLifec
public void handleUnresolved(Message<?> message) {
MessageHeaders headers = message.getHeaders();
if (SimpMessageHeaderAccessor.getFirstNativeHeader(
if (NativeMessageHeaderAccessor.getFirstNativeHeader(
SimpMessageHeaderAccessor.ORIGINAL_DESTINATION, headers) != null) {
// Re-broadcast
return;

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");
* you may not use this file except in compliance with the License.
@ -495,14 +495,12 @@ public class MessageHeaderAccessor {
}
protected String getShortPayloadLogMessage(Object payload) {
if (payload instanceof String) {
String payloadText = (String) payload;
if (payload instanceof String payloadText) {
return (payloadText.length() < 80) ?
" payload=" + payloadText :
" payload=" + payloadText.substring(0, 80) + "...(truncated)";
}
else if (payload instanceof byte[]) {
byte[] bytes = (byte[]) payload;
else if (payload instanceof byte[] bytes) {
if (isReadableContentType()) {
return (bytes.length < 80) ?
" payload=" + new String(bytes, getCharset()) :
@ -524,8 +522,7 @@ public class MessageHeaderAccessor {
if (payload instanceof String) {
return " payload=" + payload;
}
else if (payload instanceof byte[]) {
byte[] bytes = (byte[]) payload;
else if (payload instanceof byte[] bytes) {
if (isReadableContentType()) {
return " payload=" + new String(bytes, getCharset());
}
@ -601,8 +598,7 @@ public class MessageHeaderAccessor {
public static <T extends MessageHeaderAccessor> T getAccessor(
MessageHeaders messageHeaders, @Nullable Class<T> requiredType) {
if (messageHeaders instanceof MutableMessageHeaders) {
MutableMessageHeaders mutableHeaders = (MutableMessageHeaders) messageHeaders;
if (messageHeaders instanceof MutableMessageHeaders mutableHeaders) {
MessageHeaderAccessor headerAccessor = mutableHeaders.getAccessor();
if (requiredType == null || requiredType.isInstance(headerAccessor)) {
return (T) headerAccessor;