Skip to content

Commit 0bb901b

Browse files
garyrussellartembilan
authored andcommitted
Sonar fixes
- printStackTrace in test mail server - Illegal throws - Large anon. classes * - indexOf char - stored external object * - ignored exceptional return values * - checkstyle
1 parent 4199ff5 commit 0bb901b

File tree

19 files changed

+269
-224
lines changed

19 files changed

+269
-224
lines changed

spring-integration-core/src/main/java/org/springframework/integration/aggregator/MessageGroupExpiredEvent.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ public MessageGroupExpiredEvent(Object source, Object groupId, int messageCount,
4747
super(source);
4848
this.groupId = groupId;
4949
this.messageCount = messageCount;
50-
this.lastModified = lastModified;
51-
this.expired = expired;
50+
this.lastModified = (Date) lastModified.clone();
51+
this.expired = (Date) expired.clone();
5252
this.discarded = discarded;
5353
}
5454

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ else if (StringUtils.hasText(asyncExecutor)) {
163163
}
164164
String id = (String) gatewayAttributes.get("name");
165165
if (!StringUtils.hasText(id)) {
166-
id = Introspector.decapitalize(serviceInterface.substring(serviceInterface.lastIndexOf(".") + 1));
166+
id = Introspector.decapitalize(serviceInterface.substring(serviceInterface.lastIndexOf('.') + 1));
167167
}
168168

169169
gatewayProxyBuilder.addConstructorArgValue(serviceInterface);

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

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -35,42 +35,49 @@
3535
*
3636
* @author Mark Fisher
3737
* @author Artem Bilan
38+
* @author Gary Russell
3839
*/
3940
public class AnnotationConfigParser implements BeanDefinitionParser {
4041

4142
@Override
4243
public BeanDefinition parse(final Element element, ParserContext parserContext) {
43-
StandardAnnotationMetadata importingClassMetadata =
44-
new StandardAnnotationMetadata(Object.class) {
44+
new IntegrationRegistrar().registerBeanDefinitions(new ExtendedAnnotationMetadata(Object.class, element),
45+
parserContext.getRegistry());
46+
return null;
47+
}
4548

46-
@Override
47-
public Map<String, Object> getAnnotationAttributes(String annotationType) {
48-
if (EnablePublisher.class.getName().equals(annotationType)) {
49-
Element enablePublisherElement =
50-
DomUtils.getChildElementByTagName(element, "enable-publisher");
51-
if (enablePublisherElement != null) {
52-
Map<String, Object> attributes = new HashMap<>();
53-
attributes.put("defaultChannel",
54-
enablePublisherElement.getAttribute("default-publisher-channel"));
55-
attributes.put("proxyTargetClass",
56-
enablePublisherElement.getAttribute("proxy-target-class"));
57-
attributes.put("order", enablePublisherElement.getAttribute("order"));
58-
return attributes;
59-
}
60-
else {
61-
return null;
62-
}
63-
}
64-
else {
65-
return null;
66-
}
67-
}
49+
private static final class ExtendedAnnotationMetadata extends StandardAnnotationMetadata {
6850

69-
};
51+
private final Element element;
7052

71-
new IntegrationRegistrar().registerBeanDefinitions(importingClassMetadata, parserContext.getRegistry());
53+
ExtendedAnnotationMetadata(Class<?> introspectedClass, Element element) {
54+
super(introspectedClass);
55+
this.element = element;
56+
}
57+
58+
@Override
59+
public Map<String, Object> getAnnotationAttributes(String annotationType) {
60+
if (EnablePublisher.class.getName().equals(annotationType)) {
61+
Element enablePublisherElement =
62+
DomUtils.getChildElementByTagName(this.element, "enable-publisher");
63+
if (enablePublisherElement != null) {
64+
Map<String, Object> attributes = new HashMap<>();
65+
attributes.put("defaultChannel",
66+
enablePublisherElement.getAttribute("default-publisher-channel"));
67+
attributes.put("proxyTargetClass",
68+
enablePublisherElement.getAttribute("proxy-target-class"));
69+
attributes.put("order", enablePublisherElement.getAttribute("order"));
70+
return attributes;
71+
}
72+
else {
73+
return null;
74+
}
75+
}
76+
else {
77+
return null;
78+
}
79+
}
7280

73-
return null;
7481
}
7582

7683
}

spring-integration-core/src/main/java/org/springframework/integration/endpoint/ReactiveStreamsConsumer.java

Lines changed: 37 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -132,38 +132,7 @@ protected void doStart() {
132132
if (this.lifecycleDelegate != null) {
133133
this.lifecycleDelegate.start();
134134
}
135-
this.publisher.subscribe(new BaseSubscriber<Message<?>>() {
136-
137-
private final Subscriber<Message<?>> delegate = ReactiveStreamsConsumer.this.subscriber;
138-
139-
@Override
140-
public void hookOnSubscribe(Subscription s) {
141-
this.delegate.onSubscribe(s);
142-
ReactiveStreamsConsumer.this.subscription = s;
143-
}
144-
145-
@Override
146-
public void hookOnNext(Message<?> message) {
147-
try {
148-
this.delegate.onNext(message);
149-
}
150-
catch (Exception e) {
151-
ReactiveStreamsConsumer.this.errorHandler.handleError(e);
152-
hookOnError(e);
153-
}
154-
}
155-
156-
@Override
157-
public void hookOnError(Throwable t) {
158-
this.delegate.onError(t);
159-
}
160-
161-
@Override
162-
public void hookOnComplete() {
163-
this.delegate.onComplete();
164-
}
165-
166-
});
135+
this.publisher.subscribe(new DelegatingSubscriber());
167136
}
168137

169138
@Override
@@ -176,6 +145,42 @@ protected void doStop() {
176145
}
177146
}
178147

148+
private final class DelegatingSubscriber extends BaseSubscriber<Message<?>> {
149+
150+
private final Subscriber<Message<?>> delegate = ReactiveStreamsConsumer.this.subscriber;
151+
152+
DelegatingSubscriber() {
153+
super();
154+
}
155+
156+
@Override
157+
public void hookOnSubscribe(Subscription s) {
158+
this.delegate.onSubscribe(s);
159+
ReactiveStreamsConsumer.this.subscription = s;
160+
}
161+
162+
@Override
163+
public void hookOnNext(Message<?> message) {
164+
try {
165+
this.delegate.onNext(message);
166+
}
167+
catch (Exception e) {
168+
ReactiveStreamsConsumer.this.errorHandler.handleError(e);
169+
hookOnError(e);
170+
}
171+
}
172+
173+
@Override
174+
public void hookOnError(Throwable t) {
175+
this.delegate.onError(t);
176+
}
177+
178+
@Override
179+
public void hookOnComplete() {
180+
this.delegate.onComplete();
181+
}
182+
183+
}
179184

180185
private static final class MessageHandlerSubscriber
181186
implements CoreSubscriber<Message<?>>, Disposable, Lifecycle {

spring-integration-core/src/main/java/org/springframework/integration/gateway/GatewayProxyFactoryBean.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,7 @@ else if (Future.class.isAssignableFrom(returnType)) {
465465
}
466466

467467
@Nullable
468-
protected Object doInvoke(MethodInvocation invocation, boolean runningOnCallerThread) throws Throwable {
468+
protected Object doInvoke(MethodInvocation invocation, boolean runningOnCallerThread) throws Throwable { // NOSONAR
469469
Method method = invocation.getMethod();
470470
if (AopUtils.isToStringMethod(method)) {
471471
return "gateway proxy for service interface [" + this.serviceInterface + "]";
@@ -474,7 +474,7 @@ protected Object doInvoke(MethodInvocation invocation, boolean runningOnCallerTh
474474
return invokeGatewayMethod(invocation, runningOnCallerThread);
475475
}
476476
catch (Throwable e) { //NOSONAR - ok to catch, rethrown below
477-
this.rethrowExceptionCauseIfPossible(e, invocation.getMethod());
477+
rethrowExceptionCauseIfPossible(e, invocation.getMethod());
478478
return null; // preceding call should always throw something
479479
}
480480
}
@@ -538,7 +538,7 @@ else if (this.globalMethodMetadata != null) {
538538
return (response != null) ? this.convert(response, returnType) : null;
539539
}
540540

541-
private void rethrowExceptionCauseIfPossible(Throwable originalException, Method method) throws Throwable {
541+
private void rethrowExceptionCauseIfPossible(Throwable originalException, Method method) throws Throwable { // NOSONAR
542542
Class<?>[] exceptionTypes = method.getExceptionTypes();
543543
Throwable t = originalException;
544544
while (t != null) {

spring-integration-core/src/main/java/org/springframework/integration/handler/advice/AbstractHandleMessageAdvice.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
import java.lang.reflect.Method;
2020

2121
import org.aopalliance.intercept.MethodInvocation;
22-
import org.apache.commons.logging.Log;
23-
import org.apache.commons.logging.LogFactory;
2422

2523
import org.springframework.integration.context.IntegrationObjectSupport;
2624
import org.springframework.messaging.Message;
@@ -37,10 +35,8 @@
3735
*/
3836
public abstract class AbstractHandleMessageAdvice extends IntegrationObjectSupport implements HandleMessageAdvice {
3937

40-
protected final Log logger = LogFactory.getLog(this.getClass());
41-
4238
@Override
43-
public final Object invoke(MethodInvocation invocation) throws Throwable {
39+
public final Object invoke(MethodInvocation invocation) throws Throwable { // NOSONAR
4440
Method method = invocation.getMethod();
4541
Object invocationThis = invocation.getThis();
4642
Object[] arguments = invocation.getArguments();

spring-integration-core/src/main/java/org/springframework/integration/handler/advice/AbstractRequestHandlerAdvice.java

Lines changed: 45 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -64,42 +64,7 @@ public final Object invoke(final MethodInvocation invocation) throws Throwable {
6464
else {
6565
Message<?> message = (Message<?>) arguments[0];
6666
try {
67-
return doInvoke(new ExecutionCallback() {
68-
69-
@Override
70-
public Object execute() {
71-
try {
72-
return invocation.proceed();
73-
}
74-
catch (Throwable e) { //NOSONAR - ok to catch; unwrapped and rethrown below
75-
throw new ThrowableHolderException(e);
76-
}
77-
}
78-
79-
@Override
80-
public Object cloneAndExecute() {
81-
try {
82-
/*
83-
* If we don't copy the invocation carefully it won't keep a reference to the other
84-
* interceptors in the chain.
85-
*/
86-
if (invocation instanceof ProxyMethodInvocation) {
87-
return ((ProxyMethodInvocation) invocation).invocableClone().proceed();
88-
}
89-
else {
90-
throw new IllegalStateException(
91-
"MethodInvocation of the wrong type detected - this should not happen with Spring AOP," +
92-
" so please raise an issue if you see this exception");
93-
}
94-
}
95-
catch (Exception e) { //NOSONAR - catch necessary so we can wrap Errors
96-
throw new MessagingException(message, "Failed to handle", e);
97-
}
98-
catch (Throwable e) { //NOSONAR - ok to catch; unwrapped and rethrown below
99-
throw new ThrowableHolderException(e);
100-
}
101-
}
102-
}, invocationThis, message);
67+
return doInvoke(new CallbackImpl(invocation), invocationThis, message);
10368
}
10469
catch (Exception e) {
10570
throw this.unwrapThrowableIfNecessary(e);
@@ -172,6 +137,50 @@ protected interface ExecutionCallback {
172137

173138
}
174139

140+
private static final class CallbackImpl implements ExecutionCallback {
141+
142+
private final MethodInvocation invocation;
143+
144+
CallbackImpl(MethodInvocation invocation) {
145+
this.invocation = invocation;
146+
}
147+
148+
@Override
149+
public Object execute() {
150+
try {
151+
return this.invocation.proceed();
152+
}
153+
catch (Throwable e) { //NOSONAR - ok to catch; unwrapped and rethrown below
154+
throw new ThrowableHolderException(e);
155+
}
156+
}
157+
158+
@Override
159+
public Object cloneAndExecute() {
160+
try {
161+
/*
162+
* If we don't copy the invocation carefully it won't keep a reference to the other
163+
* interceptors in the chain.
164+
*/
165+
if (this.invocation instanceof ProxyMethodInvocation) {
166+
return ((ProxyMethodInvocation) this.invocation).invocableClone().proceed();
167+
}
168+
else {
169+
throw new IllegalStateException(
170+
"MethodInvocation of the wrong type detected - this should not happen with Spring AOP," +
171+
" so please raise an issue if you see this exception");
172+
}
173+
}
174+
catch (Exception e) { //NOSONAR - catch necessary so we can wrap Errors
175+
throw new MessagingException((Message<?>) this.invocation.getArguments()[0], "Failed to handle", e);
176+
}
177+
catch (Throwable e) { //NOSONAR - ok to catch; unwrapped and rethrown below
178+
throw new ThrowableHolderException(e);
179+
}
180+
}
181+
182+
}
183+
175184
@SuppressWarnings("serial")
176185
protected static final class ThrowableHolderException extends RuntimeException {
177186

spring-integration-core/src/main/java/org/springframework/integration/metadata/PropertiesPersistingMetadataStore.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,13 @@ public void setFileName(String fileName) {
9393
@Override
9494
public void afterPropertiesSet() {
9595
File baseDir = new File(this.baseDirectory);
96-
baseDir.mkdirs();
96+
if (!baseDir.mkdirs() && this.logger.isWarnEnabled()) {
97+
this.logger.warn("Failed to create directories for " + baseDir);
98+
}
9799
this.file = new File(baseDir, this.fileName);
98100
try {
99-
if (!this.file.exists()) {
100-
this.file.createNewFile();
101+
if (!this.file.exists() && !this.file.createNewFile() && this.logger.isWarnEnabled()) {
102+
this.logger.warn("Failed to create file " + this.file);
101103
}
102104
}
103105
catch (Exception e) {

0 commit comments

Comments
 (0)