Fix some Sonar smells

This commit is contained in:
Artem Bilan
2020-08-20 16:08:18 -04:00
parent 8398d9c7e7
commit 750d721437
16 changed files with 572 additions and 547 deletions

View File

@@ -89,7 +89,7 @@ public class BeanFactoryChannelResolver implements DestinationResolver<MessageCh
return this.beanFactory.getBean(name, MessageChannel.class);
}
catch (BeansException e) {
if (!(e instanceof NoSuchBeanDefinitionException)) {
if (!(e instanceof NoSuchBeanDefinitionException)) { // NOSONAR
throw new DestinationResolutionException("A bean definition with name '"
+ name + "' exists, but failed to be created", e);
}

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.
@@ -28,6 +28,7 @@ import org.springframework.core.convert.TypeDescriptor;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.expression.TypeConverter;
import org.springframework.integration.history.MessageHistory;
import org.springframework.lang.Nullable;
import org.springframework.messaging.MessageHeaders;
import org.springframework.util.ClassUtils;
@@ -36,15 +37,16 @@ import org.springframework.util.ClassUtils;
* @author Oleg Zhurakousky
* @author Gary Russell
* @author Soby Chacko
* @author Artem Bilan
*/
public class BeanFactoryTypeConverter implements TypeConverter, BeanFactoryAware {
private volatile SimpleTypeConverter delegate = new SimpleTypeConverter();
private SimpleTypeConverter delegate = new SimpleTypeConverter();
private ConversionService conversionService;
private volatile boolean haveCalledDelegateGetDefaultEditor;
private volatile ConversionService conversionService;
public BeanFactoryTypeConverter() {
this.conversionService = DefaultConversionService.getSharedInstance();
@@ -109,33 +111,24 @@ public class BeanFactoryTypeConverter implements TypeConverter, BeanFactoryAware
Class<?> sourceClass = sourceType.getType();
Class<?> targetClass = targetType.getType();
if ((sourceClass == MessageHeaders.class && targetClass == MessageHeaders.class) || // NOSONAR
(sourceClass == MessageHistory.class && targetClass == MessageHistory.class) ||
(sourceType.isAssignableTo(targetType) && ClassUtils.isPrimitiveArray(sourceClass))) {
(sourceClass == MessageHistory.class && targetClass == MessageHistory.class) ||
(sourceType.isAssignableTo(targetType) && ClassUtils.isPrimitiveArray(sourceClass))) {
return value;
}
}
if (this.conversionService.canConvert(sourceType, targetType)) {
return this.conversionService.convert(value, sourceType, targetType);
}
if (!String.class.isAssignableFrom(sourceType.getType())) {
PropertyEditor editor = this.delegate.findCustomEditor(sourceType.getType(), null);
if (editor == null) {
editor = this.getDefaultEditor(sourceType.getType());
}
if (editor != null) { // INT-1441
String text = null;
synchronized (editor) {
editor.setValue(value);
text = editor.getAsText();
}
if (String.class.isAssignableFrom(targetType.getType())) {
return text;
}
return convertValue(text, TypeDescriptor.valueOf(String.class), targetType);
Object editorResult = valueFromEditorIfAny(value, sourceType.getType(), targetType);
if (editorResult == null) {
synchronized (this.delegate) {
return this.delegate.convertIfNecessary(value, targetType.getType());
}
}
synchronized (this.delegate) {
return this.delegate.convertIfNecessary(value, targetType.getType());
else {
return editorResult;
}
}
@@ -154,4 +147,28 @@ public class BeanFactoryTypeConverter implements TypeConverter, BeanFactoryAware
return defaultEditor;
}
@Nullable
private Object valueFromEditorIfAny(Object value, Class<?> sourceClass, TypeDescriptor targetType) {
if (!String.class.isAssignableFrom(sourceClass)) {
PropertyEditor editor = this.delegate.findCustomEditor(sourceClass, null);
if (editor == null) {
editor = getDefaultEditor(sourceClass);
}
if (editor != null) { // INT-1441
String text;
synchronized (editor) {
editor.setValue(value);
text = editor.getAsText();
}
if (String.class.isAssignableFrom(targetType.getType())) {
return text;
}
return convertValue(text, TypeDescriptor.valueOf(String.class), targetType);
}
}
return null;
}
}