Skip to content

Commit 6a0d9d3

Browse files
committed
Consistent support for CompilationCustomizers as well as custom CompilerConfiguration
Issue: SPR-14585
1 parent a1b167a commit 6a0d9d3

File tree

6 files changed

+128
-10
lines changed

6 files changed

+128
-10
lines changed

spring-context/src/main/java/org/springframework/scripting/groovy/GroovyScriptEvaluator.java

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,6 +22,8 @@
2222
import groovy.lang.Binding;
2323
import groovy.lang.GroovyRuntimeException;
2424
import groovy.lang.GroovyShell;
25+
import org.codehaus.groovy.control.CompilerConfiguration;
26+
import org.codehaus.groovy.control.customizers.CompilationCustomizer;
2527

2628
import org.springframework.beans.factory.BeanClassLoaderAware;
2729
import org.springframework.scripting.ScriptCompilationException;
@@ -40,6 +42,8 @@ public class GroovyScriptEvaluator implements ScriptEvaluator, BeanClassLoaderAw
4042

4143
private ClassLoader classLoader;
4244

45+
private CompilerConfiguration compilerConfiguration = new CompilerConfiguration();
46+
4347

4448
/**
4549
* Construct a new GroovyScriptEvaluator.
@@ -56,6 +60,35 @@ public GroovyScriptEvaluator(ClassLoader classLoader) {
5660
}
5761

5862

63+
/**
64+
* Set a custom compiler configuration for this evaluator.
65+
* @since 4.3.3
66+
* @see #setCompilationCustomizers
67+
*/
68+
public void setCompilerConfiguration(CompilerConfiguration compilerConfiguration) {
69+
this.compilerConfiguration =
70+
(compilerConfiguration != null ? compilerConfiguration : new CompilerConfiguration());
71+
}
72+
73+
/**
74+
* Return this evaluator's compiler configuration (never {@code null}).
75+
* @since 4.3.3
76+
* @see #setCompilerConfiguration
77+
*/
78+
public CompilerConfiguration getCompilerConfiguration() {
79+
return this.compilerConfiguration;
80+
}
81+
82+
/**
83+
* Set one or more customizers to be applied to this evaluator's compiler configuration.
84+
* <p>Note that this modifies the shared compiler configuration held by this evaluator.
85+
* @since 4.3.3
86+
* @see #setCompilerConfiguration
87+
*/
88+
public void setCompilationCustomizers(CompilationCustomizer... compilationCustomizers) {
89+
this.compilerConfiguration.addCompilationCustomizers(compilationCustomizers);
90+
}
91+
5992
@Override
6093
public void setBeanClassLoader(ClassLoader classLoader) {
6194
this.classLoader = classLoader;
@@ -69,7 +102,8 @@ public Object evaluate(ScriptSource script) {
69102

70103
@Override
71104
public Object evaluate(ScriptSource script, Map<String, Object> arguments) {
72-
GroovyShell groovyShell = new GroovyShell(this.classLoader, new Binding(arguments));
105+
GroovyShell groovyShell = new GroovyShell(
106+
this.classLoader, new Binding(arguments), this.compilerConfiguration);
73107
try {
74108
String filename = (script instanceof ResourceScriptSource ?
75109
((ResourceScriptSource) script).getResource().getFilename() : null);

spring-context/src/main/java/org/springframework/scripting/groovy/GroovyScriptFactory.java

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import org.springframework.scripting.ScriptSource;
3737
import org.springframework.util.Assert;
3838
import org.springframework.util.ClassUtils;
39+
import org.springframework.util.ObjectUtils;
3940
import org.springframework.util.ReflectionUtils;
4041

4142
/**
@@ -104,23 +105,39 @@ public GroovyScriptFactory(String scriptSourceLocator, GroovyObjectCustomizer gr
104105
this.groovyObjectCustomizer = groovyObjectCustomizer;
105106
}
106107

108+
/**
109+
* Create a new GroovyScriptFactory for the given script source,
110+
* specifying a strategy interface that can create a custom MetaClass
111+
* to supply missing methods and otherwise change the behavior of the object.
112+
* @param scriptSourceLocator a locator that points to the source of the script.
113+
* Interpreted by the post-processor that actually creates the script.
114+
* @param compilerConfiguration a custom compiler configuration to be applied
115+
* to the GroovyClassLoader (may be {@code null})
116+
* @since 4.3.3
117+
* @see GroovyClassLoader#GroovyClassLoader(ClassLoader, CompilerConfiguration)
118+
*/
119+
public GroovyScriptFactory(String scriptSourceLocator, CompilerConfiguration compilerConfiguration) {
120+
this(scriptSourceLocator);
121+
this.compilerConfiguration = compilerConfiguration;
122+
}
123+
107124
/**
108125
* Create a new GroovyScriptFactory for the given script source,
109126
* specifying a strategy interface that can customize Groovy's compilation
110127
* process within the underlying GroovyClassLoader.
111128
* @param scriptSourceLocator a locator that points to the source of the script.
112129
* Interpreted by the post-processor that actually creates the script.
113-
* @param compilationCustomizer a customizer to be applied to the GroovyClassLoader
114-
* compiler configuration (may be {@code null})
130+
* @param compilationCustomizers one or more customizers to be applied to the
131+
* GroovyClassLoader compiler configuration
115132
* @since 4.3.3
116133
* @see CompilerConfiguration#addCompilationCustomizers
117134
* @see org.codehaus.groovy.control.customizers.ImportCustomizer
118135
*/
119-
public GroovyScriptFactory(String scriptSourceLocator, CompilationCustomizer compilationCustomizer) {
136+
public GroovyScriptFactory(String scriptSourceLocator, CompilationCustomizer... compilationCustomizers) {
120137
this(scriptSourceLocator);
121-
if (compilationCustomizer != null) {
138+
if (!ObjectUtils.isEmpty(compilationCustomizers)) {
122139
this.compilerConfiguration = new CompilerConfiguration();
123-
this.compilerConfiguration.addCompilationCustomizers(compilationCustomizer);
140+
this.compilerConfiguration.addCompilationCustomizers(compilationCustomizers);
124141
}
125142
}
126143

spring-context/src/test/java/org/springframework/scripting/groovy/GroovyScriptEvaluatorTests.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.HashMap;
2020
import java.util.Map;
2121

22+
import org.codehaus.groovy.control.customizers.ImportCustomizer;
2223
import org.junit.Test;
2324

2425
import org.springframework.core.io.ClassPathResource;
@@ -58,6 +59,26 @@ public void testGroovyScriptWithArguments() {
5859
assertEquals(6, result);
5960
}
6061

62+
@Test
63+
public void testGroovyScriptWithCompilerConfiguration() {
64+
GroovyScriptEvaluator evaluator = new GroovyScriptEvaluator();
65+
MyBytecodeProcessor processor = new MyBytecodeProcessor();
66+
evaluator.getCompilerConfiguration().setBytecodePostprocessor(processor);
67+
Object result = evaluator.evaluate(new StaticScriptSource("return 3 * 2"));
68+
assertEquals(6, result);
69+
assertTrue(processor.processed.contains("Script1"));
70+
}
71+
72+
@Test
73+
public void testGroovyScriptWithImportCustomizer() {
74+
GroovyScriptEvaluator evaluator = new GroovyScriptEvaluator();
75+
ImportCustomizer importCustomizer = new ImportCustomizer();
76+
importCustomizer.addStarImports("org.springframework.util");
77+
evaluator.setCompilationCustomizers(importCustomizer);
78+
Object result = evaluator.evaluate(new StaticScriptSource("return ResourceUtils.CLASSPATH_URL_PREFIX"));
79+
assertEquals("classpath:", result);
80+
}
81+
6182
@Test
6283
public void testGroovyScriptFromStringUsingJsr223() {
6384
StandardScriptEvaluator evaluator = new StandardScriptEvaluator();

spring-context/src/test/java/org/springframework/scripting/groovy/GroovyScriptFactoryTests.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -470,6 +470,8 @@ public void testAnonymousScriptDetected() throws Exception {
470470
ApplicationContext ctx = new ClassPathXmlApplicationContext("groovy-with-xsd.xml", getClass());
471471
Map<?, Messenger> beans = ctx.getBeansOfType(Messenger.class);
472472
assertEquals(4, beans.size());
473+
assertTrue(ctx.getBean(MyBytecodeProcessor.class).processed.contains(
474+
"org.springframework.scripting.groovy.GroovyMessenger2"));
473475
}
474476

475477
@Test
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2002-2016 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.scripting.groovy;
18+
19+
import java.util.HashSet;
20+
import java.util.Set;
21+
22+
import org.codehaus.groovy.control.BytecodeProcessor;
23+
24+
/**
25+
* @author Juergen Hoeller
26+
*/
27+
public class MyBytecodeProcessor implements BytecodeProcessor {
28+
29+
public final Set<String> processed = new HashSet<String>();
30+
31+
@Override
32+
public byte[] processBytecode(String name, byte[] original) {
33+
this.processed.add(name);
34+
return original;
35+
}
36+
37+
}

spring-context/src/test/resources/org/springframework/scripting/groovy/groovy-with-xsd.xml

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ class GroovyCalculator implements Calculator {
3939
<lang:groovy id="groovyObjectCustomizer" customizer-ref="importCustomizer">
4040
<lang:inline-script><![CDATA[
4141
public class TestCustomizer implements GroovyObjectCustomizer {
42-
public void customize(GroovyObject o) {
43-
println "customizing ${o}.."
42+
public void customize(GroovyObject go) {
43+
println "customizing ${go}..."
4444
}
4545
}]]>
4646
</lang:inline-script>
@@ -53,8 +53,15 @@ public class TestCustomizer implements GroovyObjectCustomizer {
5353
<lang:property name="message" value="Hello World!"/>
5454
</lang:groovy>
5555

56-
<lang:groovy script-source="classpath:org/springframework/scripting/groovy/Messenger.groovy">
56+
<lang:groovy script-source="classpath:org/springframework/scripting/groovy/Messenger.groovy"
57+
customizer-ref="compilerConfiguration">
5758
<lang:property name="message" value="Hello World!"/>
5859
</lang:groovy>
5960

61+
<bean id="compilerConfiguration" class="org.codehaus.groovy.control.CompilerConfiguration">
62+
<property name="bytecodePostprocessor" ref="bytecodeProcessor"/>
63+
</bean>
64+
65+
<bean id="bytecodeProcessor" class="org.springframework.scripting.groovy.MyBytecodeProcessor"/>
66+
6067
</beans>

0 commit comments

Comments
 (0)