Skip to content

Commit 2ef76b4

Browse files
garyrussellartembilan
authored andcommitted
More Sonar issue resolutions
1 parent 0bdde7e commit 2ef76b4

19 files changed

+322
-257
lines changed

spring-amqp/src/main/java/org/springframework/amqp/core/AbstractDeclarable.java

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,19 +78,19 @@ public void setIgnoreDeclarationExceptions(boolean ignoreDeclarationExceptions)
7878
* ({@code setAdminsThatShouldDeclare((AmqpAdmin) null)} or
7979
* {@code setAdminsThatShouldDeclare((AmqpAdmin[]) null)}). Clearing the collection resets
8080
* the behavior such that all admins will declare the object.
81-
* @param admins The admins.
81+
* @param adminArgs The admins.
8282
*/
83-
public void setAdminsThatShouldDeclare(Object... admins) {
84-
Collection<Object> declaringAdmins = new ArrayList<Object>();
85-
if (admins != null) {
86-
if (admins.length > 1) {
87-
Assert.noNullElements(admins, "'admins' cannot contain null elements");
83+
public void setAdminsThatShouldDeclare(Object... adminArgs) {
84+
Collection<Object> admins = new ArrayList<Object>();
85+
if (adminArgs != null) {
86+
if (adminArgs.length > 1) {
87+
Assert.noNullElements(adminArgs, "'admins' cannot contain null elements");
8888
}
89-
if (admins.length > 0 && !(admins.length == 1 && admins[0] == null)) {
90-
declaringAdmins.addAll(Arrays.asList(admins));
89+
if (adminArgs.length > 0 && !(adminArgs.length == 1 && adminArgs[0] == null)) {
90+
admins.addAll(Arrays.asList(adminArgs));
9191
}
9292
}
93-
this.declaringAdmins = declaringAdmins;
93+
this.declaringAdmins = admins;
9494
}
9595

9696
}

spring-amqp/src/main/java/org/springframework/amqp/core/Address.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -137,7 +137,7 @@ public int hashCode() {
137137

138138
@Override
139139
public String toString() {
140-
StringBuilder sb = new StringBuilder(this.exchangeName + "/");
140+
StringBuilder sb = new StringBuilder(this.exchangeName).append('/');
141141
if (StringUtils.hasText(this.routingKey)) {
142142
sb.append(this.routingKey);
143143
}

spring-amqp/src/main/java/org/springframework/amqp/core/BindingBuilder.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -58,8 +58,9 @@ private static Map<String, Object> createMapForKeys(String... keys) {
5858
*/
5959
public static final class DestinationConfigurer {
6060

61-
protected final String name;
62-
protected final DestinationType type;
61+
protected final String name; // NOSONAR
62+
63+
protected final DestinationType type; // NOSONAR
6364

6465
DestinationConfigurer(String name, DestinationType type) {
6566
this.name = name;
@@ -92,9 +93,9 @@ public GenericExchangeRoutingKeyConfigurer to(Exchange exchange) {
9293
*/
9394
public static final class HeadersExchangeMapConfigurer {
9495

95-
protected final DestinationConfigurer destination;
96+
protected final DestinationConfigurer destination; // NOSONAR
9697

97-
protected final HeadersExchange exchange;
98+
protected final HeadersExchange exchange; // NOSONAR
9899

99100
HeadersExchangeMapConfigurer(DestinationConfigurer destination, HeadersExchange exchange) {
100101
this.destination = destination;
@@ -191,9 +192,9 @@ public Binding match() {
191192

192193
private abstract static class AbstractRoutingKeyConfigurer<E extends Exchange> {
193194

194-
protected final DestinationConfigurer destination;
195+
protected final DestinationConfigurer destination; // NOSONAR
195196

196-
protected final String exchange;
197+
protected final String exchange; // NOSONAR
197198

198199
AbstractRoutingKeyConfigurer(DestinationConfigurer destination, String exchange) {
199200
this.destination = destination;
@@ -246,6 +247,7 @@ public GenericArgumentsConfigurer with(Enum<?> routingKeyEnum) {
246247
public static class GenericArgumentsConfigurer {
247248

248249
private final GenericExchangeRoutingKeyConfigurer configurer;
250+
249251
private final String routingKey;
250252

251253
public GenericArgumentsConfigurer(GenericExchangeRoutingKeyConfigurer configurer, String routingKey) {

spring-amqp/src/main/java/org/springframework/amqp/core/MessageBuilder.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2016 the original author or authors.
2+
* Copyright 2014-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -30,6 +30,10 @@
3030
*/
3131
public final class MessageBuilder extends MessageBuilderSupport<Message> {
3232

33+
private static final String MESSAGE_CANNOT_BE_NULL = "'message' cannot be null";
34+
35+
private static final String BODY_CANNOT_BE_NULL = "'body' cannot be null";
36+
3337
private final byte[] body;
3438

3539
/**
@@ -38,7 +42,7 @@ public final class MessageBuilder extends MessageBuilderSupport<Message> {
3842
* @return The builder.
3943
*/
4044
public static MessageBuilder withBody(byte[] body) {
41-
Assert.notNull(body, "'body' cannot be null");
45+
Assert.notNull(body, BODY_CANNOT_BE_NULL);
4246
return new MessageBuilder(body);
4347
}
4448

@@ -48,7 +52,7 @@ public static MessageBuilder withBody(byte[] body) {
4852
* @return The builder.
4953
*/
5054
public static MessageBuilder withClonedBody(byte[] body) {
51-
Assert.notNull(body, "'body' cannot be null");
55+
Assert.notNull(body, BODY_CANNOT_BE_NULL);
5256
return new MessageBuilder(Arrays.copyOf(body, body.length));
5357
}
5458

@@ -63,7 +67,7 @@ public static MessageBuilder withClonedBody(byte[] body) {
6367
* @see Arrays#copyOfRange(byte[], int, int)
6468
*/
6569
public static MessageBuilder withBody(byte[] body, int from, int to) {
66-
Assert.notNull(body, "'body' cannot be null");
70+
Assert.notNull(body, BODY_CANNOT_BE_NULL);
6771
return new MessageBuilder(Arrays.copyOfRange(body, from, to));
6872
}
6973

@@ -74,7 +78,7 @@ public static MessageBuilder withBody(byte[] body, int from, int to) {
7478
* @return The builder.
7579
*/
7680
public static MessageBuilder fromMessage(Message message) {
77-
Assert.notNull(message, "'message' cannot be null");
81+
Assert.notNull(message, MESSAGE_CANNOT_BE_NULL);
7882
return new MessageBuilder(message);
7983
}
8084

@@ -85,9 +89,9 @@ public static MessageBuilder fromMessage(Message message) {
8589
* @return The builder.
8690
*/
8791
public static MessageBuilder fromClonedMessage(Message message) {
88-
Assert.notNull(message, "'message' cannot be null");
92+
Assert.notNull(message, MESSAGE_CANNOT_BE_NULL);
8993
byte[] body = message.getBody();
90-
Assert.notNull(body, "'body' cannot be null");
94+
Assert.notNull(body, BODY_CANNOT_BE_NULL);
9195
return new MessageBuilder(Arrays.copyOf(body, body.length), message.getMessageProperties());
9296
}
9397

spring-amqp/src/main/java/org/springframework/amqp/core/MessageProperties.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -532,7 +532,7 @@ public void setTargetBean(Object targetBean) {
532532
}
533533

534534
@Override
535-
public int hashCode() {
535+
public int hashCode() { // NOSONAR complexity
536536
final int prime = 31;
537537
int result = 1;
538538
result = prime * result + ((this.appId == null) ? 0 : this.appId.hashCode());
@@ -559,7 +559,7 @@ public int hashCode() {
559559
}
560560

561561
@Override
562-
public boolean equals(Object obj) {
562+
public boolean equals(Object obj) { // NOSONAR complexity
563563
if (this == obj) {
564564
return true;
565565
}
@@ -716,7 +716,7 @@ else if (!this.userId.equals(other.userId)) {
716716
}
717717

718718
@Override
719-
public String toString() {
719+
public String toString() { // NOSONAR complexity
720720
return "MessageProperties [headers=" + this.headers
721721
+ (this.timestamp == null ? "" : ", timestamp=" + this.timestamp)
722722
+ (this.messageId == null ? "" : ", messageId=" + this.messageId)

spring-rabbit/src/main/java/org/springframework/amqp/rabbit/annotation/Exchange.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@
3535
@Retention(RetentionPolicy.RUNTIME)
3636
public @interface Exchange {
3737

38+
String TRUE = "true";
39+
40+
String FALSE = "false";
41+
3842
/**
3943
* @return the exchange name.
4044
*/
@@ -60,32 +64,32 @@
6064
/**
6165
* @return false if the exchange is to be declared as non-durable.
6266
*/
63-
String durable() default "true";
67+
String durable() default TRUE;
6468

6569
/**
6670
* @return true if the exchange is to be declared as auto-delete.
6771
*/
68-
String autoDelete() default "false";
72+
String autoDelete() default FALSE;
6973

7074
/**
7175
* @return true if the exchange is to be declared as internal.
7276
* @since 1.6
7377
*/
74-
String internal() default "false";
78+
String internal() default FALSE;
7579

7680
/**
7781
* @return true if the declaration exceptions should be ignored.
7882
* @since 1.6
7983
*/
80-
String ignoreDeclarationExceptions() default "false";
84+
String ignoreDeclarationExceptions() default FALSE;
8185

8286
/**
8387
* @return true if the exchange is to be declared as an
8488
* 'x-delayed-message' exchange. Requires the delayed message exchange
8589
* plugin on the broker.
8690
* @since 1.6.4
8791
*/
88-
String delayed() default "false";
92+
String delayed() default FALSE;
8993

9094
/**
9195
* @return the arguments to apply when declaring this exchange.
@@ -97,7 +101,7 @@
97101
* @return true if the admin(s), if present, should declare this component.
98102
* @since 2.1
99103
*/
100-
String declare() default "true";
104+
String declare() default TRUE;
101105

102106
/**
103107
* Return a list of admin bean names that should declare this component.

spring-rabbit/src/main/java/org/springframework/amqp/rabbit/annotation/RabbitListenerAnnotationBeanPostProcessor.java

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
import org.springframework.core.convert.ConversionService;
7070
import org.springframework.core.convert.support.DefaultConversionService;
7171
import org.springframework.core.env.Environment;
72+
import org.springframework.lang.Nullable;
7273
import org.springframework.messaging.handler.annotation.support.DefaultMessageHandlerMethodFactory;
7374
import org.springframework.messaging.handler.annotation.support.MessageHandlerMethodFactory;
7475
import org.springframework.messaging.handler.invocation.InvocableHandlerMethod;
@@ -130,7 +131,7 @@ public class RabbitListenerAnnotationBeanPostProcessor
130131

131132
private RabbitListenerEndpointRegistry endpointRegistry;
132133

133-
private String containerFactoryBeanName = DEFAULT_RABBIT_LISTENER_CONTAINER_FACTORY_BEAN_NAME;
134+
private String defaultContainerFactoryBeanName = DEFAULT_RABBIT_LISTENER_CONTAINER_FACTORY_BEAN_NAME;
134135

135136
private BeanFactory beanFactory;
136137

@@ -175,7 +176,7 @@ public void setEndpointRegistry(RabbitListenerEndpointRegistry endpointRegistry)
175176
* @param containerFactoryBeanName the {@link RabbitListenerContainerFactory} bean name.
176177
*/
177178
public void setContainerFactoryBeanName(String containerFactoryBeanName) {
178-
this.containerFactoryBeanName = containerFactoryBeanName;
179+
this.defaultContainerFactoryBeanName = containerFactoryBeanName;
179180
}
180181

181182
/**
@@ -242,8 +243,8 @@ public void afterSingletonsInstantiated() {
242243
this.registrar.setEndpointRegistry(this.endpointRegistry);
243244
}
244245

245-
if (this.containerFactoryBeanName != null) {
246-
this.registrar.setContainerFactoryBeanName(this.containerFactoryBeanName);
246+
if (this.defaultContainerFactoryBeanName != null) {
247+
this.registrar.setContainerFactoryBeanName(this.defaultContainerFactoryBeanName);
247248
}
248249

249250
// Set the custom handler method factory once resolved by the configurer
@@ -367,7 +368,8 @@ protected void processAmqpListener(RabbitListener rabbitListener, Method method,
367368
processListener(endpoint, rabbitListener, bean, methodToUse, beanName);
368369
}
369370

370-
private Method checkProxy(Method method, Object bean) {
371+
private Method checkProxy(Method methodArg, Object bean) {
372+
Method method = methodArg;
371373
if (AopUtils.isJdkDynamicProxy(bean)) {
372374
try {
373375
// Found a @RabbitListener method on the target class for this JDK proxy ->
@@ -389,10 +391,10 @@ private Method checkProxy(Method method, Object bean) {
389391
catch (NoSuchMethodException ex) {
390392
throw new IllegalStateException(String.format(
391393
"@RabbitListener method '%s' found on bean target class '%s', " +
392-
"but not found in any interface(s) for bean JDK proxy. Either " +
393-
"pull the method up to an interface or switch to subclass (CGLIB) " +
394-
"proxies by setting proxy-target-class/proxyTargetClass " +
395-
"attribute to 'true'", method.getName(), method.getDeclaringClass().getSimpleName()));
394+
"but not found in any interface(s) for a bean JDK proxy. Either " +
395+
"pull the method up to an interface or switch to subclass (CGLIB) " +
396+
"proxies by setting proxy-target-class/proxyTargetClass " +
397+
"attribute to 'true'", method.getName(), method.getDeclaringClass().getSimpleName()), ex);
396398
}
397399
}
398400
return method;
@@ -445,6 +447,13 @@ else if (errorHandler instanceof String) {
445447
}
446448
}
447449

450+
resolveAdmin(endpoint, rabbitListener, adminTarget);
451+
RabbitListenerContainerFactory<?> factory = resolveContainerFactory(rabbitListener, adminTarget, beanName);
452+
453+
this.registrar.registerEndpoint(endpoint, factory);
454+
}
455+
456+
private void resolveAdmin(MethodRabbitListenerEndpoint endpoint, RabbitListener rabbitListener, Object adminTarget) {
448457
String rabbitAdmin = resolve(rabbitListener.admin());
449458
if (StringUtils.hasText(rabbitAdmin)) {
450459
Assert.state(this.beanFactory != null, "BeanFactory must be set to resolve RabbitAdmin by bean name");
@@ -457,8 +466,11 @@ else if (errorHandler instanceof String) {
457466
rabbitAdmin + "' was found in the application context", ex);
458467
}
459468
}
469+
}
460470

461-
471+
@Nullable
472+
private RabbitListenerContainerFactory<?> resolveContainerFactory(RabbitListener rabbitListener, Object adminTarget,
473+
String beanName) {
462474
RabbitListenerContainerFactory<?> factory = null;
463475
String containerFactoryBeanName = resolve(rabbitListener.containerFactory());
464476
if (StringUtils.hasText(containerFactoryBeanName)) {
@@ -472,8 +484,7 @@ else if (errorHandler instanceof String) {
472484
containerFactoryBeanName + "' was found in the application context", ex);
473485
}
474486
}
475-
476-
this.registrar.registerEndpoint(endpoint, factory);
487+
return factory;
477488
}
478489

479490
private String getEndpointId(RabbitListener rabbitListener) {
@@ -617,6 +628,10 @@ private void declareExchangeAndBinding(QueueBinding binding, String queueName) {
617628

618629
((ConfigurableBeanFactory) this.beanFactory)
619630
.registerSingleton(exchangeName + ++this.increment, exchange);
631+
registerBindings(binding, queueName, exchangeName, exchangeType);
632+
}
633+
634+
private void registerBindings(QueueBinding binding, String queueName, String exchangeName, String exchangeType) {
620635
final String[] routingKeys;
621636
if (exchangeType.equals(ExchangeTypes.FANOUT) || binding.key().length == 0) {
622637
routingKeys = new String[] { "" };
@@ -826,17 +841,17 @@ private static class TypeMetadata {
826841
/**
827842
* Methods annotated with {@link RabbitListener}.
828843
*/
829-
final ListenerMethod[] listenerMethods;
844+
final ListenerMethod[] listenerMethods; // NOSONAR
830845

831846
/**
832847
* Methods annotated with {@link RabbitHandler}.
833848
*/
834-
final Method[] handlerMethods;
849+
final Method[] handlerMethods; // NOSONAR
835850

836851
/**
837852
* Class level {@link RabbitListener} annotations.
838853
*/
839-
final RabbitListener[] classAnnotations;
854+
final RabbitListener[] classAnnotations; // NOSONAR
840855

841856
static final TypeMetadata EMPTY = new TypeMetadata();
842857

@@ -859,9 +874,9 @@ private TypeMetadata() {
859874
*/
860875
private static class ListenerMethod {
861876

862-
final Method method;
877+
final Method method; // NOSONAR
863878

864-
final RabbitListener[] annotations;
879+
final RabbitListener[] annotations; // NOSONAR
865880

866881
ListenerMethod(Method method, RabbitListener[] annotations) { // NOSONAR
867882
this.method = method;

0 commit comments

Comments
 (0)