From a39881fd0db71d726b9533e16cba2ae6b9bcb66d Mon Sep 17 00:00:00 2001 From: Artem Bilan Date: Sat, 22 Dec 2018 18:30:03 -0500 Subject: [PATCH] Fix new issues in Sonar * Use properly `IntegrationObjectSupport.extractTypeIfPossible()` --- .../codec/kryo/AbstractKryoRegistrar.java | 2 +- ...stractSimpleMessageHandlerFactoryBean.java | 47 ++++++++--------- ...ractStandardMessageHandlerFactoryBean.java | 49 ++++++------------ .../integration/config/RouterFactoryBean.java | 29 ++++++----- .../config/SplitterFactoryBean.java | 24 +++++---- .../context/IntegrationObjectSupport.java | 50 +++++++------------ 6 files changed, 83 insertions(+), 118 deletions(-) diff --git a/spring-integration-core/src/main/java/org/springframework/integration/codec/kryo/AbstractKryoRegistrar.java b/spring-integration-core/src/main/java/org/springframework/integration/codec/kryo/AbstractKryoRegistrar.java index b1fecea480..eeda2acf30 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/codec/kryo/AbstractKryoRegistrar.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/codec/kryo/AbstractKryoRegistrar.java @@ -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) { diff --git a/spring-integration-core/src/main/java/org/springframework/integration/config/AbstractSimpleMessageHandlerFactoryBean.java b/spring-integration-core/src/main/java/org/springframework/integration/config/AbstractSimpleMessageHandlerFactoryBean.java index a7ce748ec0..6662d90ac7 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/config/AbstractSimpleMessageHandlerFactoryBean.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/config/AbstractSimpleMessageHandlerFactoryBean.java @@ -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, 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 adviceChain; + private BeanFactory beanFactory; - private volatile String componentName; + private H handler; + + private MessageChannel outputChannel; + + private Integer order; + + private List adviceChain; + + private String componentName; private ApplicationContext applicationContext; @@ -87,6 +86,8 @@ public abstract class AbstractSimpleMessageHandlerFactoryBean) { - 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 MessageHandler createMessageProcessingHandler(MessageProcessor 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 extractTypeIfPossible(Object targetObject, Class 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)); diff --git a/spring-integration-core/src/main/java/org/springframework/integration/config/RouterFactoryBean.java b/spring-integration-core/src/main/java/org/springframework/integration/config/RouterFactoryBean.java index b90d275382..ee208fa757 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/config/RouterFactoryBean.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/config/RouterFactoryBean.java @@ -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 channelMappings; + private Map 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; } diff --git a/spring-integration-core/src/main/java/org/springframework/integration/config/SplitterFactoryBean.java b/spring-integration-core/src/main/java/org/springframework/integration/config/SplitterFactoryBean.java index 79b7de5ef5..4a1b7f6553 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/config/SplitterFactoryBean.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/config/SplitterFactoryBean.java @@ -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; } diff --git a/spring-integration-core/src/main/java/org/springframework/integration/context/IntegrationObjectSupport.java b/spring-integration-core/src/main/java/org/springframework/integration/context/IntegrationObjectSupport.java index 13c291db30..7221170070 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/context/IntegrationObjectSupport.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/context/IntegrationObjectSupport.java @@ -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 extractTypeIfPossible(Object targetObject, Class expectedType) { + @Nullable + public static T extractTypeIfPossible(@Nullable Object targetObject, Class 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() {