Sonar fixes

- a few complexities
- final method calls from ctor
- raw exception throwing
- useless overrides
- loss of stack trace
This commit is contained in:
Gary Russell
2019-04-29 16:34:39 -04:00
committed by Artem Bilan
parent 7569d0ad79
commit 36c33bb5ab
45 changed files with 134 additions and 114 deletions

View File

@@ -32,7 +32,7 @@ public class GlobalChannelInterceptorWrapper implements Ordered {
private final ChannelInterceptor channelInterceptor;
private volatile String[] patterns = new String[]{"*"}; // default
private volatile String[] patterns = { "*" }; // default
private volatile int order = 0;
@@ -57,7 +57,7 @@ public class GlobalChannelInterceptorWrapper implements Ordered {
}
@Override
public int getOrder() {
public final int getOrder() {
return this.order;
}

View File

@@ -43,7 +43,7 @@ public class CompositeKryoRegistrar extends AbstractKryoRegistrar {
}
@Override
public List<Registration> getRegistrations() {
public final List<Registration> getRegistrations() {
List<Registration> registrations = new ArrayList<Registration>();
for (KryoRegistrar registrar : this.delegates) {
registrations.addAll(registrar.getRegistrations());
@@ -59,13 +59,13 @@ public class CompositeKryoRegistrar extends AbstractKryoRegistrar {
Assert.isTrue(registration.getId() >= MIN_REGISTRATION_VALUE,
"registration ID must be >= " + MIN_REGISTRATION_VALUE);
if (ids.contains(registration.getId())) {
throw new RuntimeException(String.format("Duplicate registration ID found: %d",
throw new IllegalArgumentException(String.format("Duplicate registration ID found: %d",
registration.getId()));
}
ids.add(registration.getId());
if (types.contains(registration.getType())) {
throw new RuntimeException(String.format("Duplicate registration found for type: %s",
throw new IllegalArgumentException(String.format("Duplicate registration found for type: %s",
registration.getType()));
}
types.add(registration.getType());

View File

@@ -94,6 +94,12 @@ public class RouterAnnotationPostProcessor extends AbstractMethodAnnotationPostP
router.setIgnoreSendFailures(resolveAttributeToBoolean(ignoreSendFailures));
}
routerAttributes(annotations, router);
return router;
}
private void routerAttributes(List<Annotation> annotations, AbstractMessageRouter router) {
if (routerAttributesProvided(annotations)) {
MethodInvokingRouter methodInvokingRouter = (MethodInvokingRouter) router;
@@ -127,8 +133,6 @@ public class RouterAnnotationPostProcessor extends AbstractMethodAnnotationPostP
}
}
return router;
}
private boolean routerAttributesProvided(List<Annotation> annotations) {

View File

@@ -144,27 +144,7 @@ public abstract class AbstractConsumerEndpointParser extends AbstractBeanDefinit
String inputChannelName = element.getAttribute(inputChannelAttributeName);
if (!parserContext.getRegistry().containsBeanDefinition(inputChannelName)) {
if (parserContext.getRegistry()
.containsBeanDefinition(IntegrationContextUtils.AUTO_CREATE_CHANNEL_CANDIDATES_BEAN_NAME)) {
BeanDefinition channelRegistry = parserContext.getRegistry().
getBeanDefinition(IntegrationContextUtils.AUTO_CREATE_CHANNEL_CANDIDATES_BEAN_NAME);
ConstructorArgumentValues caValues = channelRegistry.getConstructorArgumentValues();
ValueHolder vh = caValues.getArgumentValue(0, Collection.class);
if (vh == null) { //although it should never happen if it does we can fix it
caValues.addIndexedArgumentValue(0, new ManagedSet<String>());
}
@SuppressWarnings("unchecked")
Collection<String> channelCandidateNames =
(Collection<String>) caValues.getArgumentValue(0, Collection.class).getValue(); // NOSONAR see comment above
channelCandidateNames.add(inputChannelName); // NOSONAR
}
else {
parserContext.getReaderContext().error("Failed to locate '" +
IntegrationContextUtils.AUTO_CREATE_CHANNEL_CANDIDATES_BEAN_NAME + "'",
parserContext.getRegistry());
}
registerChannelForCreation(parserContext, inputChannelName);
}
IntegrationNamespaceUtils.checkAndConfigureFixedSubscriberChannel(element, parserContext, inputChannelName,
handlerBeanName);
@@ -188,6 +168,30 @@ public abstract class AbstractConsumerEndpointParser extends AbstractBeanDefinit
return null;
}
private void registerChannelForCreation(ParserContext parserContext, String inputChannelName) {
if (parserContext.getRegistry()
.containsBeanDefinition(IntegrationContextUtils.AUTO_CREATE_CHANNEL_CANDIDATES_BEAN_NAME)) {
BeanDefinition channelRegistry = parserContext.getRegistry().
getBeanDefinition(IntegrationContextUtils.AUTO_CREATE_CHANNEL_CANDIDATES_BEAN_NAME);
ConstructorArgumentValues caValues = channelRegistry.getConstructorArgumentValues();
ValueHolder vh = caValues.getArgumentValue(0, Collection.class);
if (vh == null) { //although it should never happen if it does we can fix it
caValues.addIndexedArgumentValue(0, new ManagedSet<String>());
}
@SuppressWarnings("unchecked")
Collection<String> channelCandidateNames =
(Collection<String>) caValues.getArgumentValue(0, Collection.class).getValue(); // NOSONAR see comment above
channelCandidateNames.add(inputChannelName); // NOSONAR
}
else {
parserContext.getReaderContext().error("Failed to locate '" +
IntegrationContextUtils.AUTO_CREATE_CHANNEL_CANDIDATES_BEAN_NAME + "'",
parserContext.getRegistry());
}
}
/**
* Override to allow 'reply-channel' within a chain, for components where it
* makes sense (e.g. enricher). Default is false for outbound gateways, else true.

View File

@@ -101,6 +101,17 @@ abstract class AbstractDelegatingConsumerEndpointParser extends AbstractConsumer
IntegrationNamespaceUtils.createElementDescription(element) + ".", source);
return null;
}
methodAttribute(element, parserContext, source, builder, innerDefinition, hasRef, hasExpression,
expressionElement);
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "requires-reply");
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "send-timeout");
this.postProcess(builder, element, parserContext);
return builder;
}
private void methodAttribute(Element element, ParserContext parserContext, Object source,
BeanDefinitionBuilder builder, BeanComponentDefinition innerDefinition, boolean hasRef,
boolean hasExpression, Element expressionElement) {
String method = element.getAttribute(METHOD_ATTRIBUTE);
if (StringUtils.hasText(method)) {
if (hasExpression || expressionElement != null) {
@@ -117,10 +128,6 @@ abstract class AbstractDelegatingConsumerEndpointParser extends AbstractConsumer
IntegrationNamespaceUtils.createElementDescription(element) + ".", source);
}
}
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "requires-reply");
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "send-timeout");
this.postProcess(builder, element, parserContext);
return builder;
}
/**

View File

@@ -16,9 +16,6 @@
package org.springframework.integration.dsl;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.config.AbstractFactoryBean;
@@ -42,8 +39,6 @@ public abstract class IntegrationComponentSpec<S extends IntegrationComponentSpe
protected static final SpelExpressionParser PARSER = new SpelExpressionParser();
protected final Log logger = LogFactory.getLog(getClass()); // NOSONAR
protected volatile T target; // NOSONAR
private String id;
@@ -51,11 +46,11 @@ public abstract class IntegrationComponentSpec<S extends IntegrationComponentSpe
/**
* Configure the component identifier. Used as the {@code beanName} to register the
* bean in the application context for this component.
* @param id the id.
* @param idToSet the id.
* @return the spec.
*/
protected S id(String id) {
this.id = id;
protected S id(String idToSet) {
this.id = idToSet;
return _this();
}

View File

@@ -28,7 +28,7 @@ public final class IntegrationFlowBuilder extends IntegrationFlowDefinition<Inte
}
@Override
public StandardIntegrationFlow get() {
public StandardIntegrationFlow get() { // NOSONAR - not useless, increases visibility
return super.get();
}

View File

@@ -57,22 +57,17 @@ public abstract class MessageChannelSpec<S extends MessageChannelSpec<S, C>, C e
super();
}
@Override
protected S id(String id) {
return super.id(id);
}
public S datatype(Class<?>... datatypes) {
Assert.notNull(datatypes, "'datatypes' must not be null");
Assert.noNullElements(datatypes, "'datatypes' must not contain null elements");
this.datatypes.addAll(Arrays.asList(datatypes));
public S datatype(Class<?>... types) {
Assert.notNull(types, "'datatypes' must not be null");
Assert.noNullElements(types, "'datatypes' must not contain null elements");
this.datatypes.addAll(Arrays.asList(types));
return _this();
}
public S interceptor(ChannelInterceptor... interceptors) {
Assert.notNull(interceptors, "'interceptors' must not be null");
Assert.noNullElements(interceptors, "'interceptors' must not contain null elements");
this.interceptors.addAll(Arrays.asList(interceptors));
public S interceptor(ChannelInterceptor... interceptorArray) {
Assert.notNull(interceptorArray, "'interceptorArray' must not be null");
Assert.noNullElements(interceptorArray, "'interceptorArray' must not contain null elements");
this.interceptors.addAll(Arrays.asList(interceptorArray));
return _this();
}
@@ -111,8 +106,8 @@ public abstract class MessageChannelSpec<S extends MessageChannelSpec<S, C>, C e
return interceptor(interceptor);
}
public S messageConverter(MessageConverter messageConverter) {
this.messageConverter = messageConverter;
public S messageConverter(MessageConverter converter) {
this.messageConverter = converter;
return _this();
}

View File

@@ -41,7 +41,7 @@ public class PublishSubscribeSpec extends PublishSubscribeChannelSpec<PublishSub
}
@Override
public PublishSubscribeSpec id(String id) {
public PublishSubscribeSpec id(String id) { // NOSONAR - not useless, increases visibility
return super.id(id);
}

View File

@@ -179,7 +179,7 @@ public class FunctionExpression<S> implements Expression {
}
@Override
public String getExpressionString() {
public final String getExpressionString() {
return this.function.toString();
}

View File

@@ -178,7 +178,7 @@ public class SupplierExpression<T> implements Expression {
}
@Override
public String getExpressionString() {
public final String getExpressionString() {
return this.supplier.toString();
}

View File

@@ -39,7 +39,7 @@ public abstract class IntegrationNode {
private final int nodeId;
private final String name;
private final String nodeName;
private final Stats stats;
@@ -51,7 +51,7 @@ public abstract class IntegrationNode {
protected IntegrationNode(int nodeId, String name, Object nodeObject, Stats stats) {
this.nodeId = nodeId;
this.name = name;
this.nodeName = name;
this.componentType =
nodeObject instanceof NamedComponent
? ((NamedComponent) nodeObject).getComponentType()
@@ -70,10 +70,10 @@ public abstract class IntegrationNode {
}
public String getName() {
return this.name;
return this.nodeName;
}
public String getComponentType() {
public final String getComponentType() {
return this.componentType;
}
@@ -98,12 +98,12 @@ public abstract class IntegrationNode {
/**
* Add extra property to the node.
* @param properties additional properties to add
* @param props additional properties to add
* @since 5.1
*/
public void addProperties(@Nullable Map<String, Object> properties) {
if (properties != null) {
this.properties.putAll(properties);
public void addProperties(@Nullable Map<String, Object> props) {
if (props != null) {
this.properties.putAll(props);
}
}

View File

@@ -106,7 +106,7 @@ public class LambdaMessageProcessor implements MessageProcessor<Object>, BeanFac
"An example of when this often occurs is if the lambda is configured to " +
"receive a Message<?> argument.", e.getCause());
}
throw new MessageHandlingException(message, e.getCause());
throw new MessageHandlingException(message, e.getCause()); // NOSONAR lost stack trace
}
catch (Exception e) {
throw IntegrationUtils.wrapInHandlingExceptionIfNecessary(message,

View File

@@ -84,10 +84,10 @@ public class LoggingHandler extends AbstractMessageHandler {
try {
return Level.valueOf(level.toUpperCase());
}
catch (IllegalArgumentException e) {
catch (@SuppressWarnings("unused") IllegalArgumentException e) {
throw new IllegalArgumentException("Invalid log level '" + level
+ "'. The (case-insensitive) supported values are: "
+ StringUtils.arrayToCommaDelimitedString(Level.values()));
+ StringUtils.arrayToCommaDelimitedString(Level.values())); // NOSONAR lost stack trace
}
}

View File

@@ -50,7 +50,7 @@ public class MutableMessageHeaders extends MessageHeaders {
}
@Override
protected Map<String, Object> getRawHeaders() {
protected Map<String, Object> getRawHeaders() { // NOSONAR - not useless; increases visibility
return super.getRawHeaders();
}

View File

@@ -75,7 +75,7 @@ public class SmartLifecycleRoleController implements ApplicationListener<Abstrac
public SmartLifecycleRoleController(List<String> roles, List<SmartLifecycle> lifecycles) {
Assert.notNull(roles, "'roles' cannot be null");
Assert.notNull(lifecycles, "'lifecycles' cannot be null");
Assert.isTrue(roles.size() == lifecycles.size(), "'roles' and 'lifecycles' must be the same lenght");
Assert.isTrue(roles.size() == lifecycles.size(), "'roles' and 'lifecycles' must be the same length");
Iterator<SmartLifecycle> iterator = lifecycles.iterator();
for (String role : roles) {
SmartLifecycle lifecycle = iterator.next();
@@ -134,7 +134,7 @@ public class SmartLifecycleRoleController implements ApplicationListener<Abstrac
* @param role the role.
* @param lifecycleBeanName the bean name of the {@link SmartLifecycle}.
*/
public void addLifecycleToRole(String role, String lifecycleBeanName) {
public final void addLifecycleToRole(String role, String lifecycleBeanName) {
Assert.state(this.applicationContext != null, "An application context is required to use this method");
this.lazyLifecycles.add(role, lifecycleBeanName);
}

View File

@@ -96,7 +96,7 @@ public final class IntegrationUtils {
logger.debug("No 'beanFactory' supplied; cannot find MessageBuilderFactory, using default.");
}
if (fatalWhenNoBeanFactory) {
throw new RuntimeException("All Message creators need a BeanFactory");
throw new IllegalStateException("All Message creators need a BeanFactory");
}
}
if (messageBuilderFactory == null) {

View File

@@ -488,7 +488,7 @@ public class ContentEnricher extends AbstractReplyProducingMessageHandler implem
}
@Override
protected Message<?> sendAndReceiveMessage(Object object) {
protected Message<?> sendAndReceiveMessage(Object object) { // NOSONAR - not useless, increases visibility
return super.sendAndReceiveMessage(object);
}

View File

@@ -29,6 +29,7 @@ import org.springframework.util.ClassUtils;
* Utility to help generate UUID instances from generic objects.
*
* @author Dave Syer
* @author Gary Russell
*/
public class UUIDConverter implements Converter<Object, UUID> {
@@ -41,6 +42,7 @@ public class UUIDConverter implements Converter<Object, UUID> {
*
* @see org.springframework.core.convert.converter.Converter#convert(java.lang.Object)
*/
@Override
public UUID convert(Object source) {
return getUUID(source);
}
@@ -80,7 +82,10 @@ public class UUIDConverter implements Converter<Object, UUID> {
return UUID.nameUUIDFromBytes(((String) input).getBytes(DEFAULT_CHARSET));
}
catch (UnsupportedEncodingException ex) {
throw new IllegalStateException("Cannot convert String using charset=" + DEFAULT_CHARSET, ex);
IllegalStateException exception =
new IllegalStateException("Cannot convert String using charset=" + DEFAULT_CHARSET, ex);
exception.addSuppressed(e);
throw exception; // NOSONAR - added to suppressed exceptions
}
}
}