Skip to content

Commit ddff788

Browse files
garyrussellartembilan
authored andcommitted
Fix Sonar complexity issues
Address a few more issues.
1 parent e26c61d commit ddff788

File tree

3 files changed

+69
-141
lines changed

3 files changed

+69
-141
lines changed

spring-integration-amqp/src/main/java/org/springframework/integration/amqp/support/DefaultAmqpHeaderMapper.java

+19-33
Original file line numberDiff line numberDiff line change
@@ -109,54 +109,40 @@ protected Map<String, Object> extractStandardHeaders(MessageProperties amqpMessa
109109
Map<String, Object> headers = new HashMap<String, Object>();
110110
try {
111111
JavaUtils.INSTANCE
112-
.acceptIfNotNull(AmqpHeaders.APP_ID, amqpMessageProperties.getAppId(),
113-
(key, value) -> headers.put(key, value))
114-
.acceptIfNotNull(AmqpHeaders.CLUSTER_ID, amqpMessageProperties.getClusterId(),
115-
(key, value) -> headers.put(key, value))
112+
.acceptIfNotNull(AmqpHeaders.APP_ID, amqpMessageProperties.getAppId(), headers::put)
113+
.acceptIfNotNull(AmqpHeaders.CLUSTER_ID, amqpMessageProperties.getClusterId(), headers::put)
116114
.acceptIfNotNull(AmqpHeaders.CONTENT_ENCODING, amqpMessageProperties.getContentEncoding(),
117-
(key, value) -> headers.put(key, value));
115+
headers::put);
118116
long contentLength = amqpMessageProperties.getContentLength();
119117
JavaUtils.INSTANCE
120-
.acceptIfCondition(contentLength > 0, AmqpHeaders.CONTENT_LENGTH, contentLength,
121-
(key, value) -> headers.put(key, value))
122-
.acceptIfHasText(AmqpHeaders.CONTENT_TYPE, amqpMessageProperties.getContentType(),
123-
(key, value) -> headers.put(key, value))
124-
.acceptIfHasText(AmqpHeaders.CORRELATION_ID, amqpMessageProperties.getCorrelationId(),
125-
(key, value) -> headers.put(key, value))
118+
.acceptIfCondition(contentLength > 0, AmqpHeaders.CONTENT_LENGTH, contentLength, headers::put)
119+
.acceptIfHasText(AmqpHeaders.CONTENT_TYPE, amqpMessageProperties.getContentType(), headers::put)
120+
.acceptIfHasText(AmqpHeaders.CORRELATION_ID, amqpMessageProperties.getCorrelationId(), headers::put)
126121
.acceptIfNotNull(AmqpHeaders.RECEIVED_DELIVERY_MODE, amqpMessageProperties.getReceivedDeliveryMode(),
127-
(key, value) -> headers.put(key, value));
122+
headers::put);
128123
long deliveryTag = amqpMessageProperties.getDeliveryTag();
129124
JavaUtils.INSTANCE
130-
.acceptIfCondition(deliveryTag > 0, AmqpHeaders.DELIVERY_TAG, deliveryTag,
131-
(key, value) -> headers.put(key, value))
132-
.acceptIfHasText(AmqpHeaders.EXPIRATION, amqpMessageProperties.getExpiration(),
133-
(key, value) -> headers.put(key, value));
125+
.acceptIfCondition(deliveryTag > 0, AmqpHeaders.DELIVERY_TAG, deliveryTag, headers::put)
126+
.acceptIfHasText(AmqpHeaders.EXPIRATION, amqpMessageProperties.getExpiration(), headers::put);
134127
Integer messageCount = amqpMessageProperties.getMessageCount();
135128
JavaUtils.INSTANCE
136129
.acceptIfCondition(messageCount != null && messageCount > 0, AmqpHeaders.MESSAGE_COUNT, messageCount,
137-
(key, value) -> headers.put(key, value))
138-
.acceptIfHasText(AmqpHeaders.MESSAGE_ID, amqpMessageProperties.getMessageId(),
139-
(key, value) -> headers.put(key, value));
130+
headers::put)
131+
.acceptIfHasText(AmqpHeaders.MESSAGE_ID, amqpMessageProperties.getMessageId(), headers::put);
140132
Integer priority = amqpMessageProperties.getPriority();
141133
JavaUtils.INSTANCE
142134
.acceptIfCondition(priority != null && priority > 0, IntegrationMessageHeaderAccessor.PRIORITY,
143135
priority, (key, value) -> headers.put(key, value))
144-
.acceptIfNotNull(AmqpHeaders.RECEIVED_DELAY, amqpMessageProperties.getReceivedDelay(),
145-
(key, value) -> headers.put(key, value))
136+
.acceptIfNotNull(AmqpHeaders.RECEIVED_DELAY, amqpMessageProperties.getReceivedDelay(), headers::put)
146137
.acceptIfNotNull(AmqpHeaders.RECEIVED_EXCHANGE, amqpMessageProperties.getReceivedExchange(),
147-
(key, value) -> headers.put(key, value))
138+
headers::put)
148139
.acceptIfHasText(AmqpHeaders.RECEIVED_ROUTING_KEY, amqpMessageProperties.getReceivedRoutingKey(),
149-
(key, value) -> headers.put(key, value))
150-
.acceptIfNotNull(AmqpHeaders.REDELIVERED, amqpMessageProperties.isRedelivered(),
151-
(key, value) -> headers.put(key, value))
152-
.acceptIfNotNull(AmqpHeaders.REPLY_TO, amqpMessageProperties.getReplyTo(),
153-
(key, value) -> headers.put(key, value))
154-
.acceptIfNotNull(AmqpHeaders.TIMESTAMP, amqpMessageProperties.getTimestamp(),
155-
(key, value) -> headers.put(key, value))
156-
.acceptIfHasText(AmqpHeaders.TYPE, amqpMessageProperties.getType(),
157-
(key, value) -> headers.put(key, value))
158-
.acceptIfHasText(AmqpHeaders.RECEIVED_USER_ID, amqpMessageProperties.getReceivedUserId(),
159-
(key, value) -> headers.put(key, value));
140+
headers::put)
141+
.acceptIfNotNull(AmqpHeaders.REDELIVERED, amqpMessageProperties.isRedelivered(), headers::put)
142+
.acceptIfNotNull(AmqpHeaders.REPLY_TO, amqpMessageProperties.getReplyTo(), headers::put)
143+
.acceptIfNotNull(AmqpHeaders.TIMESTAMP, amqpMessageProperties.getTimestamp(), headers::put)
144+
.acceptIfHasText(AmqpHeaders.TYPE, amqpMessageProperties.getType(), headers::put)
145+
.acceptIfHasText(AmqpHeaders.RECEIVED_USER_ID, amqpMessageProperties.getReceivedUserId(), headers::put);
160146

161147
for (String jsonHeader : JsonHeaders.HEADERS) {
162148
Object value = amqpMessageProperties.getHeaders().get(jsonHeader.replaceFirst(JsonHeaders.PREFIX, ""));

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

+27-28
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import org.springframework.integration.handler.AbstractMessageProducingHandler;
4242
import org.springframework.integration.handler.AbstractReplyProducingMessageHandler;
4343
import org.springframework.integration.support.context.NamedComponent;
44+
import org.springframework.integration.util.JavaUtils;
4445
import org.springframework.messaging.MessageChannel;
4546
import org.springframework.messaging.MessageHandler;
4647
import org.springframework.messaging.core.DestinationResolver;
@@ -195,31 +196,32 @@ protected final H createHandlerInternal() {
195196
return null;
196197
}
197198
this.handler = createHandler();
198-
if (this.handler instanceof ApplicationContextAware && this.applicationContext != null) {
199-
((ApplicationContextAware) this.handler).setApplicationContext(this.applicationContext);
200-
}
201-
if (this.handler instanceof BeanFactoryAware && getBeanFactory() != null) {
202-
((BeanFactoryAware) this.handler).setBeanFactory(getBeanFactory());
203-
}
204-
if (this.handler instanceof BeanNameAware && this.beanName != null) {
205-
((BeanNameAware) this.handler).setBeanName(this.beanName);
206-
}
207-
if (this.handler instanceof ApplicationEventPublisherAware && this.applicationEventPublisher != null) {
208-
((ApplicationEventPublisherAware) this.handler)
209-
.setApplicationEventPublisher(this.applicationEventPublisher);
210-
}
199+
JavaUtils.INSTANCE
200+
.acceptIfCondition(this.handler instanceof ApplicationContextAware && this.applicationContext != null,
201+
this.applicationContext,
202+
context -> ((ApplicationContextAware) this.handler).setApplicationContext(this.applicationContext))
203+
.acceptIfCondition(this.handler instanceof BeanFactoryAware && getBeanFactory() != null,
204+
getBeanFactory(),
205+
factory -> ((BeanFactoryAware) this.handler).setBeanFactory(factory))
206+
.acceptIfCondition(this.handler instanceof BeanNameAware && this.beanName != null, this.beanName,
207+
namne -> ((BeanNameAware) this.handler).setBeanName(this.beanName))
208+
.acceptIfCondition(this.handler instanceof ApplicationEventPublisherAware
209+
&& this.applicationEventPublisher != null,
210+
this.applicationEventPublisher,
211+
publisher -> ((ApplicationEventPublisherAware) this.handler)
212+
.setApplicationEventPublisher(publisher));
211213
configureOutputChannelIfAny();
212214
Object actualHandler = extractTarget(this.handler);
213215
if (actualHandler == null) {
214216
actualHandler = this.handler;
215217
}
218+
final Object handlerToConfigure = actualHandler; // must final for lambdas
216219
if (actualHandler instanceof IntegrationObjectSupport) {
217-
if (this.componentName != null) {
218-
((IntegrationObjectSupport) actualHandler).setComponentName(this.componentName);
219-
}
220-
if (this.channelResolver != null) {
221-
((IntegrationObjectSupport) actualHandler).setChannelResolver(this.channelResolver);
222-
}
220+
JavaUtils.INSTANCE
221+
.acceptIfNotNull(this.componentName,
222+
name -> ((IntegrationObjectSupport) handlerToConfigure).setComponentName(name))
223+
.acceptIfNotNull(this.channelResolver,
224+
resolver -> ((IntegrationObjectSupport) handlerToConfigure).setChannelResolver(resolver));
223225
}
224226
if (!CollectionUtils.isEmpty(this.adviceChain)) {
225227
if (actualHandler instanceof AbstractReplyProducingMessageHandler) {
@@ -234,15 +236,12 @@ else if (this.logger.isDebugEnabled()) {
234236
+ (name == null ? "" : (", " + name)) + ".");
235237
}
236238
}
237-
if (this.async != null) {
238-
if (actualHandler instanceof AbstractMessageProducingHandler) {
239-
((AbstractMessageProducingHandler) actualHandler)
240-
.setAsync(this.async);
241-
}
242-
}
243-
if (this.handler instanceof Orderable && this.order != null) {
244-
((Orderable) this.handler).setOrder(this.order);
245-
}
239+
JavaUtils.INSTANCE
240+
.acceptIfCondition(this.async != null && actualHandler instanceof AbstractMessageProducingHandler,
241+
this.async,
242+
asyncValue -> ((AbstractMessageProducingHandler) handlerToConfigure).setAsync(asyncValue))
243+
.acceptIfCondition(this.handler instanceof Orderable && this.order != null,
244+
this.order, theOrder -> ((Orderable) this.handler).setOrder(theOrder));
246245
this.initialized = true;
247246
}
248247
if (this.handler instanceof InitializingBean) {

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

+23-80
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.springframework.integration.store.MessageGroupStore;
3030
import org.springframework.integration.support.locks.LockRegistry;
3131
import org.springframework.integration.support.management.AbstractMessageHandlerMetrics;
32+
import org.springframework.integration.util.JavaUtils;
3233
import org.springframework.messaging.MessageChannel;
3334
import org.springframework.messaging.MessageHandler;
3435
import org.springframework.scheduling.TaskScheduler;
@@ -107,6 +108,7 @@ public void setSendTimeout(Long sendTimeout) {
107108
this.sendTimeout = sendTimeout;
108109
}
109110

111+
@Override
110112
public void setOutputChannelName(String outputChannelName) {
111113
this.outputChannelName = outputChannelName;
112114
}
@@ -194,86 +196,27 @@ protected AggregatingMessageHandler createHandler() {
194196
}
195197
}
196198
AggregatingMessageHandler aggregator = new AggregatingMessageHandler(outputProcessor);
197-
198-
if (this.expireGroupsUponCompletion != null) {
199-
aggregator.setExpireGroupsUponCompletion(this.expireGroupsUponCompletion);
200-
}
201-
202-
if (this.sendTimeout != null) {
203-
aggregator.setSendTimeout(this.sendTimeout);
204-
}
205-
206-
if (this.outputChannelName != null) {
207-
aggregator.setOutputChannelName(this.outputChannelName);
208-
}
209-
210-
if (this.metrics != null) {
211-
aggregator.configureMetrics(this.metrics);
212-
}
213-
214-
if (this.statsEnabled != null) {
215-
aggregator.setStatsEnabled(this.statsEnabled);
216-
}
217-
218-
if (this.countsEnabled != null) {
219-
aggregator.setCountsEnabled(this.countsEnabled);
220-
}
221-
222-
if (this.lockRegistry != null) {
223-
aggregator.setLockRegistry(this.lockRegistry);
224-
}
225-
226-
if (this.messageStore != null) {
227-
aggregator.setMessageStore(this.messageStore);
228-
}
229-
230-
if (this.correlationStrategy != null) {
231-
aggregator.setCorrelationStrategy(this.correlationStrategy);
232-
}
233-
234-
if (this.releaseStrategy != null) {
235-
aggregator.setReleaseStrategy(this.releaseStrategy);
236-
}
237-
238-
if (this.groupTimeoutExpression != null) {
239-
aggregator.setGroupTimeoutExpression(this.groupTimeoutExpression);
240-
}
241-
242-
if (this.forceReleaseAdviceChain != null) {
243-
aggregator.setForceReleaseAdviceChain(this.forceReleaseAdviceChain);
244-
}
245-
246-
if (this.taskScheduler != null) {
247-
aggregator.setTaskScheduler(this.taskScheduler);
248-
}
249-
250-
if (this.discardChannel != null) {
251-
aggregator.setDiscardChannel(this.discardChannel);
252-
}
253-
254-
if (this.discardChannelName != null) {
255-
aggregator.setDiscardChannelName(this.discardChannelName);
256-
}
257-
258-
if (this.sendPartialResultOnExpiry != null) {
259-
aggregator.setSendPartialResultOnExpiry(this.sendPartialResultOnExpiry);
260-
}
261-
262-
if (this.minimumTimeoutForEmptyGroups != null) {
263-
aggregator.setMinimumTimeoutForEmptyGroups(this.minimumTimeoutForEmptyGroups);
264-
}
265-
266-
if (this.expireGroupsUponTimeout != null) {
267-
aggregator.setExpireGroupsUponTimeout(this.expireGroupsUponTimeout);
268-
}
269-
270-
if (this.popSequence != null) {
271-
aggregator.setPopSequence(this.popSequence);
272-
}
273-
274-
if (this.releaseLockBeforeSend != null) {
275-
aggregator.setReleaseLockBeforeSend(this.releaseLockBeforeSend);
276-
}
199+
JavaUtils.INSTANCE
200+
.acceptIfNotNull(this.expireGroupsUponCompletion, aggregator::setExpireGroupsUponCompletion)
201+
.acceptIfNotNull(this.sendTimeout, aggregator::setSendTimeout)
202+
.acceptIfNotNull(this.outputChannelName, aggregator::setOutputChannelName)
203+
.acceptIfNotNull(this.metrics, aggregator::configureMetrics)
204+
.acceptIfNotNull(this.statsEnabled, aggregator::setStatsEnabled)
205+
.acceptIfNotNull(this.countsEnabled, aggregator::setCountsEnabled)
206+
.acceptIfNotNull(this.lockRegistry, aggregator::setLockRegistry)
207+
.acceptIfNotNull(this.messageStore, aggregator::setMessageStore)
208+
.acceptIfNotNull(this.correlationStrategy, aggregator::setCorrelationStrategy)
209+
.acceptIfNotNull(this.releaseStrategy, aggregator::setReleaseStrategy)
210+
.acceptIfNotNull(this.groupTimeoutExpression, aggregator::setGroupTimeoutExpression)
211+
.acceptIfNotNull(this.forceReleaseAdviceChain, aggregator::setForceReleaseAdviceChain)
212+
.acceptIfNotNull(this.taskScheduler, aggregator::setTaskScheduler)
213+
.acceptIfNotNull(this.discardChannel, aggregator::setDiscardChannel)
214+
.acceptIfNotNull(this.discardChannelName, aggregator::setDiscardChannelName)
215+
.acceptIfNotNull(this.sendPartialResultOnExpiry, aggregator::setSendPartialResultOnExpiry)
216+
.acceptIfNotNull(this.minimumTimeoutForEmptyGroups, aggregator::setMinimumTimeoutForEmptyGroups)
217+
.acceptIfNotNull(this.expireGroupsUponTimeout, aggregator::setExpireGroupsUponTimeout)
218+
.acceptIfNotNull(this.popSequence, aggregator::setPopSequence)
219+
.acceptIfNotNull(this.releaseLockBeforeSend, aggregator::setReleaseLockBeforeSend);
277220

278221
return aggregator;
279222
}

0 commit comments

Comments
 (0)