Skip to content

Commit ebbcb9d

Browse files
garyrussellartembilan
authored andcommitted
Sonar fixes
* exposing internal objects * boolean complexity * names, methods with _ * missing `default` in switch * lost stack trace * unnecessary null check before instanceof * unused parameter * Fix method name
1 parent 9780a6c commit ebbcb9d

File tree

35 files changed

+128
-102
lines changed

35 files changed

+128
-102
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ protected Date getLastModified() {
6565
}
6666

6767
public Date getExpired() {
68-
return this.expired;
68+
return (Date) this.expired.clone();
6969
}
7070

7171
public boolean isDiscarded() {

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public void setPatterns(String[] patterns) {
6666
}
6767

6868
public String[] getPatterns() {
69-
return this.patterns;
69+
return this.patterns; // NOSONAR - expose internals
7070
}
7171

7272
@Override

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

+7-3
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,13 @@ protected void postProcessReplyProducer(AbstractMessageProducingHandler handler)
132132
@Override
133133
protected boolean canBeUsedDirect(AbstractMessageProducingHandler handler) {
134134
return handler instanceof MessageFilter
135-
|| (!(handler instanceof MessageSelector)
136-
&& this.discardChannel == null && this.throwExceptionOnRejection == null
137-
&& this.discardWithinAdvice == null);
135+
|| (!(handler instanceof MessageSelector) && noFilterAttributesProvided());
136+
}
137+
138+
private boolean noFilterAttributesProvided() {
139+
return this.discardChannel == null
140+
&& this.throwExceptionOnRejection == null
141+
&& this.discardWithinAdvice == null;
138142
}
139143

140144
@Override

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ protected boolean canBeUsedDirect(AbstractMessageProducingHandler handler) {
152152
}
153153

154154
protected boolean noRouterAttributesProvided() {
155-
return this.channelMappings == null && this.defaultOutputChannel == null
155+
return this.channelMappings == null && this.defaultOutputChannel == null // NOSONAR boolean complexity
156156
&& getSendTimeout() == null && this.resolutionRequired == null && this.applySequence == null
157157
&& this.ignoreSendFailures == null;
158158
}

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

+6-3
Original file line numberDiff line numberDiff line change
@@ -395,9 +395,12 @@ protected void configurePollingEndpoint(AbstractPollingEndpoint pollingEndpoint,
395395
String receiveTimeout = this.beanFactory.resolveEmbeddedValue(poller.receiveTimeout());
396396

397397
if (StringUtils.hasText(ref)) {
398-
Assert.state(!StringUtils.hasText(triggerRef) && !StringUtils.hasText(executorRef) &&
399-
!StringUtils.hasText(cron) && !StringUtils.hasText(fixedDelayValue) &&
400-
!StringUtils.hasText(fixedRateValue) && !StringUtils.hasText(maxMessagesPerPollValue),
398+
Assert.state(!StringUtils.hasText(triggerRef)
399+
&& !StringUtils.hasText(executorRef)
400+
&& !StringUtils.hasText(cron)
401+
&& !StringUtils.hasText(fixedDelayValue)
402+
&& !StringUtils.hasText(fixedRateValue)
403+
&& !StringUtils.hasText(maxMessagesPerPollValue), // NOSONAR boolean complexity
401404
"The '@Poller' 'ref' attribute is mutually exclusive with other attributes.");
402405
pollerMetadata = this.beanFactory.getBean(ref, PollerMetadata.class);
403406
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ private boolean routerAttributesProvided(List<Annotation> annotations) {
147147
String applySequence = MessagingAnnotationUtils.resolveAttribute(annotations, "applySequence", String.class);
148148
String ignoreSendFailures = MessagingAnnotationUtils.resolveAttribute(annotations, "ignoreSendFailures",
149149
String.class);
150-
return StringUtils.hasText(defaultOutputChannel) || !ObjectUtils.isEmpty(channelMappings)
150+
return StringUtils.hasText(defaultOutputChannel) || !ObjectUtils.isEmpty(channelMappings) // NOSONAR complexity
151151
|| StringUtils.hasText(prefix) || StringUtils.hasText(suffix) || StringUtils.hasText(resolutionRequired)
152152
|| StringUtils.hasText(applySequence) || StringUtils.hasText(ignoreSendFailures);
153153
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ protected String resolveId(Element element, AbstractBeanDefinition definition, P
5151

5252
@Override
5353
protected boolean isEligibleAttribute(String attributeName) {
54-
return !attributeName.equals("name") && !attributeName.equals("request-channel")
54+
return !attributeName.equals("name") && !attributeName.equals("request-channel") // NOSONAR boolean complexity
5555
&& !attributeName.equals("error-channel")
5656
&& !attributeName.equals("reply-channel") && super.isEligibleAttribute(attributeName);
5757
}

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

+20-13
Original file line numberDiff line numberDiff line change
@@ -65,16 +65,16 @@ protected BeanMetadataElement parseSource(Element element, ParserContext parserC
6565
boolean hasExpressionElement = expressionElement != null;
6666
boolean hasMethod = StringUtils.hasText(methodName);
6767

68-
if (!hasInnerDef && !hasRef && !hasExpression && !hasScriptElement && !hasExpressionElement) {
68+
if (!hasInnerDef && !hasRef && !hasExpression && !hasScriptElement && !hasExpressionElement) { // NOSONAR
6969
parserContext.getReaderContext().error(
70-
"Exactly one of the 'ref', 'expression', inner bean, <script> or <expression> is required.", element);
70+
"Exactly one of the 'ref', 'expression', inner bean, <script> or <expression> is required.", element);
7171
}
7272

7373
if (hasInnerDef) {
7474
if (hasRef || hasExpression) {
7575
parserContext.getReaderContext().error(
76-
"Neither 'ref' nor 'expression' are permitted when an inner bean (<bean/>) is configured on element " +
77-
IntegrationNamespaceUtils.createElementDescription(element) + ".", source);
76+
"Neither 'ref' nor 'expression' are permitted when an inner bean (<bean/>) is configured on "
77+
+ "element " + IntegrationNamespaceUtils.createElementDescription(element) + ".", source);
7878
return null;
7979
}
8080
if (hasMethod) {
@@ -87,8 +87,9 @@ protected BeanMetadataElement parseSource(Element element, ParserContext parserC
8787
else if (hasScriptElement) {
8888
if (hasRef || hasMethod || hasExpression) {
8989
parserContext.getReaderContext().error(
90-
"Neither 'ref' and 'method' nor 'expression' are permitted when an inner script element is configured on element " +
91-
IntegrationNamespaceUtils.createElementDescription(element) + ".", source);
90+
"Neither 'ref' and 'method' nor 'expression' are permitted when an inner script element is "
91+
+ "configured on element "
92+
+ IntegrationNamespaceUtils.createElementDescription(element) + ".", source);
9293
return null;
9394
}
9495
BeanDefinition scriptBeanDefinition = parserContext.getDelegate().parseCustomElement(scriptElement);
@@ -101,7 +102,8 @@ else if (hasScriptElement) {
101102
else if (hasExpression || hasExpressionElement) {
102103
if (hasRef || hasMethod) {
103104
parserContext.getReaderContext().error(
104-
"The 'ref' and 'method' attributes can't be used with 'expression' attribute or inner <expression>.", element);
105+
"The 'ref' and 'method' attributes can't be used with 'expression' attribute or inner "
106+
+ "<expression>.", element);
105107
return null;
106108
}
107109
if (hasExpression & hasExpressionElement) {
@@ -124,9 +126,11 @@ else if (hasRef) {
124126
return result;
125127
}
126128

127-
private BeanMetadataElement parseMethodInvokingSource(BeanMetadataElement targetObject, String methodName, Element element,
128-
ParserContext parserContext) {
129-
BeanDefinitionBuilder sourceBuilder = BeanDefinitionBuilder.genericBeanDefinition(MethodInvokingMessageSource.class);
129+
private BeanMetadataElement parseMethodInvokingSource(BeanMetadataElement targetObject, String methodName,
130+
Element element, ParserContext parserContext) {
131+
132+
BeanDefinitionBuilder sourceBuilder = BeanDefinitionBuilder
133+
.genericBeanDefinition(MethodInvokingMessageSource.class);
130134
sourceBuilder.addPropertyValue("object", targetObject);
131135
sourceBuilder.addPropertyValue("methodName", methodName);
132136
this.parseHeaderExpressions(sourceBuilder, element, parserContext);
@@ -135,7 +139,9 @@ private BeanMetadataElement parseMethodInvokingSource(BeanMetadataElement target
135139

136140
private BeanMetadataElement parseExpression(String expressionString, Element expressionElement, Element element,
137141
ParserContext parserContext) {
138-
BeanDefinitionBuilder sourceBuilder = BeanDefinitionBuilder.genericBeanDefinition(ExpressionEvaluatingMessageSource.class);
142+
143+
BeanDefinitionBuilder sourceBuilder = BeanDefinitionBuilder
144+
.genericBeanDefinition(ExpressionEvaluatingMessageSource.class);
139145

140146
BeanDefinition expressionDef = null;
141147

@@ -166,8 +172,9 @@ private void parseHeaderExpressions(BeanDefinitionBuilder builder, Element eleme
166172
ManagedMap<String, Object> headerExpressions = new ManagedMap<String, Object>();
167173
for (Element headerElement : headerElements) {
168174
String headerName = headerElement.getAttribute("name");
169-
BeanDefinition expressionDef = IntegrationNamespaceUtils.createExpressionDefinitionFromValueOrExpression("value",
170-
"expression", parserContext, headerElement, true);
175+
BeanDefinition expressionDef = IntegrationNamespaceUtils
176+
.createExpressionDefinitionFromValueOrExpression("value",
177+
"expression", parserContext, headerElement, true);
171178
headerExpressions.put(headerName, expressionDef);
172179
}
173180
builder.addPropertyValue("headerExpressions", headerExpressions);

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

+5-4
Original file line numberDiff line numberDiff line change
@@ -60,23 +60,24 @@ protected AbstractBeanDefinition parseInternal(Element element, ParserContext pa
6060

6161
String endpoints = element.getAttribute("endpoint");
6262

63-
if (!hasSelector & !(hasKeyStrategy | hasKeyExpression)) {
63+
if (!hasSelector && !(hasKeyStrategy || hasKeyExpression)) {
6464
parserContext.getReaderContext().error("One of the 'selector', 'key-strategy' or 'key-expression' " +
6565
"attributes must be provided", source);
6666
}
6767

68-
if (hasSelector & (hasStore | hasKeyStrategy | hasKeyExpression | hasValueStrategy | hasValueExpression)) {
68+
if (hasSelector && (hasStore || hasKeyStrategy || hasKeyExpression || hasValueStrategy // NOSONAR complexity
69+
|| hasValueExpression)) {
6970
parserContext.getReaderContext().error("The 'selector' attribute is mutually exclusive with " +
7071
"'metadata-store', 'key-strategy', 'key-expression', 'value-strategy' " +
7172
"or 'value-expression'", source);
7273
}
7374

74-
if (hasKeyStrategy & hasKeyExpression) {
75+
if (hasKeyStrategy && hasKeyExpression) {
7576
parserContext.getReaderContext().error("The 'key-strategy' and 'key-expression' attributes " +
7677
"are mutually exclusive", source);
7778
}
7879

79-
if (hasValueStrategy & hasValueExpression) {
80+
if (hasValueStrategy && hasValueExpression) {
8081
parserContext.getReaderContext().error("The 'value-strategy' and 'value-expression' attributes " +
8182
"are mutually exclusive", source);
8283
}

spring-integration-core/src/main/java/org/springframework/integration/dispatcher/AggregateMessageDeliveryException.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ public List<? extends Exception> getAggregatedExceptions() {
5858
@Override
5959
public String getMessage() {
6060
String baseMessage = super.getMessage();
61-
StringBuilder message = new StringBuilder(appendPeriodIfNecessary(baseMessage) + " Multiple causes:\n");
61+
StringBuilder message = new StringBuilder(appendPeriodIfNecessary(baseMessage))
62+
.append(" Multiple causes:\n");
6263
for (Exception exception : this.aggregatedExceptions) {
6364
message.append(" " + exception.getMessage() + "\n");
6465
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -3114,7 +3114,7 @@ private boolean isOutputChannelRequired() {
31143114
}
31153115

31163116
@SuppressWarnings("unchecked")
3117-
protected final B _this() {
3117+
protected final B _this() { // NOSONAR name
31183118
return (B) this;
31193119
}
31203120

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

+4
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,10 @@ public Object invoke(MethodInvocation invocation) throws Throwable {
113113
result = this.delegate.getPhase();
114114
}
115115
break;
116+
117+
default:
118+
break;
119+
116120
}
117121
}
118122

spring-integration-core/src/main/java/org/springframework/integration/gateway/GatewayProxyFactoryBean.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -547,10 +547,10 @@ private void rethrowExceptionCauseIfPossible(Throwable originalException, Method
547547
throw t;
548548
}
549549
}
550-
if (t instanceof RuntimeException
550+
if (t instanceof RuntimeException // NOSONAR boolean complexity
551551
&& !(t instanceof MessagingException)
552552
&& !(t instanceof UndeclaredThrowableException)
553-
&& !(t instanceof IllegalStateException && ("Unexpected exception thrown").equals(t.getMessage()))) {
553+
&& !(t instanceof IllegalStateException && "Unexpected exception thrown".equals(t.getMessage()))) {
554554
throw t;
555555
}
556556
t = t.getCause();

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@ private static Level convertLevel(String level) {
8585
return Level.valueOf(level.toUpperCase());
8686
}
8787
catch (@SuppressWarnings("unused") IllegalArgumentException e) {
88-
throw new IllegalArgumentException("Invalid log level '" + level
88+
throw new IllegalArgumentException("Invalid log level '" + level // NOSONAR lost stack trace
8989
+ "'. The (case-insensitive) supported values are: "
90-
+ StringUtils.arrayToCommaDelimitedString(Level.values())); // NOSONAR lost stack trace
90+
+ StringUtils.arrayToCommaDelimitedString(Level.values()));
9191
}
9292
}
9393

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public final Object invoke(MethodInvocation invocation) throws Throwable { // NO
4040
Method method = invocation.getMethod();
4141
Object invocationThis = invocation.getThis();
4242
Object[] arguments = invocation.getArguments();
43-
boolean isMessageHandler = invocationThis != null && invocationThis instanceof MessageHandler;
43+
boolean isMessageHandler = invocationThis instanceof MessageHandler;
4444
boolean isMessageMethod = method.getName().equals("handleMessage")
4545
&& (arguments.length == 1 && arguments[0] instanceof Message);
4646
if (!isMessageHandler || !isMessageMethod) {
@@ -59,6 +59,6 @@ public final Object invoke(MethodInvocation invocation) throws Throwable { // NO
5959
return doInvoke(invocation, message);
6060
}
6161

62-
protected abstract Object doInvoke(MethodInvocation invocation, Message<?> message) throws Throwable;
62+
protected abstract Object doInvoke(MethodInvocation invocation, Message<?> message) throws Throwable; // NOSONAR
6363

6464
}

spring-integration-core/src/main/java/org/springframework/integration/json/JsonPropertyAccessor.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public void setObjectMapper(ObjectMapper objectMapper) {
6868

6969
@Override
7070
public Class<?>[] getSpecificTargetClasses() {
71-
return SUPPORTED_CLASSES;
71+
return SUPPORTED_CLASSES; // NOSONAR - expose internals
7272
}
7373

7474
@Override

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public Map<K, V> get() {
4848
}
4949

5050
@SuppressWarnings("unchecked")
51-
protected final B _this() {
51+
protected final B _this() { // NOSONAR name
5252
return (B) this;
5353
}
5454

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ public boolean equals(Object obj) {
111111
if (this == obj) {
112112
return true;
113113
}
114-
if (obj != null && obj instanceof MutableMessage<?>) {
114+
if (obj instanceof MutableMessage<?>) {
115115
MutableMessage<?> other = (MutableMessage<?>) obj;
116116
UUID thisId = this.headers.getId();
117117
UUID otherId = other.headers.getId();

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.nio.ByteBuffer;
2222
import java.util.Arrays;
2323
import java.util.Collection;
24-
import java.util.HashMap;
2524
import java.util.Map;
2625
import java.util.stream.Collectors;
2726

@@ -260,7 +259,7 @@ private Message<?> decodeNativeFormat(byte[] bytes, @Nullable Map<String, Object
260259
buffer.position(4);
261260
@SuppressWarnings("unchecked")
262261
Map<String, Object> headers = this.objectMapper.readValue(bytes, buffer.position(), headersLen,
263-
HashMap.class);
262+
Map.class);
264263

265264
buffer.position(buffer.position() + headersLen);
266265
buffer.getInt();

spring-integration-core/src/main/java/org/springframework/integration/transaction/TransactionSynchronizationFactoryBean.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
public class TransactionSynchronizationFactoryBean implements FactoryBean<DefaultTransactionSynchronizationFactory>,
4242
BeanFactoryAware {
4343

44-
private final SpelExpressionParser PARSER = new SpelExpressionParser();
44+
private static final SpelExpressionParser PARSER = new SpelExpressionParser();
4545

4646
private final AtomicInteger counter = new AtomicInteger();
4747

spring-integration-core/src/main/java/org/springframework/integration/util/BeanFactoryTypeConverter.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public Object convertValue(Object value, TypeDescriptor sourceType, TypeDescript
108108
if (sourceType != null) {
109109
Class<?> sourceClass = sourceType.getType();
110110
Class<?> targetClass = targetType.getType();
111-
if ((sourceClass == MessageHeaders.class && targetClass == MessageHeaders.class) ||
111+
if ((sourceClass == MessageHeaders.class && targetClass == MessageHeaders.class) || // NOSONAR
112112
(sourceClass == MessageHistory.class && targetClass == MessageHistory.class) ||
113113
(sourceType.isAssignableTo(targetType) && ClassUtils.isPrimitiveArray(sourceClass))) {
114114
return value;

spring-integration-core/src/main/java/org/springframework/integration/util/MessagingAnnotationUtils.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public static <T> T resolveAttribute(List<Annotation> annotations, String name,
7272
}
7373

7474
public static boolean hasValue(Object value) {
75-
return value != null && (!(value instanceof String) || (StringUtils.hasText((String) value)))
75+
return value != null && (!(value instanceof String) || (StringUtils.hasText((String) value))) // NOSONAR
7676
&& (!value.getClass().isArray() || ((Object[]) value).length > 0);
7777
}
7878

@@ -103,7 +103,7 @@ public static Annotation findMessagePartAnnotation(Annotation[] annotations, boo
103103
Annotation match = null;
104104
for (Annotation annotation : annotations) {
105105
Class<? extends Annotation> type = annotation.annotationType();
106-
if (type.equals(Payload.class)
106+
if (type.equals(Payload.class) // NOSONAR boolean complexity
107107
|| type.equals(Header.class)
108108
|| type.equals(Headers.class)
109109
|| (payloads && type.equals(Payloads.class))) {

spring-integration-core/src/test/java/org/springframework/integration/support/json/EmbeddedJsonHeadersMessageMapperTests.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
public class EmbeddedJsonHeadersMessageMapperTests {
4242

4343
@Test
44-
public void testEmbedAll() throws Exception {
44+
public void testEmbedAll() {
4545
EmbeddedJsonHeadersMessageMapper mapper = new EmbeddedJsonHeadersMessageMapper();
4646
GenericMessage<String> message = new GenericMessage<>("foo");
4747
assertThat(mapper.toMessage(mapper.fromMessage(message))).isEqualTo(message);
@@ -92,7 +92,7 @@ public void testBytesEmbedAll() throws Exception {
9292
}
9393

9494
@Test
95-
public void testBytesEmbedSome() throws Exception {
95+
public void testBytesEmbedSome() {
9696
EmbeddedJsonHeadersMessageMapper mapper = new EmbeddedJsonHeadersMessageMapper("I*");
9797
GenericMessage<byte[]> message = new GenericMessage<>("foo".getBytes(), Collections.singletonMap("bar", "baz"));
9898
byte[] bytes = mapper.fromMessage(message);
@@ -112,7 +112,7 @@ public void testBytesEmbedSome() throws Exception {
112112
}
113113

114114
@Test
115-
public void testBytesEmbedAllJson() throws Exception {
115+
public void testBytesEmbedAllJson() {
116116
EmbeddedJsonHeadersMessageMapper mapper = new EmbeddedJsonHeadersMessageMapper();
117117
mapper.setRawBytes(false);
118118
GenericMessage<byte[]> message = new GenericMessage<>("foo".getBytes());
@@ -126,7 +126,7 @@ public void testBytesEmbedAllJson() throws Exception {
126126
}
127127

128128
@Test
129-
public void testBytesDecodeAll() throws Exception {
129+
public void testBytesDecodeAll() {
130130
EmbeddedJsonHeadersMessageMapper mapper = new EmbeddedJsonHeadersMessageMapper();
131131
GenericMessage<byte[]> message = new GenericMessage<>("foo".getBytes());
132132
Message<?> decoded = mapper.toMessage(mapper.fromMessage(message));

0 commit comments

Comments
 (0)