Skip to content

Commit

Permalink
Sonar Fixes
Browse files Browse the repository at this point in the history
Critical smells, packages `o.s.i.a*` to `f*`.

Plus new smells caused by these changes.
  • Loading branch information
garyrussell authored and artembilan committed Dec 3, 2018
1 parent 4ade0ce commit 62fc7df
Show file tree
Hide file tree
Showing 45 changed files with 188 additions and 140 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ subprojects { subproject ->
romeToolsVersion = '1.9.0'
servletApiVersion = '4.0.0'
smackVersion = '4.3.1'
springAmqpVersion = project.hasProperty('springAmqpVersion') ? project.springAmqpVersion : '2.1.2.RELEASE'
springAmqpVersion = project.hasProperty('springAmqpVersion') ? project.springAmqpVersion : '2.1.3.BUILD-SNAPSHOT'
springDataJpaVersion = '2.1.3.RELEASE'
springDataMongoVersion = '2.1.3.RELEASE'
springDataRedisVersion = '2.1.3.RELEASE'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,14 +288,9 @@ public void onMessage(org.springframework.amqp.core.Message message) {
Message<?> messageToSend = null;
try {
Object converted = this.converter.fromMessage(message);
if (converted != null) {
messageToSend = (converted instanceof Message<?>) ? (Message<?>) converted
: buildMessage(message, converted);
this.dispatcher.dispatch(messageToSend);
}
else if (this.logger.isWarnEnabled()) {
this.logger.warn("MessageConverter returned null, no Message to dispatch");
}
messageToSend = (converted instanceof Message<?>) ? (Message<?>) converted
: buildMessage(message, converted);
this.dispatcher.dispatch(messageToSend);
}
catch (MessageDispatchingException e) {
String exceptionMessage = e.getMessage() + " for amqp-channel '"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import org.springframework.integration.amqp.channel.PublishSubscribeAmqpChannel;
import org.springframework.integration.amqp.support.AmqpHeaderMapper;
import org.springframework.integration.amqp.support.DefaultAmqpHeaderMapper;
import org.springframework.lang.Nullable;
import org.springframework.messaging.support.ChannelInterceptor;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.interceptor.TransactionAttribute;
Expand Down Expand Up @@ -152,7 +153,7 @@ public AmqpChannelFactoryBean(boolean messageDriven) {


@Override
public void setBeanName(String name) {
public void setBeanName(@Nullable String name) {
this.beanName = name;
}

Expand Down Expand Up @@ -399,8 +400,8 @@ protected AbstractAmqpChannel createInstance() throws Exception {
this.channel.setInterceptors(this.interceptors);
}
this.channel.setBeanName(this.beanName);
if (this.getBeanFactory() != null) {
this.channel.setBeanFactory(this.getBeanFactory());
if (getBeanFactory() != null) {
this.channel.setBeanFactory(getBeanFactory()); // NOSONAR never null
}
if (this.defaultDeliveryMode != null) {
this.channel.setDefaultDeliveryMode(this.defaultDeliveryMode);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 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.
Expand Down Expand Up @@ -126,7 +126,7 @@ private MessageChannel resolveErrorChannel(Throwable t) {
Message<?> failedMessage = (actualThrowable instanceof MessagingException) ?
((MessagingException) actualThrowable).getFailedMessage() : null;
if (getDefaultErrorChannel() == null && getChannelResolver() != null) {
setChannel(getChannelResolver().resolveDestination(
setChannel(getChannelResolver().resolveDestination(// NOSONAR not null
IntegrationContextUtils.ERROR_CHANNEL_BEAN_NAME));
}

Expand All @@ -137,11 +137,11 @@ private MessageChannel resolveErrorChannel(Throwable t) {
if (errorChannelHeader instanceof MessageChannel) {
return (MessageChannel) errorChannelHeader;
}
Assert.isInstanceOf(String.class, errorChannelHeader,
Assert.isInstanceOf(String.class, errorChannelHeader, () ->
"Unsupported error channel header type. Expected MessageChannel or String, but actual type is [" +
errorChannelHeader.getClass() + "]");
errorChannelHeader.getClass() + "]"); // NOSONAR never null here
if (getChannelResolver() != null) {
return getChannelResolver().resolveDestination((String) errorChannelHeader);
return getChannelResolver().resolveDestination((String) errorChannelHeader); // NOSONAR not null
}
else {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import org.springframework.integration.core.MessageSelector;
import org.springframework.integration.support.management.QueueChannelManagement;
import org.springframework.lang.Nullable;
import org.springframework.messaging.Message;
import org.springframework.util.Assert;

Expand Down Expand Up @@ -116,19 +117,7 @@ protected Message<?> doReceive(long timeout) {
return ((BlockingQueue<Message<?>>) this.queue).poll(timeout, TimeUnit.MILLISECONDS);
}
else {
Message<?> message = this.queue.poll();
if (message == null) {
long nanos = TimeUnit.MILLISECONDS.toNanos(timeout);
long deadline = System.nanoTime() + nanos;
while (message == null && nanos > 0) {
this.queueSemaphore.tryAcquire(nanos, TimeUnit.NANOSECONDS); // NOSONAR ok to ignore result
message = this.queue.poll();
if (message == null) {
nanos = deadline - System.nanoTime();
}
}
}
return message;
return pollNonBlockingQueue(timeout);
}
}
if (timeout == 0) {
Expand All @@ -141,7 +130,7 @@ protected Message<?> doReceive(long timeout) {
else {
Message<?> message = this.queue.poll();
while (message == null) {
this.queueSemaphore.tryAcquire(50, TimeUnit.MILLISECONDS);
this.queueSemaphore.tryAcquire(50, TimeUnit.MILLISECONDS); // NOSONAR ok to ignore result
message = this.queue.poll();
}
return message;
Expand All @@ -153,6 +142,23 @@ protected Message<?> doReceive(long timeout) {
}
}

@Nullable
private Message<?> pollNonBlockingQueue(long timeout) throws InterruptedException {
Message<?> message = this.queue.poll();
if (message == null) {
long nanos = TimeUnit.MILLISECONDS.toNanos(timeout);
long deadline = System.nanoTime() + nanos;
while (message == null && nanos > 0) {
this.queueSemaphore.tryAcquire(nanos, TimeUnit.NANOSECONDS); // NOSONAR ok to ignore result
message = this.queue.poll();
if (message == null) {
nanos = deadline - System.nanoTime();
}
}
}
return message;
}

@Override
public List<Message<?>> clear() {
List<Message<?>> clearedMessages = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ private void registerNullChannel() {
else {
nullChannelDefinition =
((BeanDefinitionRegistry) this.beanFactory.getParentBeanFactory())
.getBeanDefinition(IntegrationContextUtils.NULL_CHANNEL_BEAN_NAME);
.getBeanDefinition(IntegrationContextUtils.NULL_CHANNEL_BEAN_NAME); // NOSONAR not null
}
if (!NullChannel.class.getName().equals(nullChannelDefinition.getBeanClassName())) {
throw new IllegalStateException("The bean name '" + IntegrationContextUtils.NULL_CHANNEL_BEAN_NAME
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2014 the original author or authors.
* Copyright 2014-2018 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.
Expand Down Expand Up @@ -38,6 +38,7 @@
* {@link org.springframework.context.annotation.Bean} methods are also processed.
*
* @author Artem Bilan
* @author Gary Russell
* @since 4.0
*/
public class GlobalChannelInterceptorInitializer implements IntegrationConfigurationInitializer {
Expand All @@ -50,14 +51,18 @@ public void initialize(ConfigurableListableBeanFactory beanFactory) throws Beans
BeanDefinition beanDefinition = registry.getBeanDefinition(beanName);
if (beanDefinition instanceof AnnotatedBeanDefinition) {
AnnotationMetadata metadata = ((AnnotatedBeanDefinition) beanDefinition).getMetadata();
Map<String, Object> annotationAttributes = metadata.getAnnotationAttributes(GlobalChannelInterceptor.class.getName());
if (CollectionUtils.isEmpty(annotationAttributes) && beanDefinition.getSource() instanceof MethodMetadata) {
Map<String, Object> annotationAttributes = metadata
.getAnnotationAttributes(GlobalChannelInterceptor.class.getName());
if (CollectionUtils.isEmpty(annotationAttributes)
&& beanDefinition.getSource() instanceof MethodMetadata) {
MethodMetadata beanMethod = (MethodMetadata) beanDefinition.getSource();
annotationAttributes = beanMethod.getAnnotationAttributes(GlobalChannelInterceptor.class.getName());
annotationAttributes =
beanMethod.getAnnotationAttributes(GlobalChannelInterceptor.class.getName()); // NOSONAR not null
}

if (!CollectionUtils.isEmpty(annotationAttributes)) {
BeanDefinitionBuilder builder = BeanDefinitionBuilder.genericBeanDefinition(GlobalChannelInterceptorWrapper.class)
BeanDefinitionBuilder builder = BeanDefinitionBuilder
.genericBeanDefinition(GlobalChannelInterceptorWrapper.class)
.addConstructorArgReference(beanName)
.addPropertyValue("patterns", annotationAttributes.get("patterns"))
.addPropertyValue("order", annotationAttributes.get("order"));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2018 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.
Expand Down Expand Up @@ -48,6 +48,7 @@ public final class IdGeneratorConfigurer implements ApplicationListener<Applicat

private final Log logger = LogFactory.getLog(getClass());

@Override
public synchronized void onApplicationEvent(ApplicationContextEvent event) {
ApplicationContext context = event.getApplicationContext();
if (event instanceof ContextRefreshedEvent) {
Expand Down Expand Up @@ -75,7 +76,7 @@ private boolean setIdGenerator(ApplicationContext context) {
this.logger.debug("using custom MessageHeaders.IdGenerator [" + idGeneratorBean.getClass() + "]");
}
Field idGeneratorField = ReflectionUtils.findField(MessageHeaders.class, "idGenerator");
ReflectionUtils.makeAccessible(idGeneratorField);
ReflectionUtils.makeAccessible(idGeneratorField); // NOSONAR never null
IdGenerator currentIdGenerator = (IdGenerator) ReflectionUtils.getField(idGeneratorField, null);
if (currentIdGenerator != null) {
if (currentIdGenerator.equals(idGeneratorBean)) {
Expand Down Expand Up @@ -128,7 +129,7 @@ else if (this.logger.isDebugEnabled()) {
private void unsetIdGenerator() {
try {
Field idGeneratorField = ReflectionUtils.findField(MessageHeaders.class, "idGenerator");
ReflectionUtils.makeAccessible(idGeneratorField);
ReflectionUtils.makeAccessible(idGeneratorField); // NOSONAR never null
idGeneratorField.set(null, null);
IdGeneratorConfigurer.theIdGenerator = null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ else if (beanDefinition instanceof AnnotatedBeanDefinition) {
if (beanDefinition.getSource() instanceof MethodMetadata) {
MethodMetadata beanMethod = (MethodMetadata) beanDefinition.getSource();
String annotationType = IdempotentReceiver.class.getName();
if (beanMethod.isAnnotated(annotationType)) {
Object value = beanMethod.getAnnotationAttributes(annotationType).get("value");
if (beanMethod.isAnnotated(annotationType)) { // NOSONAR never null
Object value = beanMethod.getAnnotationAttributes(annotationType).get("value"); // NOSONAR
if (value != null) {

Class<?> returnType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void initialize(ConfigurableListableBeanFactory beanFactory) throws Beans

if (!hasIntegrationConverter && beanDefinition.getSource() instanceof MethodMetadata) {
MethodMetadata beanMethod = (MethodMetadata) beanDefinition.getSource();
hasIntegrationConverter = beanMethod.isAnnotated(IntegrationConverter.class.getName());
hasIntegrationConverter = beanMethod.isAnnotated(IntegrationConverter.class.getName()); // NOSONAR never null
}

if (hasIntegrationConverter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, B
if (propertyValue != null) {
@SuppressWarnings("unchecked")
Set<Object> currentComponentNamePatternsSet = (Set<Object>) propertyValue.getValue();
currentComponentNamePatternsSet.add(componentNamePatterns);
currentComponentNamePatternsSet.add(componentNamePatterns); // NOSONAR never null
}
else {
Set<Object> componentNamePatternsSet = new ManagedSet<Object>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
import org.springframework.integration.scheduling.PollerMetadata;
import org.springframework.integration.support.channel.BeanFactoryChannelResolver;
import org.springframework.integration.util.MessagingAnnotationUtils;
import org.springframework.lang.Nullable;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.MessageHandler;
import org.springframework.messaging.PollableChannel;
Expand Down Expand Up @@ -195,7 +196,7 @@ public Object postProcess(Object bean, String beanName, Method method, List<Anno

if (AnnotatedElementUtils.isAnnotated(method, IdempotentReceiver.class.getName())
&& !AnnotatedElementUtils.isAnnotated(method, Bean.class.getName())) {
String[] interceptors = AnnotationUtils.getAnnotation(method, IdempotentReceiver.class).value();
String[] interceptors = AnnotationUtils.getAnnotation(method, IdempotentReceiver.class).value(); // NOSONAR never null
for (String interceptor : interceptors) {
DefaultBeanFactoryPointcutAdvisor advisor = new DefaultBeanFactoryPointcutAdvisor();
advisor.setAdviceBeanName(interceptor);
Expand Down Expand Up @@ -469,15 +470,15 @@ protected Object resolveTargetBeanFromMethodWithBeanAnnotation(Method method) {

protected String resolveTargetBeanName(Method method) {
String id = method.getName();
String[] names = AnnotationUtils.getAnnotation(method, Bean.class).name();
String[] names = AnnotationUtils.getAnnotation(method, Bean.class).name(); // NOSONAR never null
if (!ObjectUtils.isEmpty(names)) {
id = names[0];
}
return id;
}

@SuppressWarnings("unchecked")
protected <H> H extractTypeIfPossible(Object targetObject, Class<H> expectedType) {
protected <H> H extractTypeIfPossible(@Nullable Object targetObject, Class<H> expectedType) {
if (targetObject == null) {
return null;
}
Expand All @@ -486,9 +487,6 @@ protected <H> H extractTypeIfPossible(Object targetObject, Class<H> expectedType
}
if (targetObject instanceof Advised) {
TargetSource targetSource = ((Advised) targetObject).getTargetSource();
if (targetSource == null) {
return null;
}
try {
return extractTypeIfPossible(targetSource.getTarget(), expectedType);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2017 the original author or authors.
* Copyright 2002-2018 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.
Expand Down Expand Up @@ -156,8 +156,8 @@ protected final AbstractBeanDefinition parseInternal(Element element, ParserCont

@SuppressWarnings("unchecked")
Collection<String> channelCandidateNames =
(Collection<String>) caValues.getArgumentValue(0, Collection.class).getValue();
channelCandidateNames.add(inputChannelName);
(Collection<String>) caValues.getArgumentValue(0, Collection.class).getValue(); // NOSONAR see comment above
channelCandidateNames.add(inputChannelName); // NOSONAR
}
else {
parserContext.getReaderContext().error("Failed to locate '" +
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2018 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.
Expand Down Expand Up @@ -49,6 +49,7 @@
* @author Oleg Zhurakousky
* @author Artem Bilan
* @author Gunnar Hillert
* @author Gary Russell
*/
public class ChainParser extends AbstractConsumerEndpointParser {

Expand Down Expand Up @@ -147,7 +148,7 @@ private BeanMetadataElement parseChild(String chainHandlerId, Element element, i
}
}

holder.getBeanDefinition().getPropertyValues().add("componentName", handlerComponentName);
holder.getBeanDefinition().getPropertyValues().add("componentName", handlerComponentName); // NOSONAR never null

if (hasId) {
BeanDefinitionReaderUtils.registerBeanDefinition(holder, parserContext.getRegistry());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2008 the original author or authors.
* Copyright 2002-2018 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.
Expand Down Expand Up @@ -36,6 +36,7 @@
*
* @author Mark Fisher
* @author Oleg Zhurakousky
* @author Gary Russell
*/
public class ChannelInterceptorParser {

Expand All @@ -60,7 +61,7 @@ public ManagedList parseInterceptors(Element element, ParserContext parserContex
if ("bean".equals(localName)) {
BeanDefinitionParserDelegate delegate = parserContext.getDelegate();
BeanDefinitionHolder holder = delegate.parseBeanDefinitionElement(childElement);
holder = delegate.decorateBeanDefinitionIfRequired(childElement, holder);
holder = delegate.decorateBeanDefinitionIfRequired(childElement, holder); // NOSONAR never null
parserContext.registerBeanComponent(new BeanComponentDefinition(holder));
interceptors.add(new RuntimeBeanReference(holder.getBeanName()));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2016 the original author or authors.
* Copyright 2002-2018 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.
Expand Down Expand Up @@ -97,7 +97,8 @@ else if (delegate.nodeNameEquals(child, BeanDefinitionParserDelegate.REF_ELEMENT
}
else {
BeanDefinition beanDef = delegate.parseCustomElement(child);
beanName = BeanDefinitionReaderUtils.generateBeanName(beanDef, parserContext.getRegistry());
beanName = BeanDefinitionReaderUtils.generateBeanName(beanDef, // NOSONAR never null
parserContext.getRegistry());
}
}
}
Expand Down
Loading

0 comments on commit 62fc7df

Please sign in to comment.