Apply "instanceof pattern matching" in remainder of spring-messaging module

See gh-30067
This commit is contained in:
Sam Brannen 2023-03-06 16:23:10 +01:00
parent 09b60220d8
commit 004a144bdc
36 changed files with 160 additions and 174 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
@ -333,9 +333,8 @@ public class MessageHeaders implements Map<String, Object>, Serializable {
// equals, hashCode, toString
@Override
public boolean equals(@Nullable Object other) {
return (this == other ||
(other instanceof MessageHeaders && this.headers.equals(((MessageHeaders) other).headers)));
public boolean equals(@Nullable Object obj) {
return (this == obj || (obj instanceof MessageHeaders that && this.headers.equals(that.headers)));
}
@Override

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2023 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.
@ -69,8 +69,8 @@ public abstract class AbstractJsonMessageConverter extends AbstractMessageConver
if (ClassUtils.isAssignableValue(targetClass, payload)) {
return payload;
}
else if (payload instanceof byte[]) {
return fromJson(getReader((byte[]) payload, message.getHeaders()), resolvedType);
else if (payload instanceof byte[] bytes) {
return fromJson(getReader(bytes, message.getHeaders()), resolvedType);
}
else {
// Assuming a text-based source payload

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2023 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.
@ -66,8 +66,8 @@ public class CompositeMessageConverter implements SmartMessageConverter {
@Nullable
public Object fromMessage(Message<?> message, Class<?> targetClass, @Nullable Object conversionHint) {
for (MessageConverter converter : getConverters()) {
Object result = (converter instanceof SmartMessageConverter ?
((SmartMessageConverter) converter).fromMessage(message, targetClass, conversionHint) :
Object result = (converter instanceof SmartMessageConverter smartMessageConverter ?
smartMessageConverter.fromMessage(message, targetClass, conversionHint) :
converter.fromMessage(message, targetClass));
if (result != null) {
return result;
@ -92,8 +92,8 @@ public class CompositeMessageConverter implements SmartMessageConverter {
@Nullable
public Message<?> toMessage(Object payload, @Nullable MessageHeaders headers, @Nullable Object conversionHint) {
for (MessageConverter converter : getConverters()) {
Message<?> result = (converter instanceof SmartMessageConverter ?
((SmartMessageConverter) converter).toMessage(payload, headers, conversionHint) :
Message<?> result = (converter instanceof SmartMessageConverter smartMessageConverter ?
smartMessageConverter.toMessage(payload, headers, conversionHint) :
converter.toMessage(payload, headers));
if (result != null) {
return result;

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2023 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.
@ -65,11 +65,11 @@ public class DefaultContentTypeResolver implements ContentTypeResolver {
if (value == null) {
return null;
}
else if (value instanceof MimeType) {
return (MimeType) value;
else if (value instanceof MimeType mimeType) {
return mimeType;
}
else if (value instanceof String) {
return MimeType.valueOf((String) value);
else if (value instanceof String text) {
return MimeType.valueOf(text);
}
else {
throw new IllegalArgumentException(

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
@ -213,12 +213,12 @@ public class MappingJackson2MessageConverter extends AbstractMessageConverter {
if (ClassUtils.isAssignableValue(targetClass, payload)) {
return payload;
}
else if (payload instanceof byte[]) {
else if (payload instanceof byte[] bytes) {
if (view != null) {
return this.objectMapper.readerWithView(view).forType(javaType).readValue((byte[]) payload);
return this.objectMapper.readerWithView(view).forType(javaType).readValue(bytes);
}
else {
return this.objectMapper.readValue((byte[]) payload, javaType);
return this.objectMapper.readValue(bytes, javaType);
}
}
else {
@ -290,11 +290,11 @@ public class MappingJackson2MessageConverter extends AbstractMessageConverter {
return extractViewClass(annotation, conversionHint);
}
}
else if (conversionHint instanceof JsonView) {
return extractViewClass((JsonView) conversionHint, conversionHint);
else if (conversionHint instanceof JsonView jsonView) {
return extractViewClass(jsonView, conversionHint);
}
else if (conversionHint instanceof Class) {
return (Class<?>) conversionHint;
else if (conversionHint instanceof Class<?> clazz) {
return clazz;
}
// No JSON view specified...

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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,8 +86,8 @@ public class MarshallingMessageConverter extends AbstractMessageConverter {
this();
Assert.notNull(marshaller, "Marshaller must not be null");
this.marshaller = marshaller;
if (marshaller instanceof Unmarshaller) {
this.unmarshaller = (Unmarshaller) marshaller;
if (marshaller instanceof Unmarshaller _unmarshaller) {
this.unmarshaller = _unmarshaller;
}
}
@ -159,8 +159,8 @@ public class MarshallingMessageConverter extends AbstractMessageConverter {
}
private Source getSource(Object payload) {
if (payload instanceof byte[]) {
return new StreamSource(new ByteArrayInputStream((byte[]) payload));
if (payload instanceof byte[] bytes) {
return new StreamSource(new ByteArrayInputStream(bytes));
}
else {
return new StreamSource(new StringReader(payload.toString()));

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2023 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.
@ -168,17 +168,13 @@ public abstract class AbstractMessageSendingTemplate<D> implements MessageSendin
Map<String, Object> headersToUse = processHeadersToSend(headers);
if (headersToUse != null) {
if (headersToUse instanceof MessageHeaders) {
messageHeaders = (MessageHeaders) headersToUse;
}
else {
messageHeaders = new MessageHeaders(headersToUse);
}
messageHeaders = (headersToUse instanceof MessageHeaders _messageHeaders ?
_messageHeaders : new MessageHeaders(headersToUse));
}
MessageConverter converter = getMessageConverter();
Message<?> message = (converter instanceof SmartMessageConverter ?
((SmartMessageConverter) converter).toMessage(payload, messageHeaders, conversionHint) :
Message<?> message = (converter instanceof SmartMessageConverter smartMessageConverter ?
smartMessageConverter.toMessage(payload, messageHeaders, conversionHint) :
converter.toMessage(payload, messageHeaders));
if (message == null) {
String payloadType = payload.getClass().getName();

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2023 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.
@ -201,10 +201,11 @@ public class GenericMessagingTemplate extends AbstractDestinationResolvingMessag
@Nullable
protected final Message<?> doReceive(MessageChannel channel, long timeout) {
Assert.notNull(channel, "MessageChannel is required");
Assert.state(channel instanceof PollableChannel, "A PollableChannel is required to receive messages");
if (!(channel instanceof PollableChannel pollableChannel)) {
throw new IllegalStateException("A PollableChannel is required to receive messages");
}
Message<?> message = (timeout >= 0 ?
((PollableChannel) channel).receive(timeout) : ((PollableChannel) channel).receive());
Message<?> message = (timeout >= 0 ? pollableChannel.receive(timeout) : pollableChannel.receive());
if (message == null && logger.isTraceEnabled()) {
logger.trace("Failed to receive message from channel '" + channel + "' within timeout: " + timeout);
@ -260,11 +261,11 @@ public class GenericMessagingTemplate extends AbstractDestinationResolvingMessag
@Nullable
private Long headerToLong(@Nullable Object headerValue) {
if (headerValue instanceof Number) {
return ((Number) headerValue).longValue();
if (headerValue instanceof Number number) {
return number.longValue();
}
else if (headerValue instanceof String) {
return Long.parseLong((String) headerValue);
else if (headerValue instanceof String text) {
return Long.parseLong(text);
}
else {
return null;

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2023 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.
@ -188,14 +188,12 @@ public class DestinationPatternsMessageCondition
}
private boolean matchPattern(String pattern, Object destination) {
return destination instanceof RouteMatcher.Route ?
this.routeMatcher.match(pattern, (RouteMatcher.Route) destination) :
return destination instanceof RouteMatcher.Route route ? this.routeMatcher.match(pattern, route) :
((SimpleRouteMatcher) this.routeMatcher).getPathMatcher().match(pattern, (String) destination);
}
private Comparator<String> getPatternComparator(Object destination) {
return destination instanceof RouteMatcher.Route ?
this.routeMatcher.getPatternComparator((RouteMatcher.Route) destination) :
return destination instanceof RouteMatcher.Route route ? this.routeMatcher.getPatternComparator(route) :
((SimpleRouteMatcher) this.routeMatcher).getPathMatcher().getPatternComparator((String) destination);
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2023 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.
@ -242,8 +242,8 @@ public class MessageMappingMessageHandler extends AbstractMethodMessageHandler<C
List<HandlerMethodArgumentResolver> resolvers = new ArrayList<>();
ApplicationContext context = getApplicationContext();
ConfigurableBeanFactory beanFactory = (context instanceof ConfigurableApplicationContext ?
((ConfigurableApplicationContext) context).getBeanFactory() : null);
ConfigurableBeanFactory beanFactory = (context instanceof ConfigurableApplicationContext cac ?
cac.getBeanFactory() : null);
// Annotation-based resolvers
resolvers.add(new HeaderMethodArgumentResolver(this.conversionService, beanFactory));

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2023 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.
@ -157,8 +157,8 @@ public class DefaultMessageHandlerMethodFactory
protected List<HandlerMethodArgumentResolver> initArgumentResolvers() {
List<HandlerMethodArgumentResolver> resolvers = new ArrayList<>();
ConfigurableBeanFactory beanFactory = (this.beanFactory instanceof ConfigurableBeanFactory ?
(ConfigurableBeanFactory) this.beanFactory : null);
ConfigurableBeanFactory beanFactory = (this.beanFactory instanceof ConfigurableBeanFactory cbf ?
cbf : null);
// Annotation-based argument resolution
resolvers.add(new HeaderMethodArgumentResolver(this.conversionService, beanFactory));

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2023 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.
@ -121,11 +121,11 @@ public class MessageMethodArgumentResolver implements HandlerMethodArgumentResol
if (payload == null) {
return true;
}
else if (payload instanceof byte[]) {
return ((byte[]) payload).length == 0;
else if (payload instanceof byte[] bytes) {
return bytes.length == 0;
}
else if (payload instanceof String) {
return !StringUtils.hasText((String) payload);
else if (payload instanceof String text) {
return !StringUtils.hasText(text);
}
else {
return false;

View File

@ -299,10 +299,10 @@ public abstract class AbstractMethodMessageHandler<T>
*/
protected final void detectHandlerMethods(final Object handler) {
Class<?> handlerType;
if (handler instanceof String) {
if (handler instanceof String beanName) {
ApplicationContext context = getApplicationContext();
Assert.state(context != null, "ApplicationContext is required for resolving handler bean names");
handlerType = context.getType((String) handler);
handlerType = context.getType(beanName);
}
else {
handlerType = handler.getClass();
@ -730,8 +730,9 @@ public abstract class AbstractMethodMessageHandler<T>
}
}
private void handleFailure(Throwable ex) {
Exception cause = (ex instanceof Exception ? (Exception) ex : new IllegalStateException(ex));
private void handleFailure(Throwable throwable) {
Exception cause = (throwable instanceof Exception exception ? exception :
new IllegalStateException(throwable));
processHandlerMethodException(this.handlerMethod, cause, this.message);
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
@ -177,14 +177,14 @@ public class InvocableHandlerMethod extends HandlerMethod {
catch (InvocationTargetException ex) {
// Unwrap for HandlerExceptionResolvers ...
Throwable targetException = ex.getTargetException();
if (targetException instanceof RuntimeException) {
throw (RuntimeException) targetException;
if (targetException instanceof RuntimeException runtimeException) {
throw runtimeException;
}
else if (targetException instanceof Error) {
throw (Error) targetException;
else if (targetException instanceof Error error) {
throw error;
}
else if (targetException instanceof Exception) {
throw (Exception) targetException;
else if (targetException instanceof Exception exception) {
throw exception;
}
else {
throw new IllegalStateException(formatInvokeError("Invocation failure", args), targetException);

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
@ -322,10 +322,10 @@ public abstract class AbstractMethodMessageHandler<T>
*/
protected final void detectHandlerMethods(Object handler) {
Class<?> handlerType;
if (handler instanceof String) {
if (handler instanceof String handlerName) {
ApplicationContext context = getApplicationContext();
Assert.state(context != null, "ApplicationContext is required for resolving handler bean names");
handlerType = context.getType((String) handler);
handlerType = context.getType(handlerName);
}
else {
handlerType = handler.getClass();
@ -404,11 +404,10 @@ public abstract class AbstractMethodMessageHandler<T>
*/
private HandlerMethod createHandlerMethod(Object handler, Method method) {
HandlerMethod handlerMethod;
if (handler instanceof String) {
if (handler instanceof String handlerName) {
ApplicationContext context = getApplicationContext();
Assert.state(context != null, "ApplicationContext is required for resolving handler bean names");
String beanName = (String) handler;
handlerMethod = new HandlerMethod(beanName, context.getAutowireCapableBeanFactory(), method);
handlerMethod = new HandlerMethod(handlerName, context.getAutowireCapableBeanFactory(), method);
}
else {
handlerMethod = new HandlerMethod(handler, method);

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
@ -196,8 +196,8 @@ final class DefaultRSocketRequester implements RSocketRequester {
private void createPayload(Object input, ResolvableType elementType) {
ReactiveAdapter adapter = getAdapter(input.getClass());
Publisher<?> publisher;
if (input instanceof Publisher) {
publisher = (Publisher<?>) input;
if (input instanceof Publisher<?> _publisher) {
publisher = _publisher;
}
else if (adapter != null) {
publisher = adapter.toPublisher(input);

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2023 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.
@ -222,8 +222,8 @@ final class DefaultRSocketStrategies implements RSocketStrategies {
MetadataExtractor extractor = (this.metadataExtractor != null ?
this.metadataExtractor : new DefaultMetadataExtractor(this.decoders));
if (extractor instanceof MetadataExtractorRegistry) {
this.metadataExtractors.forEach(consumer -> consumer.accept((MetadataExtractorRegistry) extractor));
if (extractor instanceof MetadataExtractorRegistry metadataExtractorRegistry) {
this.metadataExtractors.forEach(consumer -> consumer.accept(metadataExtractorRegistry));
}
return new DefaultRSocketStrategies(

View File

@ -50,9 +50,9 @@ public abstract class PayloadUtils {
*/
public static DataBuffer retainDataAndReleasePayload(Payload payload, DataBufferFactory bufferFactory) {
try {
if (bufferFactory instanceof NettyDataBufferFactory) {
if (bufferFactory instanceof NettyDataBufferFactory nettyBufferFactory) {
ByteBuf byteBuf = payload.sliceData().retain();
return ((NettyDataBufferFactory) bufferFactory).wrap(byteBuf);
return nettyBufferFactory.wrap(byteBuf);
}
else {
return bufferFactory.wrap(payload.getData());

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2023 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.
@ -156,8 +156,8 @@ class MessagingRSocket implements RSocket {
}
private int refCount(DataBuffer dataBuffer) {
return dataBuffer instanceof NettyDataBuffer ?
((NettyDataBuffer) dataBuffer).getNativeBuffer().refCnt() : 1;
return dataBuffer instanceof NettyDataBuffer nettyDataBuffer ?
nettyDataBuffer.getNativeBuffer().refCnt() : 1;
}
@SuppressWarnings("deprecation")

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
@ -484,7 +484,7 @@ public class RSocketMessageHandler extends MessageMappingMessageHandler {
Assert.notEmpty(candidateHandlers, "No handlers");
List<Object> handlers = new ArrayList<>(candidateHandlers.length);
for (Object obj : candidateHandlers) {
handlers.add(obj instanceof Class ? BeanUtils.instantiateClass((Class<?>) obj) : obj);
handlers.add(obj instanceof Class<?> clazz ? BeanUtils.instantiateClass(clazz) : obj);
}
RSocketMessageHandler handler = new RSocketMessageHandler();
handler.setHandlers(handlers);

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
@ -44,8 +44,8 @@ public class DestinationVariableArgumentResolver implements RSocketServiceArgume
}
if (argument != null) {
if (argument instanceof Collection) {
((Collection<?>) argument).forEach(requestValues::addRouteVariable);
if (argument instanceof Collection<?> collection) {
collection.forEach(requestValues::addRouteVariable);
return true;
}
else if (argument.getClass().isArray()) {

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2023 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.
@ -77,14 +77,8 @@ public class SimpMessageMappingInfo implements MessageCondition<SimpMessageMappi
@Override
public boolean equals(@Nullable Object other) {
if (this == other) {
return true;
}
if (!(other instanceof SimpMessageMappingInfo)) {
return false;
}
return this.delegate.equals(((SimpMessageMappingInfo) other).delegate);
public boolean equals(@Nullable Object obj) {
return (this == obj) || (obj instanceof SimpMessageMappingInfo that && this.delegate.equals(that.delegate));
}
@Override

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
@ -255,9 +255,9 @@ public class SimpMessagingTemplate extends AbstractMessageSendingTemplate<String
if (headers.containsKey(NativeMessageHeaderAccessor.NATIVE_HEADERS)) {
return headers;
}
if (headers instanceof MessageHeaders) {
if (headers instanceof MessageHeaders messageHeaders) {
SimpMessageHeaderAccessor accessor =
MessageHeaderAccessor.getAccessor((MessageHeaders) headers, SimpMessageHeaderAccessor.class);
MessageHeaderAccessor.getAccessor(messageHeaders, SimpMessageHeaderAccessor.class);
if (accessor != null) {
return headers;
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2023 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.
@ -212,8 +212,8 @@ public class SendToMethodReturnValueHandler implements HandlerMethodReturnValueH
protected String getUserName(Message<?> message, MessageHeaders headers) {
Principal principal = SimpMessageHeaderAccessor.getUser(headers);
if (principal != null) {
return (principal instanceof DestinationUserNameProvider ?
((DestinationUserNameProvider) principal).getDestinationUserName() : principal.getName());
return (principal instanceof DestinationUserNameProvider provider ?
provider.getDestinationUserName() : principal.getName());
}
return null;
}

View File

@ -305,8 +305,8 @@ public class SimpAnnotationMethodMessageHandler extends AbstractMethodMessageHan
@Override
protected List<HandlerMethodArgumentResolver> initArgumentResolvers() {
ApplicationContext context = getApplicationContext();
ConfigurableBeanFactory beanFactory = (context instanceof ConfigurableApplicationContext ?
((ConfigurableApplicationContext) context).getBeanFactory() : null);
ConfigurableBeanFactory beanFactory = (context instanceof ConfigurableApplicationContext cac ?
cac.getBeanFactory() : null);
List<HandlerMethodArgumentResolver> resolvers = new ArrayList<>();

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
@ -205,8 +205,8 @@ public abstract class AbstractBrokerMessageHandler
logger.info("Starting...");
this.clientInboundChannel.subscribe(this);
this.brokerChannel.subscribe(this);
if (this.clientInboundChannel instanceof InterceptableChannel) {
((InterceptableChannel) this.clientInboundChannel).addInterceptor(0, this.unsentDisconnectInterceptor);
if (this.clientInboundChannel instanceof InterceptableChannel ic) {
ic.addInterceptor(0, this.unsentDisconnectInterceptor);
}
startInternal();
this.running = true;
@ -224,8 +224,8 @@ public abstract class AbstractBrokerMessageHandler
stopInternal();
this.clientInboundChannel.unsubscribe(this);
this.brokerChannel.unsubscribe(this);
if (this.clientInboundChannel instanceof InterceptableChannel) {
((InterceptableChannel) this.clientInboundChannel).removeInterceptor(this.unsentDisconnectInterceptor);
if (this.clientInboundChannel instanceof InterceptableChannel ic) {
ic.removeInterceptor(this.unsentDisconnectInterceptor);
}
this.running = false;
logger.info("Stopped.");

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2023 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.
@ -474,9 +474,8 @@ public class DefaultSubscriptionRegistry extends AbstractSubscriptionRegistry {
}
@Override
public boolean equals(@Nullable Object other) {
return (this == other ||
(other instanceof Subscription && this.id.equals(((Subscription) other).id)));
public boolean equals(@Nullable Object obj) {
return (this == obj || (obj instanceof Subscription that && this.id.equals(that.id)));
}
@Override
@ -507,8 +506,8 @@ public class DefaultSubscriptionRegistry extends AbstractSubscriptionRegistry {
@SuppressWarnings("rawtypes")
public TypedValue read(EvaluationContext context, @Nullable Object target, String name) {
Object value;
if (target instanceof Message) {
value = name.equals("headers") ? ((Message) target).getHeaders() : null;
if (target instanceof Message message) {
value = name.equals("headers") ? message.getHeaders() : null;
}
else if (target instanceof MessageHeaders headers) {
SimpMessageHeaderAccessor accessor =

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
@ -150,12 +150,12 @@ public class OrderedMessageChannelDecorator implements MessageChannel {
Assert.isInstanceOf(ExecutorSubscribableChannel.class, channel,
"An ExecutorSubscribableChannel is required for 'preservePublishOrder'");
ExecutorSubscribableChannel execChannel = (ExecutorSubscribableChannel) channel;
if (execChannel.getInterceptors().stream().noneMatch(i -> i instanceof CallbackInterceptor)) {
if (execChannel.getInterceptors().stream().noneMatch(CallbackInterceptor.class::isInstance)) {
execChannel.addInterceptor(0, new CallbackInterceptor());
}
}
else if (channel instanceof ExecutorSubscribableChannel execChannel) {
execChannel.getInterceptors().stream().filter(i -> i instanceof CallbackInterceptor)
execChannel.getInterceptors().stream().filter(CallbackInterceptor.class::isInstance)
.findFirst().map(execChannel::removeInterceptor);
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
@ -130,8 +130,8 @@ public class SimpleBrokerMessageHandler extends AbstractBrokerMessageHandler {
}
private void initPathMatcherToUse() {
if (this.pathMatcher != null && this.subscriptionRegistry instanceof DefaultSubscriptionRegistry) {
((DefaultSubscriptionRegistry) this.subscriptionRegistry).setPathMatcher(this.pathMatcher);
if (this.pathMatcher != null && this.subscriptionRegistry instanceof DefaultSubscriptionRegistry defaultRegistry) {
defaultRegistry.setPathMatcher(this.pathMatcher);
}
}
@ -151,8 +151,8 @@ public class SimpleBrokerMessageHandler extends AbstractBrokerMessageHandler {
}
private void initCacheLimitToUse() {
if (this.cacheLimit != null && this.subscriptionRegistry instanceof DefaultSubscriptionRegistry) {
((DefaultSubscriptionRegistry) this.subscriptionRegistry).setCacheLimit(this.cacheLimit);
if (this.cacheLimit != null && this.subscriptionRegistry instanceof DefaultSubscriptionRegistry defaultRegistry) {
defaultRegistry.setCacheLimit(this.cacheLimit);
}
}
@ -178,8 +178,8 @@ public class SimpleBrokerMessageHandler extends AbstractBrokerMessageHandler {
}
private void initSelectorHeaderNameToUse() {
if (this.subscriptionRegistry instanceof DefaultSubscriptionRegistry) {
((DefaultSubscriptionRegistry) this.subscriptionRegistry).setSelectorHeaderName(this.selectorHeaderName);
if (this.subscriptionRegistry instanceof DefaultSubscriptionRegistry defaultRegistry) {
defaultRegistry.setSelectorHeaderName(this.selectorHeaderName);
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
@ -534,7 +534,7 @@ public class StompBrokerRelayMessageHandler extends AbstractBrokerMessageHandler
return;
}
StompHeaderAccessor stompAccessor;
StompHeaderAccessor stompHeaderAccessor;
StompCommand command;
MessageHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message, MessageHeaderAccessor.class);
@ -542,15 +542,15 @@ public class StompBrokerRelayMessageHandler extends AbstractBrokerMessageHandler
throw new IllegalStateException(
"No header accessor (not using the SimpMessagingTemplate?): " + message);
}
else if (accessor instanceof StompHeaderAccessor) {
stompAccessor = (StompHeaderAccessor) accessor;
command = stompAccessor.getCommand();
else if (accessor instanceof StompHeaderAccessor _stompHeaderAccessor) {
stompHeaderAccessor = _stompHeaderAccessor;
command = stompHeaderAccessor.getCommand();
}
else if (accessor instanceof SimpMessageHeaderAccessor) {
stompAccessor = StompHeaderAccessor.wrap(message);
command = stompAccessor.getCommand();
stompHeaderAccessor = StompHeaderAccessor.wrap(message);
command = stompHeaderAccessor.getCommand();
if (command == null) {
command = stompAccessor.updateStompCommandAsClientMessage();
command = stompHeaderAccessor.updateStompCommandAsClientMessage();
}
}
else {
@ -559,14 +559,14 @@ public class StompBrokerRelayMessageHandler extends AbstractBrokerMessageHandler
}
if (sessionId == null) {
if (!SimpMessageType.MESSAGE.equals(stompAccessor.getMessageType())) {
if (!SimpMessageType.MESSAGE.equals(stompHeaderAccessor.getMessageType())) {
if (logger.isErrorEnabled()) {
logger.error("Only STOMP SEND supported from within the server side. Ignoring " + message);
}
return;
}
sessionId = SYSTEM_SESSION_ID;
stompAccessor.setSessionId(sessionId);
stompHeaderAccessor.setSessionId(sessionId);
}
if (StompCommand.CONNECT.equals(command) || StompCommand.STOMP.equals(command)) {
@ -577,15 +577,15 @@ public class StompBrokerRelayMessageHandler extends AbstractBrokerMessageHandler
return;
}
if (logger.isDebugEnabled()) {
logger.debug(stompAccessor.getShortLogMessage(EMPTY_PAYLOAD));
logger.debug(stompHeaderAccessor.getShortLogMessage(EMPTY_PAYLOAD));
}
stompAccessor = (stompAccessor.isMutable() ? stompAccessor : StompHeaderAccessor.wrap(message));
stompAccessor.setLogin(this.clientLogin);
stompAccessor.setPasscode(this.clientPasscode);
stompHeaderAccessor = (stompHeaderAccessor.isMutable() ? stompHeaderAccessor : StompHeaderAccessor.wrap(message));
stompHeaderAccessor.setLogin(this.clientLogin);
stompHeaderAccessor.setPasscode(this.clientPasscode);
if (getVirtualHost() != null) {
stompAccessor.setHost(getVirtualHost());
stompHeaderAccessor.setHost(getVirtualHost());
}
RelayConnectionHandler handler = new RelayConnectionHandler(sessionId, stompAccessor);
RelayConnectionHandler handler = new RelayConnectionHandler(sessionId, stompHeaderAccessor);
this.connectionHandlers.put(sessionId, handler);
this.stats.incrementConnectCount();
Assert.state(this.tcpClient != null, "No TCP client available");
@ -600,7 +600,7 @@ public class StompBrokerRelayMessageHandler extends AbstractBrokerMessageHandler
return;
}
this.stats.incrementDisconnectCount();
handler.forward(message, stompAccessor);
handler.forward(message, stompHeaderAccessor);
}
else {
RelayConnectionHandler handler = this.connectionHandlers.get(sessionId);
@ -611,7 +611,7 @@ public class StompBrokerRelayMessageHandler extends AbstractBrokerMessageHandler
return;
}
String destination = stompAccessor.getDestination();
String destination = stompHeaderAccessor.getDestination();
if (command != null && command.requiresDestination() && !checkDestinationPrefix(destination)) {
// Not a broker destination but send a heartbeat to keep the connection
if (handler.shouldSendHeartbeatForIgnoredMessage()) {
@ -620,7 +620,7 @@ public class StompBrokerRelayMessageHandler extends AbstractBrokerMessageHandler
return;
}
handler.forward(message, stompAccessor);
handler.forward(message, stompHeaderAccessor);
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
@ -554,9 +554,8 @@ public class StompHeaders implements MultiValueMap<String, String>, Serializable
@Override
public boolean equals(@Nullable Object other) {
return (this == other || (other instanceof StompHeaders &&
this.headers.equals(((StompHeaders) other).headers)));
public boolean equals(@Nullable Object obj) {
return (this == obj || (obj instanceof StompHeaders that && this.headers.equals(that.headers)));
}
@Override

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2021 the original author or authors.
* Copyright 2002-2023 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.
@ -369,8 +369,8 @@ public class MultiServerUserRegistry implements SimpUserRegistry, SmartApplicati
@Override
public boolean equals(@Nullable Object other) {
return (this == other || (other instanceof SimpUser && this.name.equals(((SimpUser) other).getName())));
public boolean equals(@Nullable Object obj) {
return (this == obj || (obj instanceof SimpUser that && this.name.equals(that.getName())));
}
@Override
@ -455,8 +455,8 @@ public class MultiServerUserRegistry implements SimpUserRegistry, SmartApplicati
}
@Override
public boolean equals(@Nullable Object other) {
return (this == other || (other instanceof SimpSession && getId().equals(((SimpSession) other).getId())));
public boolean equals(@Nullable Object obj) {
return (this == obj || (obj instanceof SimpSession that && this.id.equals(that.getId())));
}
@Override

View File

@ -143,8 +143,8 @@ public abstract class AbstractMessageChannel implements MessageChannel, Intercep
}
catch (Exception ex) {
chain.triggerAfterSendCompletion(messageToUse, this, sent, ex);
if (ex instanceof MessagingException) {
throw (MessagingException) ex;
if (ex instanceof MessagingException messagingException) {
throw messagingException;
}
throw new MessageDeliveryException(messageToUse,"Failed to send message to " + this, ex);
}

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2018 the original author or authors.
* Copyright 2002-2023 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,8 +86,8 @@ public class ExecutorSubscribableChannel extends AbstractSubscribableChannel {
}
private void updateExecutorInterceptorsFor(ChannelInterceptor interceptor) {
if (interceptor instanceof ExecutorChannelInterceptor) {
this.executorInterceptors.add((ExecutorChannelInterceptor) interceptor);
if (interceptor instanceof ExecutorChannelInterceptor executorChannelInterceptor) {
this.executorInterceptors.add(executorChannelInterceptor);
}
}
@ -146,8 +146,8 @@ public class ExecutorSubscribableChannel extends AbstractSubscribableChannel {
}
catch (Exception ex) {
triggerAfterMessageHandled(message, ex);
if (ex instanceof MessagingException) {
throw (MessagingException) ex;
if (ex instanceof MessagingException messagingException) {
throw messagingException;
}
String description = "Failed to handle " + message + " to " + this + " in " + this.messageHandler;
throw new MessageDeliveryException(message, description, ex);

View File

@ -110,8 +110,8 @@ public class GenericMessage<T> implements Message<T>, Serializable {
public String toString() {
StringBuilder sb = new StringBuilder(getClass().getSimpleName());
sb.append(" [payload=");
if (this.payload instanceof byte[]) {
sb.append("byte[").append(((byte[]) this.payload).length).append(']');
if (this.payload instanceof byte[] bytes) {
sb.append("byte[").append(bytes.length).append(']');
}
else {
sb.append(this.payload);

View File

@ -416,7 +416,7 @@ public class MessageHeaderAccessor {
if (value == null) {
return null;
}
return (value instanceof UUID ? (UUID) value : UUID.fromString(value.toString()));
return (value instanceof UUID uuid ? uuid : UUID.fromString(value.toString()));
}
@Nullable
@ -425,7 +425,7 @@ public class MessageHeaderAccessor {
if (value == null) {
return null;
}
return (value instanceof Long ? (Long) value : Long.parseLong(value.toString()));
return (value instanceof Long num ? num : Long.parseLong(value.toString()));
}
public void setContentType(MimeType contentType) {
@ -438,7 +438,7 @@ public class MessageHeaderAccessor {
if (value == null) {
return null;
}
return (value instanceof MimeType ? (MimeType) value : MimeType.valueOf(value.toString()));
return (value instanceof MimeType mimeType ? mimeType : MimeType.valueOf(value.toString()));
}
private Charset getCharset() {