Skip to content

Commit 19b9944

Browse files
garyrussellartembilan
authored andcommitted
Sonar fixes
* use equals * inner assignment * local before return * unnecessary locals * catch and throw * uninstantiable with no statics * implement `Serializable` in `Comparator` to enable `TreeMap` serialization * exceptions as flow control
1 parent 130e1bb commit 19b9944

File tree

31 files changed

+98
-87
lines changed

31 files changed

+98
-87
lines changed

spring-integration-core/src/main/java/org/springframework/integration/aggregator/CorrelatingMessageBarrier.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ private Object getLock(Object correlationKey) {
108108
}
109109

110110

111+
@SuppressWarnings("unchecked")
111112
@Override
112113
public Message<Object> receive() {
113114
for (Object key : this.correlationLocks.keySet()) {
@@ -130,9 +131,7 @@ public Message<Object> receive() {
130131
else {
131132
remove(key);
132133
}
133-
@SuppressWarnings("unchecked")
134-
Message<Object> result = (Message<Object>) nextMessage;
135-
return result;
134+
return (Message<Object>) nextMessage;
136135
}
137136
}
138137
}

spring-integration-core/src/main/java/org/springframework/integration/aggregator/MessageSequenceComparator.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.integration.aggregator;
1818

19+
import java.io.Serializable;
1920
import java.util.Comparator;
2021

2122
import org.springframework.integration.IntegrationMessageHeaderAccessor;
@@ -25,8 +26,10 @@
2526
* @author Mark Fisher
2627
* @author Dave Syer
2728
* @author Artem Bilan
29+
* @author Gary Russell
2830
*/
29-
public class MessageSequenceComparator implements Comparator<Message<?>> {
31+
@SuppressWarnings("serial")
32+
public class MessageSequenceComparator implements Comparator<Message<?>>, Serializable {
3033

3134
@Override
3235
public int compare(Message<?> o1, Message<?> o2) {

spring-integration-core/src/main/java/org/springframework/integration/aop/PublisherAnnotationAdvisor.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ public boolean matches(Method method, Class<?> targetClass) {
193193
}
194194
// The method may be on an interface, so let's check on the target class as well.
195195
Method specificMethod = AopUtils.getMostSpecificMethod(method, targetClass);
196-
return (specificMethod != method &&
196+
return (!specificMethod.equals(method) &&
197197
(AnnotationUtils.getAnnotation(specificMethod, this.annotationType) != null));
198198
}
199199

spring-integration-core/src/main/java/org/springframework/integration/channel/MessageChannelReactiveUtils.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ public void subscribe(Subscriber<? super Message<T>> subscriber) {
106106
.<Message<T>>create(sink ->
107107
sink.onRequest(n -> {
108108
Message<?> m;
109-
while (!sink.isCancelled() && n-- > 0 && (m = this.channel.receive()) != null) {
109+
while (!sink.isCancelled() && n-- > 0
110+
&& (m = this.channel.receive()) != null) { // NOSONAR
110111
sink.next((Message<T>) m);
111112
}
112113
}),

spring-integration-core/src/main/java/org/springframework/integration/config/FixedSubscriberChannelBeanFactoryPostProcessor.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public final class FixedSubscriberChannelBeanFactoryPostProcessor implements Bea
3939

4040
private final Map<String, String> candidateFixedChannelHandlerMap;
4141

42-
private FixedSubscriberChannelBeanFactoryPostProcessor(Map<String, String> candidateHandlers) {
42+
FixedSubscriberChannelBeanFactoryPostProcessor(Map<String, String> candidateHandlers) {
4343
this.candidateFixedChannelHandlerMap = candidateHandlers;
4444
}
4545

spring-integration-core/src/main/java/org/springframework/integration/config/annotation/AbstractMethodAnnotationPostProcessor.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ public Object postProcess(Object bean, String beanName, Method method, List<Anno
158158
orderable(method, handler);
159159
producerOrRouter(annotations, handler);
160160

161-
if (handler != sourceHandler) {
161+
if (!handler.equals(sourceHandler)) {
162162
String handlerBeanName = generateHandlerBeanName(beanName, method);
163163
if (handler instanceof ReplyProducingMessageHandlerWrapper
164164
&& StringUtils.hasText(MessagingAnnotationUtils.endpointIdValue(method))) {

spring-integration-core/src/main/java/org/springframework/integration/config/xml/PointToPointChannelParser.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ protected BeanDefinitionBuilder buildBeanDefinition(Element element, ParserConte
5252

5353
// configure a queue-based channel if any queue sub-element is defined
5454
String channel = element.getAttribute(ID_ATTRIBUTE);
55-
if ((queueElement = DomUtils.getChildElementByTagName(element, "queue")) != null) {
55+
if ((queueElement = DomUtils.getChildElementByTagName(element, "queue")) != null) { // NOSONAR inner assignment
5656
builder = BeanDefinitionBuilder.genericBeanDefinition(QueueChannel.class);
5757
boolean hasStoreRef = this.parseStoreRef(builder, queueElement, channel, false);
5858
boolean hasQueueRef = this.parseQueueRef(builder, queueElement);
@@ -76,7 +76,7 @@ protected BeanDefinitionBuilder buildBeanDefinition(Element element, ParserConte
7676
element);
7777
}
7878
}
79-
else if ((queueElement = DomUtils.getChildElementByTagName(element, "priority-queue")) != null) {
79+
else if ((queueElement = DomUtils.getChildElementByTagName(element, "priority-queue")) != null) { // NOSONAR
8080
builder = BeanDefinitionBuilder.genericBeanDefinition(PriorityChannel.class);
8181
boolean hasCapacity = this.parseQueueCapacity(builder, queueElement);
8282
String comparatorRef = queueElement.getAttribute("comparator");
@@ -97,7 +97,7 @@ else if ((queueElement = DomUtils.getChildElementByTagName(element, "priority-qu
9797
}
9898

9999
}
100-
else if ((queueElement = DomUtils.getChildElementByTagName(element, "rendezvous-queue")) != null) {
100+
else if ((queueElement = DomUtils.getChildElementByTagName(element, "rendezvous-queue")) != null) { // NOSONAR
101101
builder = BeanDefinitionBuilder.genericBeanDefinition(RendezvousChannel.class);
102102
}
103103

spring-integration-core/src/main/java/org/springframework/integration/config/xml/SelectorChainParser.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
*
3636
* @author Mark Fisher
3737
* @author Iwein Fuld
38+
* @author Gary Russell
3839
*/
3940
public class SelectorChainParser extends AbstractSingleBeanDefinitionParser {
4041

@@ -43,6 +44,7 @@ protected String getBeanClassName(Element element) {
4344
return MessageSelectorChain.class.getName();
4445
}
4546

47+
@Override
4648
public void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
4749
if (!StringUtils.hasText(element.getAttribute("id"))) {
4850
parserContext.getReaderContext().error("id is required", element);
@@ -82,8 +84,7 @@ private RuntimeBeanReference buildSelectorChain(ParserContext parserContext, Nod
8284
this.parseSelectorChain(nestedBuilder, (Element) child, parserContext);
8385
String nestedBeanName = BeanDefinitionReaderUtils.registerWithGeneratedName(nestedBuilder.getBeanDefinition(),
8486
parserContext.getRegistry());
85-
RuntimeBeanReference built = new RuntimeBeanReference(nestedBeanName);
86-
return built;
87+
return new RuntimeBeanReference(nestedBeanName);
8788
}
8889

8990
private RuntimeBeanReference buildMethodInvokingSelector(ParserContext parserContext, String ref, String method) {

spring-integration-core/src/main/java/org/springframework/integration/dsl/context/StandardIntegrationFlowContext.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public final class StandardIntegrationFlowContext implements IntegrationFlowCont
5959

6060
private BeanDefinitionRegistry beanDefinitionRegistry;
6161

62-
private StandardIntegrationFlowContext() {
62+
StandardIntegrationFlowContext() {
6363
}
6464

6565
@Override

spring-integration-core/src/main/java/org/springframework/integration/router/RecipientListRouter.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -137,15 +137,15 @@ public void addRecipient(String channelName, String selectorExpression) {
137137
addRecipient(channelName, selectorExpression, this.recipients);
138138
}
139139

140-
private void addRecipient(String channelName, String selectorExpression, Queue<Recipient> recipients) {
140+
private void addRecipient(String channelName, String selectorExpression, Queue<Recipient> recipientsToAdd) {
141141
Assert.hasText(channelName, "'channelName' must not be empty.");
142142
Assert.hasText(selectorExpression, "'selectorExpression' must not be empty.");
143143
ExpressionEvaluatingSelector expressionEvaluatingSelector =
144144
new ExpressionEvaluatingSelector(selectorExpression);
145145
expressionEvaluatingSelector.setBeanFactory(getBeanFactory());
146146
Recipient recipient = new Recipient(channelName, expressionEvaluatingSelector);
147147
setupRecipient(recipient);
148-
recipients.add(recipient);
148+
recipientsToAdd.add(recipient);
149149
}
150150

151151
@Override
@@ -158,11 +158,11 @@ public void addRecipient(String channelName, MessageSelector selector) {
158158
addRecipient(channelName, selector, this.recipients);
159159
}
160160

161-
private void addRecipient(String channelName, MessageSelector selector, Queue<Recipient> recipients) {
161+
private void addRecipient(String channelName, MessageSelector selector, Queue<Recipient> recipientsToAdd) {
162162
Assert.hasText(channelName, "'channelName' must not be empty.");
163163
Recipient recipient = new Recipient(channelName, selector);
164164
setupRecipient(recipient);
165-
recipients.add(recipient);
165+
recipientsToAdd.add(recipient);
166166
}
167167

168168
public void addRecipient(MessageChannel channel) {
@@ -208,9 +208,9 @@ public int removeRecipient(String channelName, String selectorExpression) {
208208
Recipient next = it.next();
209209
MessageSelector selector = next.getSelector();
210210
MessageChannel channel = next.getChannel();
211-
if (selector instanceof ExpressionEvaluatingSelector &&
212-
channel == targetChannel &&
213-
((ExpressionEvaluatingSelector) selector).getExpressionString().equals(selectorExpression)) {
211+
if (selector instanceof ExpressionEvaluatingSelector
212+
&& channel.equals(targetChannel)
213+
&& ((ExpressionEvaluatingSelector) selector).getExpressionString().equals(selectorExpression)) {
214214
it.remove();
215215
counter++;
216216
}

spring-integration-core/src/main/java/org/springframework/integration/transformer/AbstractTransformer.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public final Message<?> transform(Message<?> message) {
3737
return (result instanceof Message) ? (Message<?>) result
3838
: this.getMessageBuilderFactory().withPayload(result).copyHeaders(message.getHeaders()).build();
3939
}
40-
catch (MessageTransformationException e) {
40+
catch (MessageTransformationException e) { // NOSONAR - catch and throw
4141
throw e;
4242
}
4343
catch (Exception e) {

spring-integration-feed/src/main/java/org/springframework/integration/feed/inbound/FeedEntryMessageSource.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.integration.feed.inbound;
1818

1919
import java.io.Reader;
20+
import java.io.Serializable;
2021
import java.net.URL;
2122
import java.util.Collections;
2223
import java.util.Comparator;
@@ -254,7 +255,8 @@ private static Date getLastModifiedDate(SyndEntry entry) {
254255
}
255256

256257

257-
private static final class SyndEntryPublishedDateComparator implements Comparator<SyndEntry> {
258+
@SuppressWarnings("serial")
259+
private static final class SyndEntryPublishedDateComparator implements Comparator<SyndEntry>, Serializable {
258260

259261
SyndEntryPublishedDateComparator() {
260262
super();

spring-integration-file/src/main/java/org/springframework/integration/file/remote/gateway/AbstractRemoteFileOutboundGateway.java

+10-11
Original file line numberDiff line numberDiff line change
@@ -920,20 +920,20 @@ protected File get(Message<?> message, Session<F> session, String remoteDir, Str
920920
else {
921921
outputStream = new BufferedOutputStream(new FileOutputStream(tempFile));
922922
}
923-
if (replacing) {
924-
if (!localFile.delete() && this.logger.isWarnEnabled()) {
925-
this.logger.warn("Failed to delete " + localFile);
926-
}
923+
if (replacing && !localFile.delete() && this.logger.isWarnEnabled()) {
924+
this.logger.warn("Failed to delete " + localFile);
927925
}
928926
try {
929927
session.read(remoteFilePath, outputStream);
930928
}
931929
catch (Exception e) {
932930
/* Some operation systems acquire exclusive file-lock during file processing
933-
and the file can't be deleted without closing streams before.
931+
and the file can't be deleted without closing streams before.
934932
*/
935933
outputStream.close();
936-
tempFile.delete();
934+
if (!tempFile.delete() && this.logger.isWarnEnabled()) {
935+
this.logger.warn("Failed to delete tempFile " + tempFile);
936+
}
937937

938938
if (e instanceof RuntimeException) {
939939
throw (RuntimeException) e;
@@ -953,11 +953,10 @@ protected File get(Message<?> message, Session<F> session, String remoteDir, Str
953953
if (!appending && !tempFile.renameTo(localFile)) {
954954
throw new MessagingException("Failed to rename local file");
955955
}
956-
if (this.options.contains(Option.PRESERVE_TIMESTAMP)
957-
|| FileExistsMode.REPLACE_IF_MODIFIED.equals(existsMode)) {
958-
if (!localFile.setLastModified(getModified(fileInfo)) && this.logger.isWarnEnabled()) {
959-
logger.warn("Failed to set lastModified on " + localFile);
960-
}
956+
if ((this.options.contains(Option.PRESERVE_TIMESTAMP)
957+
|| FileExistsMode.REPLACE_IF_MODIFIED.equals(existsMode))
958+
&& (!localFile.setLastModified(getModified(fileInfo)) && this.logger.isWarnEnabled())) {
959+
logger.warn("Failed to set lastModified on " + localFile);
961960
}
962961
if (this.options.contains(Option.DELETE)) {
963962
boolean result = session.remove(remoteFilePath);

spring-integration-file/src/main/java/org/springframework/integration/file/remote/synchronizer/AbstractInboundFileSynchronizingMessageSource.java

+24-20
Original file line numberDiff line numberDiff line change
@@ -190,39 +190,43 @@ protected void onInit() {
190190
}
191191
}
192192
this.fileSource.setDirectory(this.localDirectory);
193-
if (this.localFileListFilter == null) {
194-
this.localFileListFilter =
195-
new FileSystemPersistentAcceptOnceFileListFilter(new SimpleMetadataStore(), getComponentName());
196-
}
197-
FileListFilter<File> filter = buildFilter();
198-
if (this.scannerExplicitlySet) {
199-
Assert.state(!this.fileSource.isUseWatchService(),
200-
"'useWatchService' and 'scanner' are mutually exclusive.");
201-
this.fileSource.getScanner()
202-
.setFilter(filter);
203-
}
204-
else if (!this.fileSource.isUseWatchService()) {
205-
DirectoryScanner directoryScanner = new DefaultDirectoryScanner();
206-
directoryScanner.setFilter(filter);
207-
this.fileSource.setScanner(directoryScanner);
208-
}
209-
else {
210-
this.fileSource.setFilter(filter);
211-
}
193+
initFiltersAndScanner();
212194
if (this.getBeanFactory() != null) {
213195
this.fileSource.setBeanFactory(this.getBeanFactory());
214196
}
215197
this.fileSource.afterPropertiesSet();
216198
this.synchronizer.afterPropertiesSet();
217199
}
218-
catch (RuntimeException e) {
200+
catch (RuntimeException e) { // NOSONAR catch and throw
219201
throw e;
220202
}
221203
catch (Exception e) {
222204
throw new BeanInitializationException("Failure during initialization for: " + this, e);
223205
}
224206
}
225207

208+
private void initFiltersAndScanner() {
209+
if (this.localFileListFilter == null) {
210+
this.localFileListFilter =
211+
new FileSystemPersistentAcceptOnceFileListFilter(new SimpleMetadataStore(), getComponentName());
212+
}
213+
FileListFilter<File> filter = buildFilter();
214+
if (this.scannerExplicitlySet) {
215+
Assert.state(!this.fileSource.isUseWatchService(),
216+
"'useWatchService' and 'scanner' are mutually exclusive.");
217+
this.fileSource.getScanner()
218+
.setFilter(filter);
219+
}
220+
else if (!this.fileSource.isUseWatchService()) {
221+
DirectoryScanner directoryScanner = new DefaultDirectoryScanner();
222+
directoryScanner.setFilter(filter);
223+
this.fileSource.setScanner(directoryScanner);
224+
}
225+
else {
226+
this.fileSource.setFilter(filter);
227+
}
228+
}
229+
226230
@Override
227231
public void start() {
228232
this.running = true;

spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/FailoverClientConnectionFactory.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ private synchronized void findAConnection() throws InterruptedException {
205205
+ ", trying another");
206206
}
207207
if (!this.factoryIterator.hasNext()) {
208-
if (retried && lastFactoryToTry == null || lastFactoryToTry == nextFactory) {
208+
if (retried && (lastFactoryToTry == null || lastFactoryToTry.equals(nextFactory))) {
209209
/*
210210
* We've tried every factory including the
211211
* one the current connection was on.
@@ -249,7 +249,7 @@ public synchronized void send(Message<?> message) {
249249
success = true;
250250
}
251251
catch (RuntimeException e) {
252-
if (retried && lastFactoryTried == lastFactoryToTry) {
252+
if (retried && lastFactoryTried.equals(lastFactoryToTry)) {
253253
logger.error("All connection factories exhausted", e);
254254
this.open = false;
255255
throw e;

spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/TcpNetServerConnectionFactory.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ public void run() {
170170
try {
171171
socket.close();
172172
}
173-
catch (@SuppressWarnings("unused") IOException e1) {
173+
catch (@SuppressWarnings("unused") IOException e1) { // NOSONAR - exception as flow control
174174
// empty
175175
}
176176
}

spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/connection/TcpNioConnection.java

-6
Original file line numberDiff line numberDiff line change
@@ -444,9 +444,6 @@ private void doRead() throws IOException {
444444
}
445445
sendToPipe(this.rawBuffer);
446446
}
447-
catch (RejectedExecutionException e) {
448-
throw e;
449-
}
450447
catch (IOException e) {
451448
publishConnectionExceptionEvent(e);
452449
throw e;
@@ -508,9 +505,6 @@ public void readPacket() {
508505
}
509506
closeConnection(true);
510507
}
511-
catch (RejectedExecutionException e) {
512-
throw e;
513-
}
514508
catch (Exception e) {
515509
logger.error("Exception on Read " +
516510
getConnectionId() + " " +

spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/serializer/ByteArrayCrLfSerializer.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ public int fillToCrLf(InputStream inputStream, byte[] buffer) throws IOException
7272
}
7373
return n - 1; // trim \r
7474
}
75-
catch (SoftEndOfStreamException e) {
76-
throw e;
75+
catch (SoftEndOfStreamException e) { // NOSONAR catch and throw
76+
throw e; // it's an IO exception and we don't want an event for this
7777
}
7878
catch (IOException e) {
7979
publishEvent(e, buffer, n);

spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/serializer/ByteArrayLengthHeaderSerializer.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,8 @@ protected int readHeader(InputStream inputStream) throws IOException {
277277
}
278278
return messageLength;
279279
}
280-
catch (SoftEndOfStreamException e) {
281-
throw e;
280+
catch (SoftEndOfStreamException e) { // NOSONAR catch and throw
281+
throw e; // it's an IO exception and we don't want an event for this
282282
}
283283
catch (IOException e) {
284284
publishEvent(e, lengthPart, -1);

spring-integration-ip/src/main/java/org/springframework/integration/ip/tcp/serializer/ByteArrayRawSerializer.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,8 @@ protected byte[] doDeserialize(InputStream inputStream, byte[] buffer) throws IO
102102
}
103103
return copyToSizedArray(buffer, n);
104104
}
105-
catch (SoftEndOfStreamException e) {
106-
throw e;
105+
catch (SoftEndOfStreamException e) { // NOSONAR catch and throw
106+
throw e; // it's an IO exception and we don't want an event for this
107107
}
108108
catch (IOException e) {
109109
publishEvent(e, buffer, n);

0 commit comments

Comments
 (0)