From 1cd70bbe6b25b01d185a5283a223795395f33e27 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Thu, 14 Oct 2021 20:15:10 +0200 Subject: [PATCH] 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`. --- .../converter/AbstractMessageConverter.java | 5 ++--- .../converter/MappingJackson2MessageConverter.java | 3 +-- .../handler/CompositeMessageCondition.java | 5 ++--- .../messaging/handler/HandlerMethod.java | 3 +-- .../support/MessageMethodArgumentResolver.java | 5 ++--- .../support/PayloadMethodArgumentResolver.java | 5 ++--- .../invocation/AbstractMethodMessageHandler.java | 3 +-- .../HandlerMethodReturnValueHandlerComposite.java | 5 ++--- .../reactive/AbstractMethodMessageHandler.java | 2 +- .../HandlerMethodReturnValueHandlerComposite.java | 5 ++--- .../reactive/InvocableHandlerMethod.java | 5 ++--- .../rsocket/DefaultRSocketRequesterBuilder.java | 6 +++--- .../support/SendToMethodReturnValueHandler.java | 8 ++++---- .../SubscriptionMethodReturnValueHandler.java | 6 +++--- .../simp/broker/AbstractBrokerMessageHandler.java | 4 ++-- .../simp/broker/DefaultSubscriptionRegistry.java | 8 ++++---- .../broker/OrderedMessageChannelDecorator.java | 5 ++--- .../simp/stomp/StompBrokerRelayMessageHandler.java | 2 ++ .../messaging/simp/stomp/StompEncoder.java | 10 ++++++---- .../simp/user/MultiServerUserRegistry.java | 7 +++---- .../simp/user/UserDestinationMessageHandler.java | 8 +++++--- .../messaging/support/MessageHeaderAccessor.java | 14 +++++--------- 22 files changed, 57 insertions(+), 67 deletions(-) diff --git a/spring-messaging/src/main/java/org/springframework/messaging/converter/AbstractMessageConverter.java b/spring-messaging/src/main/java/org/springframework/messaging/converter/AbstractMessageConverter.java index b8400e046c..6a8386fbb4 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/converter/AbstractMessageConverter.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/converter/AbstractMessageConverter.java @@ -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(); diff --git a/spring-messaging/src/main/java/org/springframework/messaging/converter/MappingJackson2MessageConverter.java b/spring-messaging/src/main/java/org/springframework/messaging/converter/MappingJackson2MessageConverter.java index 9bb32eebd9..008d0b5ba7 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/converter/MappingJackson2MessageConverter.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/converter/MappingJackson2MessageConverter.java @@ -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) { diff --git a/spring-messaging/src/main/java/org/springframework/messaging/handler/CompositeMessageCondition.java b/spring-messaging/src/main/java/org/springframework/messaging/handler/CompositeMessageCondition.java index 4a70d4f6a9..4d4d21614b 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/handler/CompositeMessageCondition.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/handler/CompositeMessageCondition.java @@ -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> otherConditions = otherComposite.getMessageConditions(); for (int i = 0; i < this.messageConditions.size(); i++) { diff --git a/spring-messaging/src/main/java/org/springframework/messaging/handler/HandlerMethod.java b/spring-messaging/src/main/java/org/springframework/messaging/handler/HandlerMethod.java index 4ace844929..d254f5d5c0 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/handler/HandlerMethod.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/handler/HandlerMethod.java @@ -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)); } diff --git a/spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/support/MessageMethodArgumentResolver.java b/spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/support/MessageMethodArgumentResolver.java index fcbe593132..8b0e23fb7e 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/support/MessageMethodArgumentResolver.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/support/MessageMethodArgumentResolver.java @@ -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) { diff --git a/spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/support/PayloadMethodArgumentResolver.java b/spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/support/PayloadMethodArgumentResolver.java index 92e5d5ecc9..9e14cb324e 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/support/PayloadMethodArgumentResolver.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/support/PayloadMethodArgumentResolver.java @@ -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 { diff --git a/spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/AbstractMethodMessageHandler.java b/spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/AbstractMethodMessageHandler.java index f160d506ee..b1b0da1f7f 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/AbstractMethodMessageHandler.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/AbstractMethodMessageHandler.java @@ -478,8 +478,7 @@ public abstract class AbstractMethodMessageHandler 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()); } diff --git a/spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/HandlerMethodReturnValueHandlerComposite.java b/spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/HandlerMethodReturnValueHandlerComposite.java index 645538e827..af3efda05c 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/HandlerMethodReturnValueHandlerComposite.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/HandlerMethodReturnValueHandlerComposite.java @@ -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; } diff --git a/spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/reactive/AbstractMethodMessageHandler.java b/spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/reactive/AbstractMethodMessageHandler.java index a240f4d8ef..f59fde59ec 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/reactive/AbstractMethodMessageHandler.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/reactive/AbstractMethodMessageHandler.java @@ -512,7 +512,7 @@ public abstract class AbstractMethodMessageHandler for (T mapping : mappingsToCheck) { T match = getMatchingMapping(mapping, message); if (match != null) { - matches.add(new Match(match, this.handlerMethods.get(mapping))); + matches.add(new Match<>(match, this.handlerMethods.get(mapping))); } } } diff --git a/spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/reactive/HandlerMethodReturnValueHandlerComposite.java b/spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/reactive/HandlerMethodReturnValueHandlerComposite.java index c9347256b0..724fc8b650 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/reactive/HandlerMethodReturnValueHandlerComposite.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/reactive/HandlerMethodReturnValueHandlerComposite.java @@ -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; } diff --git a/spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/reactive/InvocableHandlerMethod.java b/spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/reactive/InvocableHandlerMethod.java index aad9384af2..3156c66a82 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/reactive/InvocableHandlerMethod.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/reactive/InvocableHandlerMethod.java @@ -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]); } diff --git a/spring-messaging/src/main/java/org/springframework/messaging/rsocket/DefaultRSocketRequesterBuilder.java b/spring-messaging/src/main/java/org/springframework/messaging/rsocket/DefaultRSocketRequesterBuilder.java index 3c3ff332a9..c251b7dbcd 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/rsocket/DefaultRSocketRequesterBuilder.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/rsocket/DefaultRSocketRequesterBuilder.java @@ -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> strategiesConfigurers = new ArrayList<>(); + private final List> strategiesConfigurers = new ArrayList<>(); - private List rsocketConnectorConfigurers = new ArrayList<>(); + private final List rsocketConnectorConfigurers = new ArrayList<>(); @Override diff --git a/spring-messaging/src/main/java/org/springframework/messaging/simp/annotation/support/SendToMethodReturnValueHandler.java b/spring-messaging/src/main/java/org/springframework/messaging/simp/annotation/support/SendToMethodReturnValueHandler.java index 8a150a572f..f91f47bc5e 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/simp/annotation/support/SendToMethodReturnValueHandler.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/simp/annotation/support/SendToMethodReturnValueHandler.java @@ -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(); } diff --git a/spring-messaging/src/main/java/org/springframework/messaging/simp/annotation/support/SubscriptionMethodReturnValueHandler.java b/spring-messaging/src/main/java/org/springframework/messaging/simp/annotation/support/SubscriptionMethodReturnValueHandler.java index 5e2be5c3e9..8ff4d521a2 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/simp/annotation/support/SubscriptionMethodReturnValueHandler.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/simp/annotation/support/SubscriptionMethodReturnValueHandler.java @@ -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(); } diff --git a/spring-messaging/src/main/java/org/springframework/messaging/simp/broker/AbstractBrokerMessageHandler.java b/spring-messaging/src/main/java/org/springframework/messaging/simp/broker/AbstractBrokerMessageHandler.java index 1babc663eb..19896834a9 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/simp/broker/AbstractBrokerMessageHandler.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/simp/broker/AbstractBrokerMessageHandler.java @@ -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); diff --git a/spring-messaging/src/main/java/org/springframework/messaging/simp/broker/DefaultSubscriptionRegistry.java b/spring-messaging/src/main/java/org/springframework/messaging/simp/broker/DefaultSubscriptionRegistry.java index 709f88e4db..e46e34de67 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/simp/broker/DefaultSubscriptionRegistry.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/simp/broker/DefaultSubscriptionRegistry.java @@ -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"); diff --git a/spring-messaging/src/main/java/org/springframework/messaging/simp/broker/OrderedMessageChannelDecorator.java b/spring-messaging/src/main/java/org/springframework/messaging/simp/broker/OrderedMessageChannelDecorator.java index 7a55adde7c..15fd1063eb 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/simp/broker/OrderedMessageChannelDecorator.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/simp/broker/OrderedMessageChannelDecorator.java @@ -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); diff --git a/spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompBrokerRelayMessageHandler.java b/spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompBrokerRelayMessageHandler.java index f756b0b1f9..343ef8696e 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompBrokerRelayMessageHandler.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompBrokerRelayMessageHandler.java @@ -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 con = getTcpConnection(); Assert.state(con != null, "No TcpConnection available"); diff --git a/spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompEncoder.java b/spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompEncoder.java index 175043f586..c4480b6fc2 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompEncoder.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompEncoder.java @@ -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 headerKeyUpdateCache = - new LinkedHashMap(HEADER_KEY_CACHE_LIMIT, 0.75f, true) { + new LinkedHashMap<>(HEADER_KEY_CACHE_LIMIT, 0.75f, true) { @Override protected boolean removeEldestEntry(Map.Entry 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; } diff --git a/spring-messaging/src/main/java/org/springframework/messaging/simp/user/MultiServerUserRegistry.java b/spring-messaging/src/main/java/org/springframework/messaging/simp/user/MultiServerUserRegistry.java index 5bc331d666..c6f32b9f3c 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/simp/user/MultiServerUserRegistry.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/simp/user/MultiServerUserRegistry.java @@ -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 sessions; + private final Set 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())); } diff --git a/spring-messaging/src/main/java/org/springframework/messaging/simp/user/UserDestinationMessageHandler.java b/spring-messaging/src/main/java/org/springframework/messaging/simp/user/UserDestinationMessageHandler.java index 4b80d09cde..f6d0db3dd3 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/simp/user/UserDestinationMessageHandler.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/simp/user/UserDestinationMessageHandler.java @@ -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; diff --git a/spring-messaging/src/main/java/org/springframework/messaging/support/MessageHeaderAccessor.java b/spring-messaging/src/main/java/org/springframework/messaging/support/MessageHeaderAccessor.java index 95e04b63f4..a1f93eb7a4 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/support/MessageHeaderAccessor.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/support/MessageHeaderAccessor.java @@ -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 getAccessor( MessageHeaders messageHeaders, @Nullable Class 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;