Skip to content

Commit be86db5

Browse files
committed
Fix new Sonar smells for scripts
Fix `ImapMailReceiverTests.testConnectionException()` for several `ImapIdleExceptionEvent` instances
1 parent 7dff1d5 commit be86db5

File tree

5 files changed

+32
-26
lines changed

5 files changed

+32
-26
lines changed

spring-integration-groovy/src/main/java/org/springframework/integration/groovy/GroovyCommandMessageProcessor.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
*/
4848
public class GroovyCommandMessageProcessor extends AbstractScriptExecutingMessageProcessor<Object> {
4949

50-
private volatile GroovyObjectCustomizer customizer;
50+
private GroovyObjectCustomizer customizer;
5151

5252
private Binding binding;
5353

@@ -125,20 +125,15 @@ protected Object executeScript(ScriptSource scriptSource, Map<String, Object> va
125125
customizerDecorator.setVariables(variables);
126126
}
127127
GroovyScriptFactory factory = new GroovyScriptFactory(getClass().getSimpleName(), customizerDecorator);
128-
if (this.beanClassLoader != null) {
129-
factory.setBeanClassLoader(this.beanClassLoader);
130-
}
131-
if (this.beanFactory != null) {
132-
factory.setBeanFactory(this.beanFactory);
133-
}
128+
factory.setBeanClassLoader(getBeanClassLoader());
129+
factory.setBeanFactory(getBeanFactory());
134130
try {
135131
Object result = factory.getScriptedObject(scriptSource);
136132
return (result instanceof GString) ? result.toString() : result;
137133
}
138134
catch (IOException e) {
139135
throw new UncheckedIOException(e);
140136
}
141-
142137
}
143138

144139
protected String generateScriptName(Message<?> message) {

spring-integration-groovy/src/main/java/org/springframework/integration/groovy/GroovyScriptExecutingMessageProcessor.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.codehaus.groovy.control.CompilerConfiguration;
2828
import org.codehaus.groovy.control.customizers.ASTTransformationCustomizer;
2929

30+
import org.springframework.beans.factory.BeanFactory;
3031
import org.springframework.beans.factory.InitializingBean;
3132
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
3233
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
@@ -144,8 +145,9 @@ protected ScriptSource getScriptSource(Message<?> message) {
144145

145146
@Override
146147
public void afterPropertiesSet() {
147-
if (this.beanFactory != null && this.beanFactory instanceof ConfigurableListableBeanFactory) {
148-
((ConfigurableListableBeanFactory) this.beanFactory).ignoreDependencyType(MetaClass.class);
148+
BeanFactory beanFactory = getBeanFactory();
149+
if (beanFactory instanceof ConfigurableListableBeanFactory) {
150+
((ConfigurableListableBeanFactory) beanFactory).ignoreDependencyType(MetaClass.class);
149151
}
150152

151153
CompilerConfiguration compilerConfig = this.compilerConfiguration;
@@ -154,7 +156,7 @@ public void afterPropertiesSet() {
154156
compilerConfig.addCompilationCustomizers(new ASTTransformationCustomizer(CompileStatic.class));
155157
}
156158

157-
this.groovyClassLoader = new GroovyClassLoader(this.beanClassLoader, compilerConfig);
159+
this.groovyClassLoader = new GroovyClassLoader(getBeanClassLoader(), compilerConfig);
158160
}
159161

160162
@Override
@@ -218,8 +220,7 @@ private Object execute(Map<String, Object> variables) throws ScriptCompilationEx
218220
}
219221
catch (IllegalAccessException ex) {
220222
throw new ScriptCompilationException(
221-
this.scriptSource, "Could not access Groovy script constructor: " + this.scriptClass.getName(),
222-
ex);
223+
this.scriptSource, "Could not access Groovy script constructor: " + this.scriptClass.getName(), ex);
223224
}
224225
}
225226

@@ -234,20 +235,20 @@ public Object getVariable(String name) {
234235
try {
235236
return super.getVariable(name);
236237
}
237-
catch (MissingPropertyException e) {
238+
catch (MissingPropertyException ex) {
238239
// Original {@link Binding} doesn't have 'variable' for the given 'name'.
239240
// Try to resolve it as 'bean' from the given <code>beanFactory</code>.
240241
}
241242

242-
if (GroovyScriptExecutingMessageProcessor.this.beanFactory == null) {
243-
throw new MissingPropertyException(name, this.getClass());
243+
BeanFactory beanFactory = getBeanFactory();
244+
if (beanFactory == null) {
245+
throw new MissingPropertyException(name, getClass());
244246
}
245-
246247
try {
247-
return GroovyScriptExecutingMessageProcessor.this.beanFactory.getBean(name);
248+
return beanFactory.getBean(name);
248249
}
249250
catch (NoSuchBeanDefinitionException e) {
250-
throw new MissingPropertyException(name, this.getClass(), e);
251+
throw new MissingPropertyException(name, getClass(), e);
251252
}
252253
}
253254

spring-integration-mail/src/test/java/org/springframework/integration/mail/ImapMailReceiverTests.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@
7474
import org.springframework.integration.channel.QueueChannel;
7575
import org.springframework.integration.handler.AbstractReplyProducingMessageHandler;
7676
import org.springframework.integration.history.MessageHistory;
77-
import org.springframework.integration.mail.ImapIdleChannelAdapter.ImapIdleExceptionEvent;
7877
import org.springframework.integration.mail.support.DefaultMailHeaderMapper;
7978
import org.springframework.integration.test.mail.TestMailServer;
8079
import org.springframework.integration.test.mail.TestMailServer.ImapServer;
@@ -689,11 +688,10 @@ public void testInitialIdleDelayWhenRecentIsSupported() throws Exception {
689688
public void testConnectionException() throws Exception {
690689
ImapMailReceiver mailReceiver = new ImapMailReceiver("imap:foo");
691690
ImapIdleChannelAdapter adapter = new ImapIdleChannelAdapter(mailReceiver);
692-
final AtomicReference<ImapIdleExceptionEvent> theEvent = new AtomicReference<>();
691+
final AtomicReference<Object> theEvent = new AtomicReference<>();
693692
final CountDownLatch latch = new CountDownLatch(1);
694693
adapter.setApplicationEventPublisher(event -> {
695-
assertThat(theEvent.get()).as("only one event expected").isNull();
696-
theEvent.set((ImapIdleExceptionEvent) event);
694+
theEvent.set(event);
697695
latch.countDown();
698696
});
699697
ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();

spring-integration-scripting/src/main/java/org/springframework/integration/scripting/AbstractScriptExecutingMessageProcessor.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ public abstract class AbstractScriptExecutingMessageProcessor<T>
4343

4444
private final ScriptVariableGenerator scriptVariableGenerator;
4545

46-
protected ClassLoader beanClassLoader;
46+
private ClassLoader beanClassLoader;
4747

48-
protected BeanFactory beanFactory;
48+
private BeanFactory beanFactory;
4949

5050
protected AbstractScriptExecutingMessageProcessor() {
5151
this(new DefaultScriptVariableGenerator());
@@ -67,6 +67,18 @@ public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
6767
this.beanFactory = beanFactory;
6868
}
6969

70+
protected ScriptVariableGenerator getScriptVariableGenerator() {
71+
return this.scriptVariableGenerator;
72+
}
73+
74+
protected ClassLoader getBeanClassLoader() {
75+
return this.beanClassLoader;
76+
}
77+
78+
protected BeanFactory getBeanFactory() {
79+
return this.beanFactory;
80+
}
81+
7082
/**
7183
* Executes the script and returns the result.
7284
*/

spring-integration-scripting/src/main/java/org/springframework/integration/scripting/jsr223/ScriptExecutorFactory.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ else if (language.equalsIgnoreCase("kotlin")) {
5252
* @since 5.2
5353
*/
5454
public static String deriveLanguageFromFileExtension(String scriptLocation) {
55-
int index = scriptLocation.lastIndexOf(".") + 1;
55+
int index = scriptLocation.lastIndexOf('.') + 1;
5656
Assert.state(index > 0, () -> "Unable to determine language for script '" + scriptLocation + "'");
5757
String extension = scriptLocation.substring(index);
5858
if (extension.equals("kts")) {

0 commit comments

Comments
 (0)