Fix new and some old Sonar smells

This commit is contained in:
Artem Bilan
2020-07-13 14:07:07 -04:00
parent 9223613ee1
commit 6e11d4cdf9
27 changed files with 287 additions and 305 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -30,17 +30,19 @@ import org.springframework.integration.handler.ExpressionEvaluatingMessageProces
* @author Mark Fisher
* @author Liujiong
* @author Gary Russell
* @author Artem Bilan
*
* @since 2.0
*/
public class ExpressionEvaluatingSelector extends AbstractMessageProcessingSelector {
private static final ExpressionParser expressionParser =
private static final ExpressionParser EXPRESSION_PARSER =
new SpelExpressionParser(new SpelParserConfiguration(true, true));
private final String expressionString;
public ExpressionEvaluatingSelector(String expressionString) {
super(new ExpressionEvaluatingMessageProcessor<Boolean>(expressionParser.parseExpression(expressionString),
super(new ExpressionEvaluatingMessageProcessor<Boolean>(EXPRESSION_PARSER.parseExpression(expressionString),
Boolean.class));
this.expressionString = expressionString;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -397,11 +397,10 @@ class GatewayMethodInboundMessageMapper implements InboundMessageMapper<Object[]
private void processMapArgument(Object messageOrPayload, boolean foundPayloadAnnotation,
Map<String, Object> headersToPopulate, Map<?, ?> argumentValue) {
if (messageOrPayload instanceof Map && !foundPayloadAnnotation) {
if (GatewayMethodInboundMessageMapper.this.payloadExpression == null) {
throw new MessagingException("Ambiguous method parameters; found more than one " +
"Map-typed parameter and neither one contains a @Payload annotation");
}
if (messageOrPayload instanceof Map && !foundPayloadAnnotation
&& GatewayMethodInboundMessageMapper.this.payloadExpression == null) {
throw new MessagingException("Ambiguous method parameters; found more than one " +
"Map-typed parameter and neither one contains a @Payload annotation");
}
copyHeaders(argumentValue, headersToPopulate);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2017-2019 the original author or authors.
* Copyright 2017-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -41,7 +41,7 @@ import org.springframework.beans.BeanUtils;
*/
public final class SimpleJsonSerializer {
private static final Log logger = LogFactory.getLog(SimpleJsonSerializer.class);
private static final Log LOGGER = LogFactory.getLog(SimpleJsonSerializer.class);
private SimpleJsonSerializer() {
}
@@ -69,12 +69,12 @@ public final class SimpleJsonSerializer {
}
catch (InvocationTargetException | IllegalAccessException | IllegalArgumentException e) {
Throwable exception = e;
if (e instanceof InvocationTargetException) {
if (e instanceof InvocationTargetException) { // NOSONAR
exception = e.getCause();
}
if (logger.isDebugEnabled()) {
logger.debug("Failed to serialize property " + propertyName, exception);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Failed to serialize property " + propertyName, exception);
}
result =

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -35,6 +35,7 @@ import org.springframework.integration.core.MessageSelector;
import org.springframework.integration.filter.ExpressionEvaluatingSelector;
import org.springframework.jmx.export.annotation.ManagedAttribute;
import org.springframework.jmx.export.annotation.ManagedOperation;
import org.springframework.lang.Nullable;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.core.DestinationResolver;
@@ -210,7 +211,7 @@ public class RecipientListRouter extends AbstractMessageRouter implements Recipi
MessageSelector selector = next.getSelector();
MessageChannel channel = next.getChannel();
if (selector instanceof ExpressionEvaluatingSelector
&& channel.equals(targetChannel)
&& targetChannel.equals(channel)
&& ((ExpressionEvaluatingSelector) selector).getExpressionString().equals(selectorExpression)) {
it.remove();
counter++;
@@ -307,14 +308,13 @@ public class RecipientListRouter extends AbstractMessageRouter implements Recipi
return this.selector;
}
@Nullable
public MessageChannel getChannel() {
if (this.channel == null) {
String channelNameForInitialization = this.channelName;
if (channelNameForInitialization != null) {
if (this.channelResolver != null) {
this.channel = this.channelResolver.resolveDestination(channelNameForInitialization);
this.channelName = null;
}
if (channelNameForInitialization != null && this.channelResolver != null) {
this.channel = this.channelResolver.resolveDestination(channelNameForInitialization);
this.channelName = null;
}
}
return this.channel;

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2014-2019 the original author or authors.
* Copyright 2014-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -34,7 +34,7 @@ import org.apache.commons.logging.LogFactory;
*/
public class PollSkipAdvice implements MethodInterceptor {
private static final Log logger = LogFactory.getLog(PollSkipAdvice.class);
private static final Log LOGGER = LogFactory.getLog(PollSkipAdvice.class);
private final PollSkipStrategy pollSkipStrategy;
@@ -51,8 +51,8 @@ public class PollSkipAdvice implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
if ("call".equals(invocation.getMethod().getName()) && this.pollSkipStrategy.skipPoll()) {
if (logger.isDebugEnabled()) {
logger.debug("Skipping poll because "
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Skipping poll because "
+ this.pollSkipStrategy.getClass().getName()
+ ".skipPoll() returned true");
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2016-2019 the original author or authors.
* Copyright 2016-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -33,7 +33,7 @@ import org.springframework.messaging.Message;
*/
class PersistentMessageGroup implements MessageGroup {
private static final Log logger = LogFactory.getLog(PersistentMessageGroup.class);
private static final Log LOGGER = LogFactory.getLog(PersistentMessageGroup.class);
private final MessageGroupStore messageGroupStore;
@@ -64,8 +64,8 @@ class PersistentMessageGroup implements MessageGroup {
if (this.oneMessage == null) {
synchronized (this) {
if (this.oneMessage == null) {
if (logger.isDebugEnabled()) {
logger.debug("Lazy loading of one message for messageGroup: " + this.original.getGroupId());
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Lazy loading of one message for messageGroup: " + this.original.getGroupId());
}
this.oneMessage = this.messageGroupStore.getOneMessageFromGroup(this.original.getGroupId());
}
@@ -97,8 +97,8 @@ class PersistentMessageGroup implements MessageGroup {
if (this.size == 0) {
synchronized (this) {
if (this.size == 0) {
if (logger.isDebugEnabled()) {
logger.debug("Lazy loading of group size for messageGroup: " + this.original.getGroupId());
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Lazy loading of group size for messageGroup: " + this.original.getGroupId());
}
this.size = this.messageGroupStore.messageGroupSize(this.original.getGroupId());
}
@@ -180,8 +180,8 @@ class PersistentMessageGroup implements MessageGroup {
synchronized (this) {
if (this.collection == null) {
Object groupId = PersistentMessageGroup.this.original.getGroupId();
if (logger.isDebugEnabled()) {
logger.debug("Lazy loading of messages for messageGroup: " + groupId);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Lazy loading of messages for messageGroup: " + groupId);
}
this.collection = PersistentMessageGroup.this.messageGroupStore.getMessagesForGroup(groupId);
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -41,11 +41,11 @@ import org.springframework.util.Assert;
* @author Gary Russell
* @author Artem Bilan
*
* @see org.springframework.beans.factory.BeanFactory
* @see BeanFactory
*/
public class BeanFactoryChannelResolver implements DestinationResolver<MessageChannel>, BeanFactoryAware {
private static final Log logger = LogFactory.getLog(BeanFactoryChannelResolver.class);
private static final Log LOGGER = LogFactory.getLog(BeanFactoryChannelResolver.class);
private BeanFactory beanFactory;
@@ -102,7 +102,7 @@ public class BeanFactoryChannelResolver implements DestinationResolver<MessageCh
HeaderChannelRegistry.class);
}
catch (Exception ex) {
logger.debug("No HeaderChannelRegistry found");
LOGGER.debug("No HeaderChannelRegistry found");
}
this.initialized = true;
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013-2019 the original author or authors.
* Copyright 2013-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -43,7 +43,7 @@ import org.springframework.util.ClassUtils;
*/
public abstract class AbstractJacksonJsonObjectMapper<N, P, J> implements JsonObjectMapper<N, P>, BeanClassLoaderAware {
protected static final Collection<Class<?>> supportedJsonTypes =
protected static final Collection<Class<?>> SUPPORTED_JSON_TYPES =
Arrays.asList(String.class, byte[].class, File.class, URL.class, InputStream.class, Reader.class);
private volatile ClassLoader classLoader = ClassUtils.getDefaultClassLoader();

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013-2019 the original author or authors.
* Copyright 2013-2020 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -150,7 +150,7 @@ public class Jackson2JsonObjectMapper extends AbstractJacksonJsonObjectMapper<Js
return this.objectMapper.readValue((Reader) json, type);
}
else {
throw new IllegalArgumentException("'json' argument must be an instance of: " + supportedJsonTypes
throw new IllegalArgumentException("'json' argument must be an instance of: " + SUPPORTED_JSON_TYPES
+ " , but gotten: " + json.getClass());
}
}

View File

@@ -29,28 +29,14 @@ public final class JacksonPresent {
private static final ClassLoader CLASS_LOADER = ClassUtils.getDefaultClassLoader();
private static final boolean jackson2Present =
private static final boolean JACKSON_2_PRESENT =
ClassUtils.isPresent("com.fasterxml.jackson.databind.ObjectMapper", CLASS_LOADER) &&
ClassUtils.isPresent("com.fasterxml.jackson.core.JsonGenerator", CLASS_LOADER);
private static final boolean jacksonPresent =
ClassUtils.isPresent("org.codehaus.jackson.map.ObjectMapper", CLASS_LOADER) &&
ClassUtils.isPresent("org.codehaus.jackson.JsonGenerator", CLASS_LOADER);
public static boolean isJackson2Present() {
return jackson2Present;
return JACKSON_2_PRESENT;
}
/**
* @return true if Jackson 1.x is present on classpath
* @deprecated Jackson 1.x is not supported any more. Use Jackson 2.x.
*/
@Deprecated
public static boolean isJacksonPresent() {
return jacksonPresent;
}
private JacksonPresent() {
}

View File

@@ -66,7 +66,7 @@ public class LockRegistryLeaderInitiator implements SmartLifecycle, DisposableBe
public static final long DEFAULT_BUSY_WAIT_TIME = 50L;
private static final Log logger = LogFactory.getLog(LockRegistryLeaderInitiator.class);
private static final Log LOGGER = LogFactory.getLog(LockRegistryLeaderInitiator.class);
private final Object lifecycleMonitor = new Object();
@@ -295,7 +295,7 @@ public class LockRegistryLeaderInitiator implements SmartLifecycle, DisposableBe
this.leaderSelector = new LeaderSelector(buildLeaderPath());
this.running = true;
this.future = this.executorService.submit(this.leaderSelector);
logger.debug("Started LeaderInitiator");
LOGGER.debug("Started LeaderInitiator");
}
}
}
@@ -321,7 +321,7 @@ public class LockRegistryLeaderInitiator implements SmartLifecycle, DisposableBe
this.future.cancel(true);
}
this.future = null;
logger.debug("Stopped LeaderInitiator for " + getContext());
LOGGER.debug("Stopped LeaderInitiator for " + getContext());
}
}
}
@@ -369,7 +369,7 @@ public class LockRegistryLeaderInitiator implements SmartLifecycle, DisposableBe
this.lock.unlock();
}
catch (Exception e) {
logger.debug("Could not unlock during stop for " + this.context
LOGGER.debug("Could not unlock during stop for " + this.context
+ " - treat as broken. Revoking...", e);
}
// We are stopping, therefore not leading any more
@@ -380,8 +380,8 @@ public class LockRegistryLeaderInitiator implements SmartLifecycle, DisposableBe
}
private void tryAcquireLock() throws InterruptedException {
if (logger.isDebugEnabled()) {
logger.debug("Acquiring the lock for " + this.context);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Acquiring the lock for " + this.context);
}
// We always try to acquire the lock, in case it expired
boolean acquired = this.lock.tryLock(LockRegistryLeaderInitiator.this.heartBeatMillis,
@@ -423,7 +423,7 @@ public class LockRegistryLeaderInitiator implements SmartLifecycle, DisposableBe
this.lock.unlock();
}
catch (Exception e1) {
logger.debug("Could not unlock - treat as broken " + this.context +
LOGGER.debug("Could not unlock - treat as broken " + this.context +
". Revoking " + (isRunning() ? " and retrying..." : "..."), e1);
}
@@ -449,8 +449,8 @@ public class LockRegistryLeaderInitiator implements SmartLifecycle, DisposableBe
Thread.currentThread().interrupt();
}
}
if (logger.isDebugEnabled()) {
logger.debug("Error acquiring the lock for " + this.context +
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Error acquiring the lock for " + this.context +
". " + (isRunning() ? "Retrying..." : ""), ex);
}
}
@@ -458,7 +458,7 @@ public class LockRegistryLeaderInitiator implements SmartLifecycle, DisposableBe
}
private void restartSelectorBecauseOfError(Exception ex) {
logger.warn("Restarting LeaderSelector for " + this.context + " because of error.", ex);
LOGGER.warn("Restarting LeaderSelector for " + this.context + " because of error.", ex);
LockRegistryLeaderInitiator.this.future =
LockRegistryLeaderInitiator.this.executorService.submit(
() -> {
@@ -480,7 +480,7 @@ public class LockRegistryLeaderInitiator implements SmartLifecycle, DisposableBe
LockRegistryLeaderInitiator.this, this.context, this.lockKey);
}
catch (Exception e) {
logger.warn("Error publishing OnGranted event.", e);
LOGGER.warn("Error publishing OnGranted event.", e);
}
}
}
@@ -494,7 +494,7 @@ public class LockRegistryLeaderInitiator implements SmartLifecycle, DisposableBe
LockRegistryLeaderInitiator.this.candidate.getRole());
}
catch (Exception e) {
logger.warn("Error publishing OnRevoked event.", e);
LOGGER.warn("Error publishing OnRevoked event.", e);
}
}
}
@@ -508,7 +508,7 @@ public class LockRegistryLeaderInitiator implements SmartLifecycle, DisposableBe
LockRegistryLeaderInitiator.this.candidate.getRole());
}
catch (Exception e) {
logger.warn("Error publishing OnFailedToAcquire event.", e);
LOGGER.warn("Error publishing OnFailedToAcquire event.", e);
}
}
}
@@ -530,8 +530,8 @@ public class LockRegistryLeaderInitiator implements SmartLifecycle, DisposableBe
@Override
public void yield() {
if (logger.isDebugEnabled()) {
logger.debug("Yielding leadership from " + this);
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("Yielding leadership from " + this);
}
if (LockRegistryLeaderInitiator.this.future != null) {
LockRegistryLeaderInitiator.this.future.cancel(true);