Skip to content

Commit a39881f

Browse files
artembilangaryrussell
authored andcommitted
Fix new issues in Sonar
* Use properly `IntegrationObjectSupport.extractTypeIfPossible()`
1 parent 113a371 commit a39881f

File tree

6 files changed

+80
-115
lines changed

6 files changed

+80
-115
lines changed

spring-integration-core/src/main/java/org/springframework/integration/codec/kryo/AbstractKryoRegistrar.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public abstract class AbstractKryoRegistrar implements KryoRegistrar {
3434

3535
protected static final Kryo kryo = new Kryo(); // NOSONAR TODO uppercase in 5.2
3636

37-
protected final Log log = LogFactory.getLog(getClass());
37+
protected final Log log = LogFactory.getLog(getClass()); // NOSONAR property is final
3838

3939
@Override
4040
public void registerTypes(Kryo kryo) {

spring-integration-core/src/main/java/org/springframework/integration/config/AbstractSimpleMessageHandlerFactoryBean.java

+18-23
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -23,6 +23,7 @@
2323
import org.apache.commons.logging.LogFactory;
2424

2525
import org.springframework.aop.framework.Advised;
26+
import org.springframework.aop.framework.AopProxyUtils;
2627
import org.springframework.beans.BeansException;
2728
import org.springframework.beans.factory.BeanFactory;
2829
import org.springframework.beans.factory.BeanFactoryAware;
@@ -59,23 +60,21 @@ public abstract class AbstractSimpleMessageHandlerFactoryBean<H extends MessageH
5960
implements FactoryBean<MessageHandler>, ApplicationContextAware, BeanFactoryAware, BeanNameAware,
6061
ApplicationEventPublisherAware {
6162

62-
protected final Log logger = LogFactory.getLog(this.getClass());
63+
protected final Log logger = LogFactory.getLog(getClass()); //NOSONAR protected with final
6364

64-
private volatile H handler;
65-
66-
private volatile MessageChannel outputChannel;
67-
68-
private volatile Integer order;
65+
private final Object initializationMonitor = new Object();
6966

7067
private BeanFactory beanFactory;
7168

72-
private volatile boolean initialized;
69+
private H handler;
7370

74-
private final Object initializationMonitor = new Object();
71+
private MessageChannel outputChannel;
7572

76-
private volatile List<Advice> adviceChain;
73+
private Integer order;
7774

78-
private volatile String componentName;
75+
private List<Advice> adviceChain;
76+
77+
private String componentName;
7978

8079
private ApplicationContext applicationContext;
8180

@@ -87,6 +86,8 @@ public abstract class AbstractSimpleMessageHandlerFactoryBean<H extends MessageH
8786

8887
private Boolean async;
8988

89+
private boolean initialized;
90+
9091
@Override
9192
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
9293
this.applicationContext = applicationContext;
@@ -168,9 +169,9 @@ public void setComponentName(String componentName) {
168169
}
169170

170171
@Override
171-
public H getObject() throws Exception {
172+
public H getObject() {
172173
if (this.handler == null) {
173-
this.handler = this.createHandlerInternal();
174+
this.handler = createHandlerInternal();
174175
Assert.notNull(this.handler, "failed to create MessageHandler");
175176
}
176177
return this.handler;
@@ -221,7 +222,7 @@ else if (this.logger.isDebugEnabled()) {
221222
name = ((NamedComponent) actualHandler).getComponentName();
222223
}
223224
this.logger.debug("adviceChain can only be set on an AbstractReplyProducingMessageHandler"
224-
+ (name == null ? "" : (", " + name)) + ".");
225+
+ (name == null ? "" : (", " + name)) + ".");
225226
}
226227
}
227228
if (this.async != null) {
@@ -270,18 +271,12 @@ public boolean isSingleton() {
270271
return true;
271272
}
272273

273-
private Object extractTarget(Object object) {
274+
private static Object extractTarget(Object object) {
274275
if (!(object instanceof Advised)) {
275276
return object;
276277
}
277-
Advised advised = (Advised) object;
278-
try {
279-
// TargetSource is never null
280-
return extractTarget(advised.getTargetSource().getTarget());
281-
}
282-
catch (Exception e) {
283-
this.logger.error("Could not extract target", e);
284-
return null;
278+
else {
279+
return extractTarget(AopProxyUtils.getSingletonTarget(object));
285280
}
286281
}
287282

spring-integration-core/src/main/java/org/springframework/integration/config/AbstractStandardMessageHandlerFactoryBean.java

+15-34
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,11 @@
1919
import java.util.HashSet;
2020
import java.util.Set;
2121

22-
import org.springframework.aop.TargetSource;
23-
import org.springframework.aop.framework.Advised;
2422
import org.springframework.beans.factory.DisposableBean;
2523
import org.springframework.expression.Expression;
2624
import org.springframework.expression.ExpressionParser;
2725
import org.springframework.expression.spel.standard.SpelExpressionParser;
26+
import org.springframework.integration.context.IntegrationObjectSupport;
2827
import org.springframework.integration.handler.AbstractMessageProducingHandler;
2928
import org.springframework.integration.handler.AbstractReplyProducingMessageHandler;
3029
import org.springframework.integration.handler.MessageProcessor;
@@ -121,13 +120,15 @@ protected MessageHandler createHandler() {
121120
if (this.targetObject != null) {
122121
Assert.state(this.expression == null,
123122
"The 'targetObject' and 'expression' properties are mutually exclusive.");
124-
AbstractMessageProducingHandler actualHandler = this.extractTypeIfPossible(this.targetObject,
125-
AbstractMessageProducingHandler.class);
126-
boolean targetIsDirectReplyProducingHandler = actualHandler != null
127-
&& canBeUsedDirect(actualHandler) // give subclasses a say
128-
&& methodIsHandleMessageOrEmpty(this.targetMethodName);
123+
AbstractMessageProducingHandler actualHandler =
124+
IntegrationObjectSupport.extractTypeIfPossible(this.targetObject,
125+
AbstractMessageProducingHandler.class);
126+
boolean targetIsDirectReplyProducingHandler =
127+
actualHandler != null
128+
&& canBeUsedDirect(actualHandler) // give subclasses a say
129+
&& methodIsHandleMessageOrEmpty(this.targetMethodName);
129130
if (this.targetObject instanceof MessageProcessor<?>) {
130-
handler = this.createMessageProcessingHandler((MessageProcessor<?>) this.targetObject);
131+
handler = createMessageProcessingHandler((MessageProcessor<?>) this.targetObject);
131132
}
132133
else if (targetIsDirectReplyProducingHandler) {
133134
if (logger.isDebugEnabled()) {
@@ -138,21 +139,21 @@ else if (targetIsDirectReplyProducingHandler) {
138139
handler = (MessageHandler) this.targetObject;
139140
}
140141
else {
141-
handler = this.createMethodInvokingHandler(this.targetObject, this.targetMethodName);
142+
handler = createMethodInvokingHandler(this.targetObject, this.targetMethodName);
142143
}
143144
}
144145
else if (this.expression != null) {
145-
handler = this.createExpressionEvaluatingHandler(this.expression);
146+
handler = createExpressionEvaluatingHandler(this.expression);
146147
}
147148
else {
148-
handler = this.createDefaultHandler();
149+
handler = createDefaultHandler();
149150
}
150151
return handler;
151152
}
152153

153154
protected void checkForIllegalTarget(Object targetObject, String targetMethodName) {
154155
if (targetObject instanceof AbstractReplyProducingMessageHandler
155-
&& this.methodIsHandleMessageOrEmpty(targetMethodName)) {
156+
&& methodIsHandleMessageOrEmpty(targetMethodName)) {
156157
/*
157158
* If we allow an ARPMH to be the target of another ARPMH, the reply would
158159
* be attempted to be sent by the inner (no output channel) and a reply would
@@ -180,37 +181,17 @@ private void checkReuse(AbstractMessageProducingHandler replyHandler) {
180181
protected abstract MessageHandler createMethodInvokingHandler(Object targetObject, String targetMethodName);
181182

182183
protected MessageHandler createExpressionEvaluatingHandler(Expression expression) {
183-
throw new UnsupportedOperationException(this.getClass().getName() + " does not support expressions.");
184+
throw new UnsupportedOperationException(getClass().getName() + " does not support expressions.");
184185
}
185186

186187
protected <T> MessageHandler createMessageProcessingHandler(MessageProcessor<T> processor) {
187-
return this.createMethodInvokingHandler(processor, null);
188+
return createMethodInvokingHandler(processor, null);
188189
}
189190

190191
protected MessageHandler createDefaultHandler() {
191192
throw new IllegalArgumentException("Exactly one of the 'targetObject' or 'expression' property is required.");
192193
}
193194

194-
@SuppressWarnings("unchecked")
195-
protected <T> T extractTypeIfPossible(Object targetObject, Class<T> expectedType) {
196-
if (targetObject == null) {
197-
return null;
198-
}
199-
if (expectedType.isAssignableFrom(targetObject.getClass())) {
200-
return (T) targetObject;
201-
}
202-
if (targetObject instanceof Advised) {
203-
TargetSource targetSource = ((Advised) targetObject).getTargetSource();
204-
try {
205-
return extractTypeIfPossible(targetSource.getTarget(), expectedType);
206-
}
207-
catch (Exception e) {
208-
throw new IllegalStateException(e);
209-
}
210-
}
211-
return null;
212-
}
213-
214195
protected boolean methodIsHandleMessageOrEmpty(String targetMethodName) {
215196
return (!StringUtils.hasText(targetMethodName)
216197
|| "handleMessage".equals(targetMethodName));

spring-integration-core/src/main/java/org/springframework/integration/config/RouterFactoryBean.java

+16-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -19,6 +19,7 @@
1919
import java.util.Map;
2020

2121
import org.springframework.expression.Expression;
22+
import org.springframework.integration.context.IntegrationObjectSupport;
2223
import org.springframework.integration.handler.AbstractMessageProducingHandler;
2324
import org.springframework.integration.router.AbstractMappingMessageRouter;
2425
import org.springframework.integration.router.AbstractMessageRouter;
@@ -42,17 +43,17 @@
4243
*/
4344
public class RouterFactoryBean extends AbstractStandardMessageHandlerFactoryBean {
4445

45-
private volatile Map<String, String> channelMappings;
46+
private Map<String, String> channelMappings;
4647

47-
private volatile MessageChannel defaultOutputChannel;
48+
private MessageChannel defaultOutputChannel;
4849

49-
private volatile String defaultOutputChannelName;
50+
private String defaultOutputChannelName;
5051

51-
private volatile Boolean resolutionRequired;
52+
private Boolean resolutionRequired;
5253

53-
private volatile Boolean applySequence;
54+
private Boolean applySequence;
5455

55-
private volatile Boolean ignoreSendFailures;
56+
private Boolean ignoreSendFailures;
5657

5758
public void setDefaultOutputChannel(MessageChannel defaultOutputChannel) {
5859
this.defaultOutputChannel = defaultOutputChannel;
@@ -81,19 +82,21 @@ public void setChannelMappings(Map<String, String> channelMappings) {
8182
@Override
8283
protected MessageHandler createMethodInvokingHandler(Object targetObject, String targetMethodName) {
8384
Assert.notNull(targetObject, "target object must not be null");
84-
AbstractMessageRouter router = this.extractTypeIfPossible(targetObject, AbstractMessageRouter.class);
85+
AbstractMessageRouter router =
86+
IntegrationObjectSupport.extractTypeIfPossible(targetObject, AbstractMessageRouter.class);
8587
if (router == null) {
8688
if (targetObject instanceof MessageHandler && this.noRouterAttributesProvided()
8789
&& this.methodIsHandleMessageOrEmpty(targetMethodName)) {
8890
return (MessageHandler) targetObject;
8991
}
90-
router = this.createMethodInvokingRouter(targetObject, targetMethodName);
91-
this.configureRouter(router);
92+
router = createMethodInvokingRouter(targetObject, targetMethodName);
93+
configureRouter(router);
9294
}
9395
else {
94-
Assert.isTrue(!StringUtils.hasText(targetMethodName), "target method should not be provided when the target "
95-
+ "object is an implementation of AbstractMessageRouter");
96-
this.configureRouter(router);
96+
Assert.isTrue(!StringUtils.hasText(targetMethodName),
97+
"target method should not be provided when the target "
98+
+ "object is an implementation of AbstractMessageRouter");
99+
configureRouter(router);
97100
if (targetObject instanceof MessageHandler) {
98101
return (MessageHandler) targetObject;
99102
}

spring-integration-core/src/main/java/org/springframework/integration/config/SplitterFactoryBean.java

+13-11
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2017 the original author or authors.
2+
* Copyright 2002-2018 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.
@@ -17,6 +17,7 @@
1717
package org.springframework.integration.config;
1818

1919
import org.springframework.expression.Expression;
20+
import org.springframework.integration.context.IntegrationObjectSupport;
2021
import org.springframework.integration.handler.AbstractMessageProducingHandler;
2122
import org.springframework.integration.splitter.AbstractMessageSplitter;
2223
import org.springframework.integration.splitter.DefaultMessageSplitter;
@@ -37,9 +38,9 @@
3738
*/
3839
public class SplitterFactoryBean extends AbstractStandardMessageHandlerFactoryBean {
3940

40-
private volatile Boolean applySequence;
41+
private Boolean applySequence;
4142

42-
private volatile String delimiters;
43+
private String delimiters;
4344

4445
public void setApplySequence(boolean applySequence) {
4546
this.applySequence = applySequence;
@@ -52,17 +53,18 @@ public void setDelimiters(String delimiters) {
5253
@Override
5354
protected MessageHandler createMethodInvokingHandler(Object targetObject, String targetMethodName) {
5455
Assert.notNull(targetObject, "targetObject must not be null");
55-
AbstractMessageSplitter splitter = this.extractTypeIfPossible(targetObject, AbstractMessageSplitter.class);
56+
AbstractMessageSplitter splitter =
57+
IntegrationObjectSupport.extractTypeIfPossible(targetObject, AbstractMessageSplitter.class);
5658
if (splitter == null) {
57-
this.checkForIllegalTarget(targetObject, targetMethodName);
58-
splitter = this.createMethodInvokingSplitter(targetObject, targetMethodName);
59-
this.configureSplitter(splitter);
59+
checkForIllegalTarget(targetObject, targetMethodName);
60+
splitter = createMethodInvokingSplitter(targetObject, targetMethodName);
61+
configureSplitter(splitter);
6062
}
6163
else {
6264
Assert.isTrue(!StringUtils.hasText(targetMethodName),
6365
"target method should not be provided when the target "
6466
+ "object is an implementation of AbstractMessageSplitter");
65-
this.configureSplitter(splitter);
67+
configureSplitter(splitter);
6668
if (targetObject instanceof MessageHandler) {
6769
return (MessageHandler) targetObject;
6870
}
@@ -78,16 +80,16 @@ protected AbstractMessageSplitter createMethodInvokingSplitter(Object targetObje
7880

7981
@Override
8082
protected MessageHandler createExpressionEvaluatingHandler(Expression expression) {
81-
return this.configureSplitter(new ExpressionEvaluatingSplitter(expression));
83+
return configureSplitter(new ExpressionEvaluatingSplitter(expression));
8284
}
8385

8486
@Override
8587
protected MessageHandler createDefaultHandler() {
86-
return this.configureSplitter(new DefaultMessageSplitter());
88+
return configureSplitter(new DefaultMessageSplitter());
8789
}
8890

8991
protected AbstractMessageSplitter configureSplitter(AbstractMessageSplitter splitter) {
90-
this.postProcessReplyProducer(splitter);
92+
postProcessReplyProducer(splitter);
9193
return splitter;
9294
}
9395

0 commit comments

Comments
 (0)