diff --git a/spring-amqp/src/main/java/org/springframework/amqp/core/Address.java b/spring-amqp/src/main/java/org/springframework/amqp/core/Address.java index c1d10642fb..564c752d16 100644 --- a/spring-amqp/src/main/java/org/springframework/amqp/core/Address.java +++ b/spring-amqp/src/main/java/org/springframework/amqp/core/Address.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2024 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. @@ -16,6 +16,7 @@ package org.springframework.amqp.core; +import java.util.Objects; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -39,6 +40,7 @@ * @author Dave Syer * @author Artem Bilan * @author Gary Russell + * @author Ngoc Nhan */ public class Address { @@ -111,21 +113,9 @@ public String getRoutingKey() { @Override public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - - Address address = (Address) o; - - return !(this.exchangeName != null - ? !this.exchangeName.equals(address.exchangeName) - : address.exchangeName != null) - && !(this.routingKey != null - ? !this.routingKey.equals(address.routingKey) - : address.routingKey != null); + return o instanceof Address address + && Objects.equals(this.exchangeName, address.exchangeName) + && Objects.equals(this.routingKey, address.routingKey); } @Override diff --git a/spring-amqp/src/main/java/org/springframework/amqp/core/Binding.java b/spring-amqp/src/main/java/org/springframework/amqp/core/Binding.java index a5ee74273f..ac2ac58acd 100644 --- a/spring-amqp/src/main/java/org/springframework/amqp/core/Binding.java +++ b/spring-amqp/src/main/java/org/springframework/amqp/core/Binding.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2023 the original author or authors. + * Copyright 2002-2024 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. @@ -30,6 +30,7 @@ * @author Mark Fisher * @author Dave Syer * @author Gary Russell + * @author Ngoc Nhan * * @see AmqpAdmin */ @@ -74,7 +75,7 @@ public Binding(@Nullable Queue lazyQueue, @Nullable String destination, Destinat String exchange, @Nullable String routingKey, @Nullable Map arguments) { super(arguments); - Assert.isTrue(lazyQueue == null || destinationType.equals(DestinationType.QUEUE), + Assert.isTrue(lazyQueue == null || destinationType == DestinationType.QUEUE, "'lazyQueue' must be null for destination type " + destinationType); Assert.isTrue(lazyQueue != null || destination != null, "`destination` cannot be null"); this.lazyQueue = lazyQueue; diff --git a/spring-amqp/src/main/java/org/springframework/amqp/core/BindingBuilder.java b/spring-amqp/src/main/java/org/springframework/amqp/core/BindingBuilder.java index 5eceadd960..d297f16d9d 100644 --- a/spring-amqp/src/main/java/org/springframework/amqp/core/BindingBuilder.java +++ b/spring-amqp/src/main/java/org/springframework/amqp/core/BindingBuilder.java @@ -231,12 +231,12 @@ public static final class TopicExchangeRoutingKeyConfigurer extends AbstractRout public Binding with(String routingKey) { return new Binding(destination.queue, destination.name, destination.type, exchange, routingKey, - Collections.emptyMap()); + Collections.emptyMap()); } public Binding with(Enum routingKeyEnum) { return new Binding(destination.queue, destination.name, destination.type, exchange, - routingKeyEnum.toString(), Collections.emptyMap()); + routingKeyEnum.toString(), Collections.emptyMap()); } } @@ -282,7 +282,7 @@ public Binding and(Map map) { public Binding noargs() { return new Binding(this.configurer.destination.queue, this.configurer.destination.name, this.configurer.destination.type, this.configurer.exchange, - this.routingKey, Collections.emptyMap()); + this.routingKey, Collections.emptyMap()); } } @@ -298,17 +298,17 @@ public static final class DirectExchangeRoutingKeyConfigurer extends AbstractRou public Binding with(String routingKey) { return new Binding(destination.queue, destination.name, destination.type, exchange, routingKey, - Collections.emptyMap()); + Collections.emptyMap()); } public Binding with(Enum routingKeyEnum) { return new Binding(destination.queue, destination.name, destination.type, exchange, - routingKeyEnum.toString(), Collections.emptyMap()); + routingKeyEnum.toString(), Collections.emptyMap()); } public Binding withQueueName() { return new Binding(destination.queue, destination.name, destination.type, exchange, destination.name, - Collections.emptyMap()); + Collections.emptyMap()); } } diff --git a/spring-amqp/src/main/java/org/springframework/amqp/support/converter/AbstractJavaTypeMapper.java b/spring-amqp/src/main/java/org/springframework/amqp/support/converter/AbstractJavaTypeMapper.java index 76531523be..e19c62dc17 100644 --- a/spring-amqp/src/main/java/org/springframework/amqp/support/converter/AbstractJavaTypeMapper.java +++ b/spring-amqp/src/main/java/org/springframework/amqp/support/converter/AbstractJavaTypeMapper.java @@ -99,11 +99,9 @@ protected String retrieveHeader(MessageProperties properties, String headerName) protected String retrieveHeaderAsString(MessageProperties properties, String headerName) { Map headers = properties.getHeaders(); Object classIdFieldNameValue = headers.get(headerName); - String classId = null; - if (classIdFieldNameValue != null) { - classId = classIdFieldNameValue.toString(); - } - return classId; + return classIdFieldNameValue != null + ? classIdFieldNameValue.toString() + : null; } private void createReverseMap() { diff --git a/spring-amqp/src/main/java/org/springframework/amqp/support/converter/ContentTypeDelegatingMessageConverter.java b/spring-amqp/src/main/java/org/springframework/amqp/support/converter/ContentTypeDelegatingMessageConverter.java index 0fad672ee1..35fb9d901c 100644 --- a/spring-amqp/src/main/java/org/springframework/amqp/support/converter/ContentTypeDelegatingMessageConverter.java +++ b/spring-amqp/src/main/java/org/springframework/amqp/support/converter/ContentTypeDelegatingMessageConverter.java @@ -109,9 +109,8 @@ protected MessageConverter getConverterForContentType(String contentType) { if (delegate == null) { throw new MessageConversionException("No delegate converter is specified for content type " + contentType); } - else { - return delegate; - } + + return delegate; } } diff --git a/spring-amqp/src/main/java/org/springframework/amqp/support/converter/MarshallingMessageConverter.java b/spring-amqp/src/main/java/org/springframework/amqp/support/converter/MarshallingMessageConverter.java index 25f77c9cd7..6d94890221 100644 --- a/spring-amqp/src/main/java/org/springframework/amqp/support/converter/MarshallingMessageConverter.java +++ b/spring-amqp/src/main/java/org/springframework/amqp/support/converter/MarshallingMessageConverter.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2024 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. @@ -40,6 +40,7 @@ * @author Arjen Poutsma * @author Juergen Hoeller * @author James Carr + * @author Ngoc Nhan * @see org.springframework.amqp.core.AmqpTemplate#convertAndSend(Object) * @see org.springframework.amqp.core.AmqpTemplate#receiveAndConvert() */ @@ -77,10 +78,9 @@ public MarshallingMessageConverter(Marshaller marshaller) { "interface. Please set an Unmarshaller explicitly by using the " + "MarshallingMessageConverter(Marshaller, Unmarshaller) constructor."); } - else { - this.marshaller = marshaller; - this.unmarshaller = (Unmarshaller) marshaller; - } + + this.marshaller = marshaller; + this.unmarshaller = (Unmarshaller) marshaller; } /** diff --git a/spring-amqp/src/main/java/org/springframework/amqp/support/converter/MessagingMessageConverter.java b/spring-amqp/src/main/java/org/springframework/amqp/support/converter/MessagingMessageConverter.java index 0c91aac106..5bc545086b 100644 --- a/spring-amqp/src/main/java/org/springframework/amqp/support/converter/MessagingMessageConverter.java +++ b/spring-amqp/src/main/java/org/springframework/amqp/support/converter/MessagingMessageConverter.java @@ -1,5 +1,5 @@ /* - * Copyright 2014-2020 the original author or authors. + * Copyright 2014-2024 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. @@ -40,6 +40,7 @@ * is considered to be a request). * * @author Stephane Nicoll + * @author Ngoc Nhan * @since 1.4 */ public class MessagingMessageConverter implements MessageConverter, InitializingBean { @@ -104,11 +105,10 @@ public void afterPropertiesSet() { public org.springframework.amqp.core.Message toMessage(Object object, MessageProperties messageProperties) throws MessageConversionException { - if (!(object instanceof Message)) { + if (!(object instanceof Message input)) { throw new IllegalArgumentException("Could not convert [" + object + "] - only [" + Message.class.getName() + "] is handled by this converter"); } - Message input = (Message) object; this.headerMapper.fromHeaders(input.getHeaders(), messageProperties); org.springframework.amqp.core.Message amqpMessage = this.payloadConverter.toMessage( input.getPayload(), messageProperties); diff --git a/spring-amqp/src/main/java/org/springframework/amqp/support/converter/RemoteInvocationResult.java b/spring-amqp/src/main/java/org/springframework/amqp/support/converter/RemoteInvocationResult.java index 7d08e1d139..6945162895 100644 --- a/spring-amqp/src/main/java/org/springframework/amqp/support/converter/RemoteInvocationResult.java +++ b/spring-amqp/src/main/java/org/springframework/amqp/support/converter/RemoteInvocationResult.java @@ -26,6 +26,7 @@ * * @author Juergen Hoeller * @author Gary Russell + * @author Ngoc Nhan * @since 3.0 */ public class RemoteInvocationResult implements Serializable { @@ -142,16 +143,13 @@ public boolean hasInvocationTargetException() { @Nullable public Object recreate() throws Throwable { if (this.exception != null) { - Throwable exToThrow = this.exception; - if (this.exception instanceof InvocationTargetException invocationTargetException) { - exToThrow = invocationTargetException.getTargetException(); - } + Throwable exToThrow = this.exception instanceof InvocationTargetException invocationTargetException + ? invocationTargetException.getTargetException() + : this.exception; RemoteInvocationUtils.fillInClientStackTraceIfPossible(exToThrow); throw exToThrow; } - else { - return this.value; - } + return this.value; } } diff --git a/spring-amqp/src/main/java/org/springframework/amqp/support/postprocessor/AbstractDecompressingPostProcessor.java b/spring-amqp/src/main/java/org/springframework/amqp/support/postprocessor/AbstractDecompressingPostProcessor.java index 249f0e25e4..3c87829cff 100644 --- a/spring-amqp/src/main/java/org/springframework/amqp/support/postprocessor/AbstractDecompressingPostProcessor.java +++ b/spring-amqp/src/main/java/org/springframework/amqp/support/postprocessor/AbstractDecompressingPostProcessor.java @@ -38,6 +38,7 @@ * the final content encoding of the decompressed message. * * @author Gary Russell + * @author Ngoc Nhan * @since 1.4.2 */ public abstract class AbstractDecompressingPostProcessor implements MessagePostProcessor, Ordered { @@ -115,9 +116,8 @@ public Message postProcessMessage(Message message) throws AmqpException { throw new AmqpIOException(e); } } - else { - return message; - } + + return message; } /** diff --git a/spring-amqp/src/main/java/org/springframework/amqp/support/postprocessor/DelegatingDecompressingPostProcessor.java b/spring-amqp/src/main/java/org/springframework/amqp/support/postprocessor/DelegatingDecompressingPostProcessor.java index 9474d1b7c8..0651734b8c 100644 --- a/spring-amqp/src/main/java/org/springframework/amqp/support/postprocessor/DelegatingDecompressingPostProcessor.java +++ b/spring-amqp/src/main/java/org/springframework/amqp/support/postprocessor/DelegatingDecompressingPostProcessor.java @@ -98,22 +98,20 @@ public Message postProcessMessage(Message message) throws AmqpException { if (encoding == null) { return message; } - else { - int delimAt = encoding.indexOf(':'); - if (delimAt < 0) { - delimAt = encoding.indexOf(','); - } - if (delimAt > 0) { - encoding = encoding.substring(0, delimAt); - } - MessagePostProcessor decompressor = this.decompressors.get(encoding); - if (decompressor != null) { - return decompressor.postProcessMessage(message); - } - else { - return message; - } + + int delimAt = encoding.indexOf(':'); + if (delimAt < 0) { + delimAt = encoding.indexOf(','); + } + if (delimAt > 0) { + encoding = encoding.substring(0, delimAt); } + MessagePostProcessor decompressor = this.decompressors.get(encoding); + if (decompressor != null) { + return decompressor.postProcessMessage(message); + } + + return message; } } diff --git a/spring-amqp/src/main/java/org/springframework/amqp/utils/test/TestUtils.java b/spring-amqp/src/main/java/org/springframework/amqp/utils/test/TestUtils.java index 5934aa6a6a..f8c802e558 100644 --- a/spring-amqp/src/main/java/org/springframework/amqp/utils/test/TestUtils.java +++ b/spring-amqp/src/main/java/org/springframework/amqp/utils/test/TestUtils.java @@ -1,5 +1,5 @@ /* - * Copyright 2013-2019 the original author or authors. + * Copyright 2013-2024 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. @@ -25,6 +25,7 @@ * @author Iwein Fuld * @author Oleg Zhurakousky * @author Gary Russell + * @author Ngoc Nhan * @since 1.2 */ public final class TestUtils { @@ -47,13 +48,14 @@ public static Object getPropertyValue(Object root, String propertyPath) { value = accessor.getPropertyValue(tokens[i]); if (value != null) { accessor = new DirectFieldAccessor(value); + continue; } - else if (i == tokens.length - 1) { + + if (i == tokens.length - 1) { return null; } - else { - throw new IllegalArgumentException("intermediate property '" + tokens[i] + "' is null"); - } + + throw new IllegalArgumentException("intermediate property '" + tokens[i] + "' is null"); } return value; } diff --git a/spring-amqp/src/test/java/org/springframework/amqp/core/AddressTests.java b/spring-amqp/src/test/java/org/springframework/amqp/core/AddressTests.java index feaea573b2..17c24e4130 100644 --- a/spring-amqp/src/test/java/org/springframework/amqp/core/AddressTests.java +++ b/spring-amqp/src/test/java/org/springframework/amqp/core/AddressTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2024 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. @@ -25,6 +25,7 @@ * @author Mark Fisher * @author Artem Bilan * @author Gary Russell + * @author Ngoc Nhan */ public class AddressTests { @@ -100,6 +101,9 @@ public void testDirectReplyTo() { @Test public void testEquals() { assertThat(new Address("foo/bar")).isEqualTo(new Address("foo/bar")); + assertThat(new Address("foo", null)).isEqualTo(new Address("foo", null)); + assertThat(new Address(null, "bar")).isEqualTo(new Address(null, "bar")); + assertThat(new Address(null, null)).isEqualTo(new Address(null, null)); } }