Skip to content

Commit 3a846ba

Browse files
committed
Fix some Sonar smells
1 parent cc0c2fd commit 3a846ba

17 files changed

+145
-122
lines changed

spring-integration-core/src/main/java/org/springframework/integration/expression/ReloadableResourceBundleExpressionSource.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 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.
@@ -68,12 +68,12 @@
6868
*/
6969
public class ReloadableResourceBundleExpressionSource implements ExpressionSource, ResourceLoaderAware {
7070

71+
private static final Log LOGGER = LogFactory.getLog(ReloadableResourceBundleExpressionSource.class);
72+
7173
private static final String PROPERTIES_SUFFIX = ".properties";
7274

7375
private static final String XML_SUFFIX = ".xml";
7476

75-
private static final Log LOGGER = LogFactory.getLog(ReloadableResourceBundleExpressionSource.class);
76-
7777

7878
/**
7979
* Cache to hold filename lists per Locale
@@ -211,7 +211,7 @@ public void setFallbackToSystemLocale(boolean fallbackToSystemLocale) {
211211
* @param cacheSeconds The cache seconds.
212212
*/
213213
public void setCacheSeconds(int cacheSeconds) {
214-
this.cacheMillis = (cacheSeconds * 1000);
214+
this.cacheMillis = (cacheSeconds * 1000); // NOSONAR
215215
}
216216

217217
/**
@@ -324,8 +324,7 @@ private List<String> calculateAllFilenames(String basename, Locale locale) {
324324
return filenames;
325325
}
326326
}
327-
List<String> filenames = new ArrayList<>(7);
328-
filenames.addAll(calculateFilenamesForLocale(basename, locale));
327+
List<String> filenames = new ArrayList<>(calculateFilenamesForLocale(basename, locale));
329328
if (this.fallbackToSystemLocale && !locale.equals(Locale.getDefault())) {
330329
List<String> fallbackFilenames = calculateFilenamesForLocale(basename, Locale.getDefault());
331330
for (String fallbackFilename : fallbackFilenames) {
@@ -359,7 +358,7 @@ private List<String> calculateAllFilenames(String basename, Locale locale) {
359358
* @return the List of filenames to check
360359
*/
361360
private List<String> calculateFilenamesForLocale(String basename, Locale locale) {
362-
List<String> result = new ArrayList<>(3);
361+
List<String> result = new ArrayList<>();
363362
String language = locale.getLanguage();
364363
String country = locale.getCountry();
365364
String variant = locale.getVariant();

spring-integration-core/src/main/java/org/springframework/integration/handler/advice/IdempotentReceiverInterceptor.java

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2019 the original author or authors.
2+
* Copyright 2014-2020 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.
@@ -170,11 +170,8 @@ protected Object doInvoke(MethodInvocation invocation, Message<?> message) throw
170170
}
171171

172172
private MessageChannel obtainDiscardChannel() {
173-
if (this.discardChannel == null) {
174-
if (this.discardChannelName != null) {
175-
this.discardChannel = getChannelResolver()
176-
.resolveDestination(this.discardChannelName);
177-
}
173+
if (this.discardChannel == null && this.discardChannelName != null) {
174+
this.discardChannel = getChannelResolver().resolveDestination(this.discardChannelName);
178175
}
179176
return this.discardChannel;
180177
}

spring-integration-core/src/main/java/org/springframework/integration/handler/advice/RequestHandlerCircuitBreakerAdvice.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 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.
@@ -36,9 +36,19 @@
3636
*/
3737
public class RequestHandlerCircuitBreakerAdvice extends AbstractRequestHandlerAdvice {
3838

39-
private volatile int threshold = 5;
39+
/**
40+
* A default failures threshold as {@value DEFAULT_THRESHOLD}.
41+
*/
42+
public static final int DEFAULT_THRESHOLD = 5;
4043

41-
private volatile long halfOpenAfter = 1000;
44+
/**
45+
* A half-open duration as {@value DEFAULT_HALF_OPEN_AFTER} .
46+
*/
47+
public static final int DEFAULT_HALF_OPEN_AFTER = 1000;
48+
49+
private int threshold = DEFAULT_THRESHOLD;
50+
51+
private long halfOpenAfter = DEFAULT_HALF_OPEN_AFTER;
4252

4353
private final ConcurrentMap<Object, AdvisedMetadata> metadataMap = new ConcurrentHashMap<>();
4454

@@ -101,6 +111,7 @@ private void setLastFailure(long lastFailure) {
101111
private AtomicInteger getFailures() {
102112
return this.failures;
103113
}
114+
104115
}
105116

106117
/**

spring-integration-core/src/main/java/org/springframework/integration/handler/advice/RequestHandlerRetryAdvice.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 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.
@@ -37,17 +37,18 @@
3737
*
3838
* @author Gary Russell
3939
* @author Artem Bilan
40+
*
4041
* @since 2.2
4142
*/
4243
public class RequestHandlerRetryAdvice extends AbstractRequestHandlerAdvice
4344
implements RetryListener {
4445

46+
private static final ThreadLocal<Message<?>> MESSAGE_HOLDER = new ThreadLocal<>();
47+
4548
private RetryTemplate retryTemplate = new RetryTemplate();
4649

4750
private RecoveryCallback<Object> recoveryCallback;
4851

49-
private static final ThreadLocal<Message<?>> messageHolder = new ThreadLocal<Message<?>>();
50-
5152
// Stateless unless a state generator is provided
5253
private volatile RetryStateGenerator retryStateGenerator = message -> null;
5354

@@ -78,9 +79,8 @@ protected void onInit() {
7879

7980
@Override
8081
protected Object doInvoke(final ExecutionCallback callback, Object target, final Message<?> message) {
81-
RetryState retryState = null;
82-
retryState = this.retryStateGenerator.determineRetryState(message);
83-
messageHolder.set(message);
82+
RetryState retryState = this.retryStateGenerator.determineRetryState(message);
83+
MESSAGE_HOLDER.set(message);
8484

8585
try {
8686
return this.retryTemplate.execute(context -> callback.cloneAndExecute(), this.recoveryCallback, retryState);
@@ -98,13 +98,13 @@ protected Object doInvoke(final ExecutionCallback callback, Object target, final
9898
throw new ThrowableHolderException(e);
9999
}
100100
finally {
101-
messageHolder.remove();
101+
MESSAGE_HOLDER.remove();
102102
}
103103
}
104104

105105
@Override
106106
public <T, E extends Throwable> boolean open(RetryContext context, RetryCallback<T, E> callback) {
107-
context.setAttribute(ErrorMessageUtils.FAILED_MESSAGE_CONTEXT_KEY, messageHolder.get());
107+
context.setAttribute(ErrorMessageUtils.FAILED_MESSAGE_CONTEXT_KEY, MESSAGE_HOLDER.get());
108108
return true;
109109
}
110110

spring-integration-core/src/main/java/org/springframework/integration/mapping/AbstractHeaderMapper.java

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,6 @@ public void setRequestHeaderNames(String... requestHeaderNames) {
128128
* Provide the header names that should be mapped to a response
129129
* from a {@link MessageHeaders}.
130130
* <p>The values can also contain simple wildcard patterns (e.g. "foo*" or "*foo") to be matched.
131-
*
132131
* @param replyHeaderNames The reply header names.
133132
*/
134133
public void setReplyHeaderNames(String... replyHeaderNames) {
@@ -141,7 +140,7 @@ public void setReplyHeaderNames(String... replyHeaderNames) {
141140
* standard header prefix.
142141
* @param standardHeaderPrefix the prefix for standard headers.
143142
* @param headerNames the collection of header names to map.
144-
* @return the deafault {@link HeaderMatcher} instance.
143+
* @return the default {@link HeaderMatcher} instance.
145144
*/
146145
protected HeaderMatcher createDefaultHeaderMatcher(String standardHeaderPrefix, Collection<String> headerNames) {
147146
return new ContentBasedHeaderMatcher(true, headerNames);
@@ -401,6 +400,7 @@ protected void populateStandardHeaders(@Nullable Map<String, Object> allHeaders,
401400

402401
/**
403402
* Strategy interface to determine if a given header name matches.
403+
*
404404
* @since 4.1
405405
*/
406406
@FunctionalInterface
@@ -427,11 +427,12 @@ default boolean isNegated() {
427427
* A content-based {@link HeaderMatcher} that matches if the specified
428428
* header is contained within a list of candidates. The case of the
429429
* header does not matter.
430+
*
430431
* @since 4.1
431432
*/
432433
protected static class ContentBasedHeaderMatcher implements HeaderMatcher {
433434

434-
private static final Log logger = LogFactory.getLog(HeaderMatcher.class);
435+
private static final Log LOGGER = LogFactory.getLog(HeaderMatcher.class);
435436

436437
private final boolean match;
437438

@@ -446,13 +447,13 @@ public ContentBasedHeaderMatcher(boolean match, Collection<String> content) {
446447
@Override
447448
public boolean matchHeader(String headerName) {
448449
boolean result = (this.match == containsIgnoreCase(headerName));
449-
if (result && logger.isDebugEnabled()) {
450+
if (result && LOGGER.isDebugEnabled()) {
450451
StringBuilder message = new StringBuilder("headerName=[{0}] WILL be mapped, ");
451452
if (!this.match) {
452453
message.append("not ");
453454
}
454455
message.append("found in {1}");
455-
logger.debug(MessageFormat.format(message.toString(), headerName, this.content));
456+
LOGGER.debug(MessageFormat.format(message.toString(), headerName, this.content));
456457
}
457458
return result;
458459
}
@@ -471,12 +472,14 @@ private boolean containsIgnoreCase(String name) {
471472
/**
472473
* A pattern-based {@link HeaderMatcher} that matches if the specified
473474
* header matches one of the specified simple patterns.
474-
* @see org.springframework.util.PatternMatchUtils#simpleMatch(String, String)
475+
*
475476
* @since 4.1
477+
*
478+
* @see org.springframework.util.PatternMatchUtils#simpleMatch(String, String)
476479
*/
477480
protected static class PatternBasedHeaderMatcher implements HeaderMatcher {
478481

479-
private static final Log logger = LogFactory.getLog(HeaderMatcher.class);
482+
private static final Log LOGGER = LogFactory.getLog(HeaderMatcher.class);
480483

481484
private final Collection<String> patterns = new ArrayList<>();
482485

@@ -493,8 +496,8 @@ public boolean matchHeader(String headerName) {
493496
String header = headerName.toLowerCase();
494497
for (String pattern : this.patterns) {
495498
if (PatternMatchUtils.simpleMatch(pattern, header)) {
496-
if (logger.isDebugEnabled()) {
497-
logger.debug(MessageFormat.format(
499+
if (LOGGER.isDebugEnabled()) {
500+
LOGGER.debug(MessageFormat.format(
498501
"headerName=[{0}] WILL be mapped, matched pattern={1}", headerName, pattern));
499502
}
500503
return true;
@@ -509,12 +512,14 @@ public boolean matchHeader(String headerName) {
509512
* A pattern-based {@link HeaderMatcher} that matches if the specified
510513
* header matches the specified simple pattern.
511514
* <p> The {@code negate == true} state indicates if the matching should be treated as "not matched".
512-
* @see org.springframework.util.PatternMatchUtils#simpleMatch(String, String)
515+
*
513516
* @since 4.3
517+
*
518+
* @see org.springframework.util.PatternMatchUtils#simpleMatch(String, String)
514519
*/
515520
protected static class SinglePatternBasedHeaderMatcher implements HeaderMatcher {
516521

517-
private static final Log logger = LogFactory.getLog(HeaderMatcher.class);
522+
private static final Log LOGGER = LogFactory.getLog(HeaderMatcher.class);
518523

519524
private final String pattern;
520525

@@ -534,8 +539,8 @@ public SinglePatternBasedHeaderMatcher(String pattern, boolean negate) {
534539
public boolean matchHeader(String headerName) {
535540
String header = headerName.toLowerCase();
536541
if (PatternMatchUtils.simpleMatch(this.pattern, header)) {
537-
if (logger.isDebugEnabled()) {
538-
logger.debug(MessageFormat.format(
542+
if (LOGGER.isDebugEnabled()) {
543+
LOGGER.debug(MessageFormat.format(
539544
"headerName=[{0}] WILL be mapped, matched pattern={1}", headerName, this.pattern));
540545
}
541546
return true;
@@ -553,11 +558,12 @@ public boolean isNegated() {
553558
/**
554559
* A prefix-based {@link HeaderMatcher} that matches if the specified
555560
* header starts with a configurable prefix.
561+
*
556562
* @since 4.1
557563
*/
558564
protected static class PrefixBasedMatcher implements HeaderMatcher {
559565

560-
private static final Log logger = LogFactory.getLog(HeaderMatcher.class);
566+
private static final Log LOGGER = LogFactory.getLog(HeaderMatcher.class);
561567

562568
private final boolean match;
563569

@@ -571,13 +577,13 @@ public PrefixBasedMatcher(boolean match, String prefix) {
571577
@Override
572578
public boolean matchHeader(String headerName) {
573579
boolean result = (this.match == headerName.startsWith(this.prefix));
574-
if (result && logger.isDebugEnabled()) {
580+
if (result && LOGGER.isDebugEnabled()) {
575581
StringBuilder message = new StringBuilder("headerName=[{0}] WILL be mapped, ");
576582
if (!this.match) {
577583
message.append("does not ");
578584
}
579585
message.append("start with [{1}]");
580-
logger.debug(MessageFormat.format(message.toString(), headerName, this.prefix));
586+
LOGGER.debug(MessageFormat.format(message.toString(), headerName, this.prefix));
581587
}
582588
return result;
583589
}
@@ -587,11 +593,12 @@ public boolean matchHeader(String headerName) {
587593
/**
588594
* A composite {@link HeaderMatcher} that matches if one of provided
589595
* {@link HeaderMatcher}s matches to the {@code headerName}.
596+
*
590597
* @since 4.1
591598
*/
592599
protected static class CompositeHeaderMatcher implements HeaderMatcher {
593600

594-
private static final Log logger = LogFactory.getLog(HeaderMatcher.class);
601+
private static final Log LOGGER = LogFactory.getLog(HeaderMatcher.class);
595602

596603
private final Collection<HeaderMatcher> matchers;
597604

@@ -613,8 +620,8 @@ public boolean matchHeader(String headerName) {
613620
return true;
614621
}
615622
}
616-
if (logger.isDebugEnabled()) {
617-
logger.debug(MessageFormat.format("headerName=[{0}] WILL NOT be mapped", headerName));
623+
if (LOGGER.isDebugEnabled()) {
624+
LOGGER.debug(MessageFormat.format("headerName=[{0}] WILL NOT be mapped", headerName));
618625
}
619626
return false;
620627
}

spring-integration-core/src/main/java/org/springframework/integration/selector/MetadataStoreSelector.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@ public boolean accept(Message<?> message) {
114114
? this.valueStrategy.processMessage(message)
115115
: (timestamp == null ? "0" : Long.toString(timestamp));
116116

117-
if (this.compareValues == null) {
117+
BiPredicate<String, String> predicate = this.compareValues;
118+
if (predicate == null) {
118119
return this.metadataStore.putIfAbsent(key, value) == null;
119120
}
120121
else {
@@ -123,7 +124,7 @@ public boolean accept(Message<?> message) {
123124
if (oldValue == null) {
124125
return this.metadataStore.putIfAbsent(key, value) == null;
125126
}
126-
if (this.compareValues.test(oldValue, value)) { // NOSONAR (null dereference)
127+
if (predicate.test(oldValue, value)) {
127128
return this.metadataStore.replace(key, oldValue, value);
128129
}
129130
return false;

spring-integration-core/src/main/java/org/springframework/integration/support/AbstractIntegrationMessageBuilder.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2014-2019 the original author or authors.
2+
* Copyright 2014-2020 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.
@@ -46,7 +46,7 @@ public AbstractIntegrationMessageBuilder<T> setExpirationDate(Long expirationDat
4646
return setHeader(IntegrationMessageHeaderAccessor.EXPIRATION_DATE, expirationDate);
4747
}
4848

49-
public AbstractIntegrationMessageBuilder<T> setExpirationDate(Date expirationDate) {
49+
public AbstractIntegrationMessageBuilder<T> setExpirationDate(@Nullable Date expirationDate) {
5050
if (expirationDate != null) {
5151
return setHeader(IntegrationMessageHeaderAccessor.EXPIRATION_DATE, expirationDate.getTime());
5252
}
@@ -62,7 +62,7 @@ public AbstractIntegrationMessageBuilder<T> setCorrelationId(Object correlationI
6262
public AbstractIntegrationMessageBuilder<T> pushSequenceDetails(Object correlationId, int sequenceNumber,
6363
int sequenceSize) {
6464

65-
Object incomingCorrelationId = this.getCorrelationId();
65+
Object incomingCorrelationId = getCorrelationId();
6666
List<List<Object>> incomingSequenceDetails = getSequenceDetails();
6767
if (incomingCorrelationId != null) {
6868
if (incomingSequenceDetails == null) {
@@ -92,8 +92,8 @@ public AbstractIntegrationMessageBuilder<T> popSequenceDetails() {
9292
incomingSequenceDetails = new ArrayList<>(incomingSequenceDetails);
9393
}
9494
List<Object> sequenceDetails = incomingSequenceDetails.remove(incomingSequenceDetails.size() - 1);
95-
Assert.state(sequenceDetails.size() == 3, "Wrong sequence details (not created by MessageBuilder?): "
96-
+ sequenceDetails);
95+
Assert.state(sequenceDetails.size() == 3, // NOSONAR
96+
() -> "Wrong sequence details (not created by MessageBuilder?): " + sequenceDetails);
9797
setCorrelationId(sequenceDetails.get(0));
9898
Integer sequenceNumber = (Integer) sequenceDetails.get(1);
9999
Integer sequenceSize = (Integer) sequenceDetails.get(2);
@@ -166,10 +166,13 @@ public AbstractIntegrationMessageBuilder<T> filterAndCopyHeadersIfAbsent(Map<Str
166166
@Nullable
167167
protected abstract List<List<Object>> getSequenceDetails();
168168

169+
@Nullable
169170
protected abstract Object getCorrelationId();
170171

172+
@Nullable
171173
protected abstract Object getSequenceNumber();
172174

175+
@Nullable
173176
protected abstract Object getSequenceSize();
174177

175178
public abstract T getPayload();

0 commit comments

Comments
 (0)