diff --git a/spring-messaging/src/main/java/org/springframework/messaging/MessageHeaders.java b/spring-messaging/src/main/java/org/springframework/messaging/MessageHeaders.java index 7dc660eeaf..0c22dae2bc 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/MessageHeaders.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/MessageHeaders.java @@ -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, 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 diff --git a/spring-messaging/src/main/java/org/springframework/messaging/converter/AbstractJsonMessageConverter.java b/spring-messaging/src/main/java/org/springframework/messaging/converter/AbstractJsonMessageConverter.java index a7a9597104..034b450c71 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/converter/AbstractJsonMessageConverter.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/converter/AbstractJsonMessageConverter.java @@ -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 diff --git a/spring-messaging/src/main/java/org/springframework/messaging/converter/CompositeMessageConverter.java b/spring-messaging/src/main/java/org/springframework/messaging/converter/CompositeMessageConverter.java index b213a323ca..0c46db9234 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/converter/CompositeMessageConverter.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/converter/CompositeMessageConverter.java @@ -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; diff --git a/spring-messaging/src/main/java/org/springframework/messaging/converter/DefaultContentTypeResolver.java b/spring-messaging/src/main/java/org/springframework/messaging/converter/DefaultContentTypeResolver.java index fff67d348e..8dca8058c2 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/converter/DefaultContentTypeResolver.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/converter/DefaultContentTypeResolver.java @@ -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( 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 79378eb618..421b014ef9 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 @@ -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... diff --git a/spring-messaging/src/main/java/org/springframework/messaging/converter/MarshallingMessageConverter.java b/spring-messaging/src/main/java/org/springframework/messaging/converter/MarshallingMessageConverter.java index 782915011c..0821559db6 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/converter/MarshallingMessageConverter.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/converter/MarshallingMessageConverter.java @@ -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())); diff --git a/spring-messaging/src/main/java/org/springframework/messaging/core/AbstractMessageSendingTemplate.java b/spring-messaging/src/main/java/org/springframework/messaging/core/AbstractMessageSendingTemplate.java index 0622061baf..b4cf36ae00 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/core/AbstractMessageSendingTemplate.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/core/AbstractMessageSendingTemplate.java @@ -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 implements MessageSendin Map 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(); diff --git a/spring-messaging/src/main/java/org/springframework/messaging/core/GenericMessagingTemplate.java b/spring-messaging/src/main/java/org/springframework/messaging/core/GenericMessagingTemplate.java index 5b361ca827..95952f4b0b 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/core/GenericMessagingTemplate.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/core/GenericMessagingTemplate.java @@ -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; diff --git a/spring-messaging/src/main/java/org/springframework/messaging/handler/DestinationPatternsMessageCondition.java b/spring-messaging/src/main/java/org/springframework/messaging/handler/DestinationPatternsMessageCondition.java index c8a5bf219b..9c10ea0816 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/handler/DestinationPatternsMessageCondition.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/handler/DestinationPatternsMessageCondition.java @@ -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 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); } diff --git a/spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/reactive/MessageMappingMessageHandler.java b/spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/reactive/MessageMappingMessageHandler.java index 771680dbc4..ff44fa0a4d 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/reactive/MessageMappingMessageHandler.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/reactive/MessageMappingMessageHandler.java @@ -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 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)); diff --git a/spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/support/DefaultMessageHandlerMethodFactory.java b/spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/support/DefaultMessageHandlerMethodFactory.java index b3da10249d..b1022ff357 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/support/DefaultMessageHandlerMethodFactory.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/handler/annotation/support/DefaultMessageHandlerMethodFactory.java @@ -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 initArgumentResolvers() { List 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)); 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 8b0e23fb7e..022d1f3f1a 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-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; 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 48fce4c124..b640581891 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 @@ -299,10 +299,10 @@ public abstract class AbstractMethodMessageHandler */ 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 } } - 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); } } diff --git a/spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/InvocableHandlerMethod.java b/spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/InvocableHandlerMethod.java index 2ab4642774..76a7aa4e72 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/InvocableHandlerMethod.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/InvocableHandlerMethod.java @@ -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); 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 0ce87e47b1..6677c252c9 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 @@ -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 */ 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 */ 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); diff --git a/spring-messaging/src/main/java/org/springframework/messaging/rsocket/DefaultRSocketRequester.java b/spring-messaging/src/main/java/org/springframework/messaging/rsocket/DefaultRSocketRequester.java index d8403afc97..f318abd113 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/rsocket/DefaultRSocketRequester.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/rsocket/DefaultRSocketRequester.java @@ -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); diff --git a/spring-messaging/src/main/java/org/springframework/messaging/rsocket/DefaultRSocketStrategies.java b/spring-messaging/src/main/java/org/springframework/messaging/rsocket/DefaultRSocketStrategies.java index 1c2698bdaa..ec6ceab06b 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/rsocket/DefaultRSocketStrategies.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/rsocket/DefaultRSocketStrategies.java @@ -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( diff --git a/spring-messaging/src/main/java/org/springframework/messaging/rsocket/PayloadUtils.java b/spring-messaging/src/main/java/org/springframework/messaging/rsocket/PayloadUtils.java index 2793bff567..2649daa3f2 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/rsocket/PayloadUtils.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/rsocket/PayloadUtils.java @@ -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()); diff --git a/spring-messaging/src/main/java/org/springframework/messaging/rsocket/annotation/support/MessagingRSocket.java b/spring-messaging/src/main/java/org/springframework/messaging/rsocket/annotation/support/MessagingRSocket.java index a483b32dde..b7686c46f5 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/rsocket/annotation/support/MessagingRSocket.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/rsocket/annotation/support/MessagingRSocket.java @@ -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") diff --git a/spring-messaging/src/main/java/org/springframework/messaging/rsocket/annotation/support/RSocketMessageHandler.java b/spring-messaging/src/main/java/org/springframework/messaging/rsocket/annotation/support/RSocketMessageHandler.java index 5bf5458dd8..0592c14ec9 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/rsocket/annotation/support/RSocketMessageHandler.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/rsocket/annotation/support/RSocketMessageHandler.java @@ -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 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); diff --git a/spring-messaging/src/main/java/org/springframework/messaging/rsocket/service/DestinationVariableArgumentResolver.java b/spring-messaging/src/main/java/org/springframework/messaging/rsocket/service/DestinationVariableArgumentResolver.java index 6cc9cabc6e..99b00160da 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/rsocket/service/DestinationVariableArgumentResolver.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/rsocket/service/DestinationVariableArgumentResolver.java @@ -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()) { diff --git a/spring-messaging/src/main/java/org/springframework/messaging/simp/SimpMessageMappingInfo.java b/spring-messaging/src/main/java/org/springframework/messaging/simp/SimpMessageMappingInfo.java index 26bfbe2e85..b2c4737200 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/simp/SimpMessageMappingInfo.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/simp/SimpMessageMappingInfo.java @@ -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 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; } diff --git a/spring-messaging/src/main/java/org/springframework/messaging/simp/annotation/support/SimpAnnotationMethodMessageHandler.java b/spring-messaging/src/main/java/org/springframework/messaging/simp/annotation/support/SimpAnnotationMethodMessageHandler.java index 309c14ca91..5d6c1d2da5 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/simp/annotation/support/SimpAnnotationMethodMessageHandler.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/simp/annotation/support/SimpAnnotationMethodMessageHandler.java @@ -305,8 +305,8 @@ public class SimpAnnotationMethodMessageHandler extends AbstractMethodMessageHan @Override protected List initArgumentResolvers() { ApplicationContext context = getApplicationContext(); - ConfigurableBeanFactory beanFactory = (context instanceof ConfigurableApplicationContext ? - ((ConfigurableApplicationContext) context).getBeanFactory() : null); + ConfigurableBeanFactory beanFactory = (context instanceof ConfigurableApplicationContext cac ? + cac.getBeanFactory() : null); List resolvers = new ArrayList<>(); 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 e4f7f04314..e21725519b 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-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."); 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 e46e34de67..33bda8bec6 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-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 = 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 0638e2f4f8..63c1830e87 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-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); } diff --git a/spring-messaging/src/main/java/org/springframework/messaging/simp/broker/SimpleBrokerMessageHandler.java b/spring-messaging/src/main/java/org/springframework/messaging/simp/broker/SimpleBrokerMessageHandler.java index c954bc71ba..e3440d755a 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/simp/broker/SimpleBrokerMessageHandler.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/simp/broker/SimpleBrokerMessageHandler.java @@ -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); } } 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 516fa12e1f..43fcf18d03 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 @@ -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); } } diff --git a/spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompHeaders.java b/spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompHeaders.java index f39b7ad9ac..2bc1251dd7 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompHeaders.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/StompHeaders.java @@ -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, 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 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 c6f32b9f3c..f10bd29055 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-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 diff --git a/spring-messaging/src/main/java/org/springframework/messaging/support/AbstractMessageChannel.java b/spring-messaging/src/main/java/org/springframework/messaging/support/AbstractMessageChannel.java index 40d6a09899..de64124185 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/support/AbstractMessageChannel.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/support/AbstractMessageChannel.java @@ -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); } diff --git a/spring-messaging/src/main/java/org/springframework/messaging/support/ExecutorSubscribableChannel.java b/spring-messaging/src/main/java/org/springframework/messaging/support/ExecutorSubscribableChannel.java index 2e2971c66e..1a2081f68d 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/support/ExecutorSubscribableChannel.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/support/ExecutorSubscribableChannel.java @@ -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); diff --git a/spring-messaging/src/main/java/org/springframework/messaging/support/GenericMessage.java b/spring-messaging/src/main/java/org/springframework/messaging/support/GenericMessage.java index a4b2a05de8..dbb2efa0b7 100644 --- a/spring-messaging/src/main/java/org/springframework/messaging/support/GenericMessage.java +++ b/spring-messaging/src/main/java/org/springframework/messaging/support/GenericMessage.java @@ -110,8 +110,8 @@ public class GenericMessage implements Message, 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); 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 f643f05c62..0331203200 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 @@ -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() {