Fix new issues in Sonar

* Use properly `IntegrationObjectSupport.extractTypeIfPossible()`
This commit is contained in:
Artem Bilan
2018-12-22 18:30:03 -05:00
committed by Gary Russell
parent 113a371f2c
commit a39881fd0d
6 changed files with 83 additions and 118 deletions

View File

@@ -34,7 +34,7 @@ public abstract class AbstractKryoRegistrar implements KryoRegistrar {
protected static final Kryo kryo = new Kryo(); // NOSONAR TODO uppercase in 5.2
protected final Log log = LogFactory.getLog(getClass());
protected final Log log = LogFactory.getLog(getClass()); // NOSONAR property is final
@Override
public void registerTypes(Kryo kryo) {

View File

@@ -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.
@@ -23,6 +23,7 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.aop.framework.Advised;
import org.springframework.aop.framework.AopProxyUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
@@ -59,23 +60,21 @@ public abstract class AbstractSimpleMessageHandlerFactoryBean<H extends MessageH
implements FactoryBean<MessageHandler>, ApplicationContextAware, BeanFactoryAware, BeanNameAware,
ApplicationEventPublisherAware {
protected final Log logger = LogFactory.getLog(this.getClass());
private volatile H handler;
private volatile MessageChannel outputChannel;
private volatile Integer order;
private BeanFactory beanFactory;
private volatile boolean initialized;
protected final Log logger = LogFactory.getLog(getClass()); //NOSONAR protected with final
private final Object initializationMonitor = new Object();
private volatile List<Advice> adviceChain;
private BeanFactory beanFactory;
private volatile String componentName;
private H handler;
private MessageChannel outputChannel;
private Integer order;
private List<Advice> adviceChain;
private String componentName;
private ApplicationContext applicationContext;
@@ -87,6 +86,8 @@ public abstract class AbstractSimpleMessageHandlerFactoryBean<H extends MessageH
private Boolean async;
private boolean initialized;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
@@ -168,9 +169,9 @@ public abstract class AbstractSimpleMessageHandlerFactoryBean<H extends MessageH
}
@Override
public H getObject() throws Exception {
public H getObject() {
if (this.handler == null) {
this.handler = this.createHandlerInternal();
this.handler = createHandlerInternal();
Assert.notNull(this.handler, "failed to create MessageHandler");
}
return this.handler;
@@ -221,7 +222,7 @@ public abstract class AbstractSimpleMessageHandlerFactoryBean<H extends MessageH
name = ((NamedComponent) actualHandler).getComponentName();
}
this.logger.debug("adviceChain can only be set on an AbstractReplyProducingMessageHandler"
+ (name == null ? "" : (", " + name)) + ".");
+ (name == null ? "" : (", " + name)) + ".");
}
}
if (this.async != null) {
@@ -270,18 +271,12 @@ public abstract class AbstractSimpleMessageHandlerFactoryBean<H extends MessageH
return true;
}
private Object extractTarget(Object object) {
private static Object extractTarget(Object object) {
if (!(object instanceof Advised)) {
return object;
}
Advised advised = (Advised) object;
try {
// TargetSource is never null
return extractTarget(advised.getTargetSource().getTarget());
}
catch (Exception e) {
this.logger.error("Could not extract target", e);
return null;
else {
return extractTarget(AopProxyUtils.getSingletonTarget(object));
}
}

View File

@@ -19,12 +19,11 @@ package org.springframework.integration.config;
import java.util.HashSet;
import java.util.Set;
import org.springframework.aop.TargetSource;
import org.springframework.aop.framework.Advised;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.integration.context.IntegrationObjectSupport;
import org.springframework.integration.handler.AbstractMessageProducingHandler;
import org.springframework.integration.handler.AbstractReplyProducingMessageHandler;
import org.springframework.integration.handler.MessageProcessor;
@@ -121,13 +120,15 @@ public abstract class AbstractStandardMessageHandlerFactoryBean
if (this.targetObject != null) {
Assert.state(this.expression == null,
"The 'targetObject' and 'expression' properties are mutually exclusive.");
AbstractMessageProducingHandler actualHandler = this.extractTypeIfPossible(this.targetObject,
AbstractMessageProducingHandler.class);
boolean targetIsDirectReplyProducingHandler = actualHandler != null
&& canBeUsedDirect(actualHandler) // give subclasses a say
&& methodIsHandleMessageOrEmpty(this.targetMethodName);
AbstractMessageProducingHandler actualHandler =
IntegrationObjectSupport.extractTypeIfPossible(this.targetObject,
AbstractMessageProducingHandler.class);
boolean targetIsDirectReplyProducingHandler =
actualHandler != null
&& canBeUsedDirect(actualHandler) // give subclasses a say
&& methodIsHandleMessageOrEmpty(this.targetMethodName);
if (this.targetObject instanceof MessageProcessor<?>) {
handler = this.createMessageProcessingHandler((MessageProcessor<?>) this.targetObject);
handler = createMessageProcessingHandler((MessageProcessor<?>) this.targetObject);
}
else if (targetIsDirectReplyProducingHandler) {
if (logger.isDebugEnabled()) {
@@ -138,21 +139,21 @@ public abstract class AbstractStandardMessageHandlerFactoryBean
handler = (MessageHandler) this.targetObject;
}
else {
handler = this.createMethodInvokingHandler(this.targetObject, this.targetMethodName);
handler = createMethodInvokingHandler(this.targetObject, this.targetMethodName);
}
}
else if (this.expression != null) {
handler = this.createExpressionEvaluatingHandler(this.expression);
handler = createExpressionEvaluatingHandler(this.expression);
}
else {
handler = this.createDefaultHandler();
handler = createDefaultHandler();
}
return handler;
}
protected void checkForIllegalTarget(Object targetObject, String targetMethodName) {
if (targetObject instanceof AbstractReplyProducingMessageHandler
&& this.methodIsHandleMessageOrEmpty(targetMethodName)) {
&& methodIsHandleMessageOrEmpty(targetMethodName)) {
/*
* If we allow an ARPMH to be the target of another ARPMH, the reply would
* be attempted to be sent by the inner (no output channel) and a reply would
@@ -180,37 +181,17 @@ public abstract class AbstractStandardMessageHandlerFactoryBean
protected abstract MessageHandler createMethodInvokingHandler(Object targetObject, String targetMethodName);
protected MessageHandler createExpressionEvaluatingHandler(Expression expression) {
throw new UnsupportedOperationException(this.getClass().getName() + " does not support expressions.");
throw new UnsupportedOperationException(getClass().getName() + " does not support expressions.");
}
protected <T> MessageHandler createMessageProcessingHandler(MessageProcessor<T> processor) {
return this.createMethodInvokingHandler(processor, null);
return createMethodInvokingHandler(processor, null);
}
protected MessageHandler createDefaultHandler() {
throw new IllegalArgumentException("Exactly one of the 'targetObject' or 'expression' property is required.");
}
@SuppressWarnings("unchecked")
protected <T> T extractTypeIfPossible(Object targetObject, Class<T> expectedType) {
if (targetObject == null) {
return null;
}
if (expectedType.isAssignableFrom(targetObject.getClass())) {
return (T) targetObject;
}
if (targetObject instanceof Advised) {
TargetSource targetSource = ((Advised) targetObject).getTargetSource();
try {
return extractTypeIfPossible(targetSource.getTarget(), expectedType);
}
catch (Exception e) {
throw new IllegalStateException(e);
}
}
return null;
}
protected boolean methodIsHandleMessageOrEmpty(String targetMethodName) {
return (!StringUtils.hasText(targetMethodName)
|| "handleMessage".equals(targetMethodName));

View File

@@ -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.
@@ -19,6 +19,7 @@ package org.springframework.integration.config;
import java.util.Map;
import org.springframework.expression.Expression;
import org.springframework.integration.context.IntegrationObjectSupport;
import org.springframework.integration.handler.AbstractMessageProducingHandler;
import org.springframework.integration.router.AbstractMappingMessageRouter;
import org.springframework.integration.router.AbstractMessageRouter;
@@ -42,17 +43,17 @@ import org.springframework.util.StringUtils;
*/
public class RouterFactoryBean extends AbstractStandardMessageHandlerFactoryBean {
private volatile Map<String, String> channelMappings;
private Map<String, String> channelMappings;
private volatile MessageChannel defaultOutputChannel;
private MessageChannel defaultOutputChannel;
private volatile String defaultOutputChannelName;
private String defaultOutputChannelName;
private volatile Boolean resolutionRequired;
private Boolean resolutionRequired;
private volatile Boolean applySequence;
private Boolean applySequence;
private volatile Boolean ignoreSendFailures;
private Boolean ignoreSendFailures;
public void setDefaultOutputChannel(MessageChannel defaultOutputChannel) {
this.defaultOutputChannel = defaultOutputChannel;
@@ -81,19 +82,21 @@ public class RouterFactoryBean extends AbstractStandardMessageHandlerFactoryBean
@Override
protected MessageHandler createMethodInvokingHandler(Object targetObject, String targetMethodName) {
Assert.notNull(targetObject, "target object must not be null");
AbstractMessageRouter router = this.extractTypeIfPossible(targetObject, AbstractMessageRouter.class);
AbstractMessageRouter router =
IntegrationObjectSupport.extractTypeIfPossible(targetObject, AbstractMessageRouter.class);
if (router == null) {
if (targetObject instanceof MessageHandler && this.noRouterAttributesProvided()
&& this.methodIsHandleMessageOrEmpty(targetMethodName)) {
return (MessageHandler) targetObject;
}
router = this.createMethodInvokingRouter(targetObject, targetMethodName);
this.configureRouter(router);
router = createMethodInvokingRouter(targetObject, targetMethodName);
configureRouter(router);
}
else {
Assert.isTrue(!StringUtils.hasText(targetMethodName), "target method should not be provided when the target "
+ "object is an implementation of AbstractMessageRouter");
this.configureRouter(router);
Assert.isTrue(!StringUtils.hasText(targetMethodName),
"target method should not be provided when the target "
+ "object is an implementation of AbstractMessageRouter");
configureRouter(router);
if (targetObject instanceof MessageHandler) {
return (MessageHandler) targetObject;
}

View File

@@ -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.
@@ -17,6 +17,7 @@
package org.springframework.integration.config;
import org.springframework.expression.Expression;
import org.springframework.integration.context.IntegrationObjectSupport;
import org.springframework.integration.handler.AbstractMessageProducingHandler;
import org.springframework.integration.splitter.AbstractMessageSplitter;
import org.springframework.integration.splitter.DefaultMessageSplitter;
@@ -37,9 +38,9 @@ import org.springframework.util.StringUtils;
*/
public class SplitterFactoryBean extends AbstractStandardMessageHandlerFactoryBean {
private volatile Boolean applySequence;
private Boolean applySequence;
private volatile String delimiters;
private String delimiters;
public void setApplySequence(boolean applySequence) {
this.applySequence = applySequence;
@@ -52,17 +53,18 @@ public class SplitterFactoryBean extends AbstractStandardMessageHandlerFactoryBe
@Override
protected MessageHandler createMethodInvokingHandler(Object targetObject, String targetMethodName) {
Assert.notNull(targetObject, "targetObject must not be null");
AbstractMessageSplitter splitter = this.extractTypeIfPossible(targetObject, AbstractMessageSplitter.class);
AbstractMessageSplitter splitter =
IntegrationObjectSupport.extractTypeIfPossible(targetObject, AbstractMessageSplitter.class);
if (splitter == null) {
this.checkForIllegalTarget(targetObject, targetMethodName);
splitter = this.createMethodInvokingSplitter(targetObject, targetMethodName);
this.configureSplitter(splitter);
checkForIllegalTarget(targetObject, targetMethodName);
splitter = createMethodInvokingSplitter(targetObject, targetMethodName);
configureSplitter(splitter);
}
else {
Assert.isTrue(!StringUtils.hasText(targetMethodName),
"target method should not be provided when the target "
+ "object is an implementation of AbstractMessageSplitter");
this.configureSplitter(splitter);
configureSplitter(splitter);
if (targetObject instanceof MessageHandler) {
return (MessageHandler) targetObject;
}
@@ -78,16 +80,16 @@ public class SplitterFactoryBean extends AbstractStandardMessageHandlerFactoryBe
@Override
protected MessageHandler createExpressionEvaluatingHandler(Expression expression) {
return this.configureSplitter(new ExpressionEvaluatingSplitter(expression));
return configureSplitter(new ExpressionEvaluatingSplitter(expression));
}
@Override
protected MessageHandler createDefaultHandler() {
return this.configureSplitter(new DefaultMessageSplitter());
return configureSplitter(new DefaultMessageSplitter());
}
protected AbstractMessageSplitter configureSplitter(AbstractMessageSplitter splitter) {
this.postProcessReplyProducer(splitter);
postProcessReplyProducer(splitter);
return splitter;
}

View File

@@ -22,12 +22,10 @@ import java.util.UUID;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.aop.TargetSource;
import org.springframework.aop.framework.Advised;
import org.springframework.aop.framework.AopProxyUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
@@ -42,6 +40,7 @@ import org.springframework.integration.support.MessageBuilderFactory;
import org.springframework.integration.support.channel.BeanFactoryChannelResolver;
import org.springframework.integration.support.context.NamedComponent;
import org.springframework.integration.support.utils.IntegrationUtils;
import org.springframework.lang.Nullable;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.core.DestinationResolver;
import org.springframework.scheduling.TaskScheduler;
@@ -171,24 +170,15 @@ public abstract class IntegrationObjectSupport implements BeanNameAware, NamedCo
@Override
public final void afterPropertiesSet() {
this.integrationProperties = IntegrationContextUtils.getIntegrationProperties(this.beanFactory);
try {
if (this.messageBuilderFactory == null) {
if (this.beanFactory != null) {
this.messageBuilderFactory = IntegrationUtils.getMessageBuilderFactory(this.beanFactory);
}
else {
this.messageBuilderFactory = new DefaultMessageBuilderFactory();
}
if (this.messageBuilderFactory == null) {
if (this.beanFactory != null) {
this.messageBuilderFactory = IntegrationUtils.getMessageBuilderFactory(this.beanFactory);
}
this.onInit();
}
catch (Exception e) {
if (e instanceof RuntimeException) {
throw (RuntimeException) e;
else {
this.messageBuilderFactory = new DefaultMessageBuilderFactory();
}
throw new BeanInitializationException("failed to initialize", e);
}
onInit();
this.initialized = true;
}
@@ -291,29 +281,23 @@ public abstract class IntegrationObjectSupport implements BeanNameAware, NamedCo
return this.defaultConversionService.convert(this.integrationProperties.getProperty(key), tClass);
}
@Override
public String toString() {
return (this.beanName != null) ? this.beanName : super.toString();
}
@SuppressWarnings("unchecked")
protected <T> T extractTypeIfPossible(Object targetObject, Class<T> expectedType) {
@Nullable
public static <T> T extractTypeIfPossible(@Nullable Object targetObject, Class<T> expectedType) {
if (targetObject == null) {
return null;
}
if (expectedType.isAssignableFrom(targetObject.getClass())) {
return (T) targetObject;
}
if (targetObject instanceof Advised) {
TargetSource targetSource = ((Advised) targetObject).getTargetSource();
try {
return extractTypeIfPossible(targetSource.getTarget(), expectedType);
}
catch (Exception e) {
throw new IllegalStateException(e);
}
else {
return extractTypeIfPossible(AopProxyUtils.getSingletonTarget(targetObject), expectedType);
}
return null;
}
@Override
public String toString() {
return (this.beanName != null) ? this.beanName : super.toString();
}
public static UUID generateId() {