Skip to content

Commit 750d721

Browse files
committed
Fix some Sonar smells
1 parent 8398d9c commit 750d721

File tree

16 files changed

+527
-502
lines changed

16 files changed

+527
-502
lines changed

spring-integration-core/src/main/java/org/springframework/integration/support/channel/BeanFactoryChannelResolver.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public MessageChannel resolveDestination(String name) {
8989
return this.beanFactory.getBean(name, MessageChannel.class);
9090
}
9191
catch (BeansException e) {
92-
if (!(e instanceof NoSuchBeanDefinitionException)) {
92+
if (!(e instanceof NoSuchBeanDefinitionException)) { // NOSONAR
9393
throw new DestinationResolutionException("A bean definition with name '"
9494
+ name + "' exists, but failed to be created", e);
9595
}

spring-integration-core/src/main/java/org/springframework/integration/util/BeanFactoryTypeConverter.java

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,6 +28,7 @@
2828
import org.springframework.core.convert.support.DefaultConversionService;
2929
import org.springframework.expression.TypeConverter;
3030
import org.springframework.integration.history.MessageHistory;
31+
import org.springframework.lang.Nullable;
3132
import org.springframework.messaging.MessageHeaders;
3233
import org.springframework.util.ClassUtils;
3334

@@ -36,14 +37,15 @@
3637
* @author Oleg Zhurakousky
3738
* @author Gary Russell
3839
* @author Soby Chacko
40+
* @author Artem Bilan
3941
*/
4042
public class BeanFactoryTypeConverter implements TypeConverter, BeanFactoryAware {
4143

42-
private volatile SimpleTypeConverter delegate = new SimpleTypeConverter();
44+
private SimpleTypeConverter delegate = new SimpleTypeConverter();
4345

44-
private volatile boolean haveCalledDelegateGetDefaultEditor;
46+
private ConversionService conversionService;
4547

46-
private volatile ConversionService conversionService;
48+
private volatile boolean haveCalledDelegateGetDefaultEditor;
4749

4850

4951
public BeanFactoryTypeConverter() {
@@ -109,33 +111,24 @@ public Object convertValue(Object value, TypeDescriptor sourceType, TypeDescript
109111
Class<?> sourceClass = sourceType.getType();
110112
Class<?> targetClass = targetType.getType();
111113
if ((sourceClass == MessageHeaders.class && targetClass == MessageHeaders.class) || // NOSONAR
112-
(sourceClass == MessageHistory.class && targetClass == MessageHistory.class) ||
113-
(sourceType.isAssignableTo(targetType) && ClassUtils.isPrimitiveArray(sourceClass))) {
114+
(sourceClass == MessageHistory.class && targetClass == MessageHistory.class) ||
115+
(sourceType.isAssignableTo(targetType) && ClassUtils.isPrimitiveArray(sourceClass))) {
114116
return value;
115117
}
116118
}
117119
if (this.conversionService.canConvert(sourceType, targetType)) {
118120
return this.conversionService.convert(value, sourceType, targetType);
119121
}
120-
if (!String.class.isAssignableFrom(sourceType.getType())) {
121-
PropertyEditor editor = this.delegate.findCustomEditor(sourceType.getType(), null);
122-
if (editor == null) {
123-
editor = this.getDefaultEditor(sourceType.getType());
124-
}
125-
if (editor != null) { // INT-1441
126-
String text = null;
127-
synchronized (editor) {
128-
editor.setValue(value);
129-
text = editor.getAsText();
130-
}
131-
if (String.class.isAssignableFrom(targetType.getType())) {
132-
return text;
133-
}
134-
return convertValue(text, TypeDescriptor.valueOf(String.class), targetType);
122+
123+
Object editorResult = valueFromEditorIfAny(value, sourceType.getType(), targetType);
124+
125+
if (editorResult == null) {
126+
synchronized (this.delegate) {
127+
return this.delegate.convertIfNecessary(value, targetType.getType());
135128
}
136129
}
137-
synchronized (this.delegate) {
138-
return this.delegate.convertIfNecessary(value, targetType.getType());
130+
else {
131+
return editorResult;
139132
}
140133
}
141134

@@ -154,4 +147,28 @@ private PropertyEditor getDefaultEditor(Class<?> sourceType) {
154147
return defaultEditor;
155148
}
156149

150+
@Nullable
151+
private Object valueFromEditorIfAny(Object value, Class<?> sourceClass, TypeDescriptor targetType) {
152+
if (!String.class.isAssignableFrom(sourceClass)) {
153+
PropertyEditor editor = this.delegate.findCustomEditor(sourceClass, null);
154+
if (editor == null) {
155+
editor = getDefaultEditor(sourceClass);
156+
}
157+
if (editor != null) { // INT-1441
158+
String text;
159+
synchronized (editor) {
160+
editor.setValue(value);
161+
text = editor.getAsText();
162+
}
163+
164+
if (String.class.isAssignableFrom(targetType.getType())) {
165+
return text;
166+
}
167+
168+
return convertValue(text, TypeDescriptor.valueOf(String.class), targetType);
169+
}
170+
}
171+
return null;
172+
}
173+
157174
}

spring-integration-http/src/main/java/org/springframework/integration/http/config/HttpInboundEndpointParser.java

Lines changed: 73 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -84,18 +84,7 @@ protected String resolveId(Element element, AbstractBeanDefinition definition, P
8484
@Override
8585
protected void doParse(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
8686
builder.addConstructorArgValue(this.expectReply);
87-
String inputChannelAttributeName = this.getInputChannelAttributeName();
88-
String inputChannelRef = element.getAttribute(inputChannelAttributeName);
89-
if (!StringUtils.hasText(inputChannelRef)) {
90-
if (this.expectReply) {
91-
parserContext.getReaderContext().error(
92-
"a '" + inputChannelAttributeName + "' reference is required", element);
93-
}
94-
else {
95-
inputChannelRef = IntegrationNamespaceUtils.createDirectChannel(element, parserContext);
96-
}
97-
}
98-
builder.addPropertyReference("requestChannel", inputChannelRef);
87+
parseInputChannel(element, parserContext, builder);
9988
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "error-channel");
10089

10190
BeanDefinition payloadExpressionDef =
@@ -104,22 +93,7 @@ protected void doParse(Element element, ParserContext parserContext, BeanDefinit
10493
builder.addPropertyValue("payloadExpression", payloadExpressionDef);
10594
}
10695

107-
List<Element> headerElements = DomUtils.getChildElementsByTagName(element, "header");
108-
109-
if (!CollectionUtils.isEmpty(headerElements)) {
110-
ManagedMap<String, Object> headerElementsMap = new ManagedMap<>();
111-
for (Element headerElement : headerElements) {
112-
String name = headerElement.getAttribute(NAME_ATTRIBUTE);
113-
BeanDefinition headerExpressionDef =
114-
IntegrationNamespaceUtils
115-
.createExpressionDefIfAttributeDefined(IntegrationNamespaceUtils.EXPRESSION_ATTRIBUTE,
116-
headerElement);
117-
if (headerExpressionDef != null) {
118-
headerElementsMap.put(name, headerExpressionDef);
119-
}
120-
}
121-
builder.addPropertyValue("headerExpressions", headerElementsMap);
122-
}
96+
parseHeaders(element, builder);
12397

12498
if (this.expectReply) {
12599
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "reply-channel");
@@ -145,19 +119,84 @@ protected void doParse(Element element, ParserContext parserContext, BeanDefinit
145119
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "message-converters");
146120
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "merge-with-default-converters");
147121

148-
String headerMapper = element.getAttribute("header-mapper");
149122

123+
parseHeaderMapper(element, parserContext, builder);
124+
125+
BeanDefinition requestMappingDef = createRequestMapping(element);
126+
builder.addPropertyValue("requestMapping", requestMappingDef);
127+
128+
129+
parseCrossOrigin(element, builder);
130+
131+
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element,
132+
"request-payload-type", "requestPayloadTypeClass");
133+
134+
BeanDefinition statusCodeExpressionDef =
135+
IntegrationNamespaceUtils.createExpressionDefIfAttributeDefined("status-code-expression", element);
136+
if (statusCodeExpressionDef == null) {
137+
statusCodeExpressionDef = IntegrationNamespaceUtils
138+
.createExpressionDefIfAttributeDefined("reply-timeout-status-code-expression", element);
139+
}
140+
if (statusCodeExpressionDef != null) {
141+
builder.addPropertyValue("statusCodeExpression", statusCodeExpressionDef);
142+
}
143+
144+
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, IntegrationNamespaceUtils.AUTO_STARTUP);
145+
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, IntegrationNamespaceUtils.PHASE);
146+
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "validator");
147+
}
148+
149+
private void parseInputChannel(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
150+
String inputChannelAttributeName = this.getInputChannelAttributeName();
151+
String inputChannelRef = element.getAttribute(inputChannelAttributeName);
152+
if (!StringUtils.hasText(inputChannelRef)) {
153+
if (this.expectReply) {
154+
parserContext.getReaderContext().error(
155+
"a '" + inputChannelAttributeName + "' reference is required", element);
156+
}
157+
else {
158+
inputChannelRef = IntegrationNamespaceUtils.createDirectChannel(element, parserContext);
159+
}
160+
}
161+
builder.addPropertyReference("requestChannel", inputChannelRef);
162+
}
163+
164+
private String getInputChannelAttributeName() {
165+
return this.expectReply ? "request-channel" : "channel";
166+
}
167+
168+
private void parseHeaders(Element element, BeanDefinitionBuilder builder) {
169+
List<Element> headerElements = DomUtils.getChildElementsByTagName(element, "header");
170+
171+
if (!CollectionUtils.isEmpty(headerElements)) {
172+
ManagedMap<String, Object> headerElementsMap = new ManagedMap<>();
173+
for (Element headerElement : headerElements) {
174+
String name = headerElement.getAttribute(NAME_ATTRIBUTE);
175+
BeanDefinition headerExpressionDef =
176+
IntegrationNamespaceUtils
177+
.createExpressionDefIfAttributeDefined(IntegrationNamespaceUtils.EXPRESSION_ATTRIBUTE,
178+
headerElement);
179+
if (headerExpressionDef != null) {
180+
headerElementsMap.put(name, headerExpressionDef);
181+
}
182+
}
183+
builder.addPropertyValue("headerExpressions", headerElementsMap);
184+
}
185+
}
186+
187+
private void parseHeaderMapper(Element element, ParserContext parserContext, BeanDefinitionBuilder builder) {
150188
String mappedRequestHeaders = element.getAttribute("mapped-request-headers");
151189
String mappedResponseHeaders = element.getAttribute("mapped-response-headers");
152190

153191
boolean hasMappedRequestHeaders = StringUtils.hasText(mappedRequestHeaders);
154192
boolean hasMappedResponseHeaders = StringUtils.hasText(mappedResponseHeaders);
155193

194+
String headerMapper = element.getAttribute("header-mapper");
156195
if (StringUtils.hasText(headerMapper)) {
157196
if (hasMappedRequestHeaders || hasMappedResponseHeaders) {
158197
parserContext.getReaderContext()
159198
.error("Neither 'mapped-request-headers' or 'mapped-response-headers' " +
160-
"attributes are allowed when a 'header-mapper' has been specified.",
199+
"attributes are allowed when a 'header-mapper' has been specified.",
161200
parserContext.extractSource(element));
162201
}
163202
builder.addPropertyReference("headerMapper", headerMapper);
@@ -176,11 +215,9 @@ protected void doParse(Element element, ParserContext parserContext, BeanDefinit
176215

177216
builder.addPropertyValue("headerMapper", headerMapperBuilder.getBeanDefinition());
178217
}
218+
}
179219

180-
BeanDefinition requestMappingDef = createRequestMapping(element);
181-
builder.addPropertyValue("requestMapping", requestMappingDef);
182-
183-
220+
private void parseCrossOrigin(Element element, BeanDefinitionBuilder builder) {
184221
Element crossOriginElement = DomUtils.getChildElementByTagName(element, "cross-origin");
185222
if (crossOriginElement != null) {
186223
BeanDefinitionBuilder crossOriginBuilder =
@@ -194,27 +231,6 @@ protected void doParse(Element element, ParserContext parserContext, BeanDefinit
194231
"allow-credentials", true);
195232
builder.addPropertyValue("crossOrigin", crossOriginBuilder.getBeanDefinition());
196233
}
197-
198-
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element,
199-
"request-payload-type", "requestPayloadTypeClass");
200-
201-
BeanDefinition statusCodeExpressionDef =
202-
IntegrationNamespaceUtils.createExpressionDefIfAttributeDefined("status-code-expression", element);
203-
if (statusCodeExpressionDef == null) {
204-
statusCodeExpressionDef = IntegrationNamespaceUtils
205-
.createExpressionDefIfAttributeDefined("reply-timeout-status-code-expression", element);
206-
}
207-
if (statusCodeExpressionDef != null) {
208-
builder.addPropertyValue("statusCodeExpression", statusCodeExpressionDef);
209-
}
210-
211-
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, IntegrationNamespaceUtils.AUTO_STARTUP);
212-
IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, IntegrationNamespaceUtils.PHASE);
213-
IntegrationNamespaceUtils.setReferenceIfAttributeDefined(builder, element, "validator");
214-
}
215-
216-
private String getInputChannelAttributeName() {
217-
return this.expectReply ? "request-channel" : "channel";
218234
}
219235

220236
private BeanDefinition createRequestMapping(Element element) {
@@ -231,7 +247,7 @@ private BeanDefinition createRequestMapping(Element element) {
231247
Element requestMappingElement = DomUtils.getChildElementByTagName(element, "request-mapping");
232248

233249
if (requestMappingElement != null) {
234-
for (String requestMappingAttribute : new String[] { "params", "headers", "consumes", "produces" }) {
250+
for (String requestMappingAttribute : new String[]{ "params", "headers", "consumes", "produces" }) {
235251
IntegrationNamespaceUtils.setValueIfAttributeDefined(requestMappingDefBuilder, requestMappingElement,
236252
requestMappingAttribute);
237253
}

0 commit comments

Comments
 (0)