Fix new Sonar Smells

* Fix synchronization smell in the `MessagePublishingInterceptor`
* Cache `Pointcut` in the `PublisherAnnotationAdvisor`;
fix `@SuppressWarnings` also
* Fix complexity in the `IntegrationMBeanExporter`
This commit is contained in:
Artem Bilan
2019-03-15 13:32:18 -04:00
committed by Gary Russell
parent 9cf52b7281
commit 921c797646
3 changed files with 62 additions and 78 deletions

View File

@@ -104,7 +104,7 @@ public class MessagePublishingInterceptor implements MethodInterceptor, BeanFact
this.beanFactory = beanFactory;
this.messagingTemplate.setBeanFactory(beanFactory);
if (this.channelResolver == null) {
this.channelResolver = IntegrationContextUtils.getChannelResolver(beanFactory);
this.channelResolver = IntegrationContextUtils.getChannelResolver(this.beanFactory);
}
}
@@ -123,7 +123,7 @@ public class MessagePublishingInterceptor implements MethodInterceptor, BeanFact
final StandardEvaluationContext context = ExpressionUtils.createStandardEvaluationContext(this.beanFactory);
Class<?> targetClass = AopUtils.getTargetClass(invocation.getThis());
final Method method = AopUtils.getMostSpecificMethod(invocation.getMethod(), targetClass);
String[] argumentNames = this.resolveArgumentNames(method);
String[] argumentNames = resolveArgumentNames(method);
context.setVariable(PublisherMetadataSource.METHOD_NAME_VARIABLE_NAME, method.getName());
if (invocation.getArguments().length > 0 && argumentNames != null) {
Map<Object, Object> argumentMap = new HashMap<>();
@@ -180,16 +180,12 @@ public class MessagePublishingInterceptor implements MethodInterceptor, BeanFact
this.messagingTemplate.send(channel, message);
}
else {
if (this.defaultChannelName != null) {
synchronized (this) {
if (this.defaultChannelName != null && this.messagingTemplate.getDefaultDestination() == null) {
Assert.state(this.channelResolver != null,
"ChannelResolver is required to resolve channel names.");
this.messagingTemplate.setDefaultChannel(
this.channelResolver.resolveDestination(this.defaultChannelName));
}
this.defaultChannelName = null;
}
String channelNameToUse = this.defaultChannelName;
if (channelNameToUse != null && this.messagingTemplate.getDefaultDestination() == null) {
Assert.state(this.channelResolver != null, "ChannelResolver is required to resolve channel names.");
this.messagingTemplate.setDefaultChannel(
this.channelResolver.resolveDestination(channelNameToUse));
this.defaultChannelName = null;
}
this.messagingTemplate.send(message);
}

View File

@@ -19,8 +19,7 @@ package org.springframework.integration.aop;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;
import org.aopalliance.aop.Advice;
@@ -52,23 +51,24 @@ import org.springframework.util.Assert;
@SuppressWarnings("serial")
public class PublisherAnnotationAdvisor extends AbstractPointcutAdvisor implements BeanFactoryAware {
private final Set<Class<? extends Annotation>> publisherAnnotationTypes;
private final MessagePublishingInterceptor interceptor;
@SuppressWarnings("unchecked")
public PublisherAnnotationAdvisor(Class<? extends Annotation>... publisherAnnotationTypes) {
this.publisherAnnotationTypes = new HashSet<>(Arrays.asList(publisherAnnotationTypes));
PublisherMetadataSource metadataSource =
new MethodAnnotationPublisherMetadataSource(this.publisherAnnotationTypes);
this.interceptor = new MessagePublishingInterceptor(metadataSource);
}
private final Pointcut pointcut;
@SuppressWarnings("unchecked")
public PublisherAnnotationAdvisor() {
this(Publisher.class);
}
@SuppressWarnings("varargs")
@SafeVarargs
public PublisherAnnotationAdvisor(Class<? extends Annotation>... publisherAnnotationTypes) {
PublisherMetadataSource metadataSource =
new MethodAnnotationPublisherMetadataSource(
Arrays.stream(publisherAnnotationTypes).collect(Collectors.toSet()));
this.interceptor = new MessagePublishingInterceptor(metadataSource);
this.pointcut = buildPointcut(publisherAnnotationTypes);
}
/**
* A channel bean name to be used as default for publishing.
@@ -91,12 +91,12 @@ public class PublisherAnnotationAdvisor extends AbstractPointcutAdvisor implemen
@Override
public Pointcut getPointcut() {
return this.buildPointcut();
return this.pointcut;
}
private Pointcut buildPointcut() {
private static Pointcut buildPointcut(Class<? extends Annotation>[] publisherAnnotationTypes) {
ComposablePointcut result = null;
for (Class<? extends Annotation> publisherAnnotationType : this.publisherAnnotationTypes) {
for (Class<? extends Annotation> publisherAnnotationType : publisherAnnotationTypes) {
Pointcut cpc = new MetaAnnotationMatchingPointcut(publisherAnnotationType, true);
Pointcut mpc = new MetaAnnotationMatchingPointcut(null, publisherAnnotationType);
if (result == null) {
@@ -187,8 +187,7 @@ public class PublisherAnnotationAdvisor extends AbstractPointcutAdvisor implemen
@Override
@SuppressWarnings("rawtypes")
public boolean matches(Method method, Class targetClass) {
public boolean matches(Method method, Class<?> targetClass) {
if (AnnotationUtils.getAnnotation(method, this.annotationType) != null) {
return true;
}