Skip to content

Commit fbeff47

Browse files
committed
Consistent support for CompilationCustomizers as well as custom CompilerConfiguration
Issue: SPR-14585 (cherry picked from commit 6a0d9d3)
1 parent d057099 commit fbeff47

File tree

8 files changed

+212
-24
lines changed

8 files changed

+212
-24
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: 60 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@
1717
package org.springframework.scripting.groovy;
1818

1919
import java.io.IOException;
20+
import java.lang.reflect.InvocationTargetException;
2021

2122
import groovy.lang.GroovyClassLoader;
2223
import groovy.lang.GroovyObject;
2324
import groovy.lang.MetaClass;
2425
import groovy.lang.Script;
2526
import org.codehaus.groovy.control.CompilationFailedException;
27+
import org.codehaus.groovy.control.CompilerConfiguration;
28+
import org.codehaus.groovy.control.customizers.CompilationCustomizer;
2629

2730
import org.springframework.beans.factory.BeanClassLoaderAware;
2831
import org.springframework.beans.factory.BeanFactory;
@@ -33,6 +36,8 @@
3336
import org.springframework.scripting.ScriptSource;
3437
import org.springframework.util.Assert;
3538
import org.springframework.util.ClassUtils;
39+
import org.springframework.util.ObjectUtils;
40+
import org.springframework.util.ReflectionUtils;
3641

3742
/**
3843
* {@link org.springframework.scripting.ScriptFactory} implementation
@@ -55,7 +60,9 @@ public class GroovyScriptFactory implements ScriptFactory, BeanFactoryAware, Bea
5560

5661
private final String scriptSourceLocator;
5762

58-
private final GroovyObjectCustomizer groovyObjectCustomizer;
63+
private GroovyObjectCustomizer groovyObjectCustomizer;
64+
65+
private CompilerConfiguration compilerConfiguration;
5966

6067
private GroovyClassLoader groovyClassLoader;
6168

@@ -78,27 +85,62 @@ public class GroovyScriptFactory implements ScriptFactory, BeanFactoryAware, Bea
7885
* Interpreted by the post-processor that actually creates the script.
7986
*/
8087
public GroovyScriptFactory(String scriptSourceLocator) {
81-
this(scriptSourceLocator, null);
88+
Assert.hasText(scriptSourceLocator, "'scriptSourceLocator' must not be empty");
89+
this.scriptSourceLocator = scriptSourceLocator;
8290
}
8391

8492
/**
8593
* Create a new GroovyScriptFactory for the given script source,
8694
* specifying a strategy interface that can create a custom MetaClass
8795
* to supply missing methods and otherwise change the behavior of the object.
88-
* <p>We don't need to specify script interfaces here, since
89-
* a Groovy script defines its Java interfaces itself.
9096
* @param scriptSourceLocator a locator that points to the source of the script.
9197
* Interpreted by the post-processor that actually creates the script.
9298
* @param groovyObjectCustomizer a customizer that can set a custom metaclass
9399
* or make other changes to the GroovyObject created by this factory
94100
* (may be {@code null})
101+
* @see GroovyObjectCustomizer#customize
95102
*/
96103
public GroovyScriptFactory(String scriptSourceLocator, GroovyObjectCustomizer groovyObjectCustomizer) {
97-
Assert.hasText(scriptSourceLocator, "'scriptSourceLocator' must not be empty");
98-
this.scriptSourceLocator = scriptSourceLocator;
104+
this(scriptSourceLocator);
99105
this.groovyObjectCustomizer = groovyObjectCustomizer;
100106
}
101107

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+
124+
/**
125+
* Create a new GroovyScriptFactory for the given script source,
126+
* specifying a strategy interface that can customize Groovy's compilation
127+
* process within the underlying GroovyClassLoader.
128+
* @param scriptSourceLocator a locator that points to the source of the script.
129+
* Interpreted by the post-processor that actually creates the script.
130+
* @param compilationCustomizers one or more customizers to be applied to the
131+
* GroovyClassLoader compiler configuration
132+
* @since 4.3.3
133+
* @see CompilerConfiguration#addCompilationCustomizers
134+
* @see org.codehaus.groovy.control.customizers.ImportCustomizer
135+
*/
136+
public GroovyScriptFactory(String scriptSourceLocator, CompilationCustomizer... compilationCustomizers) {
137+
this(scriptSourceLocator);
138+
if (!ObjectUtils.isEmpty(compilationCustomizers)) {
139+
this.compilerConfiguration = new CompilerConfiguration();
140+
this.compilerConfiguration.addCompilationCustomizers(compilationCustomizers);
141+
}
142+
}
143+
102144

103145
@Override
104146
public void setBeanFactory(BeanFactory beanFactory) {
@@ -109,7 +151,7 @@ public void setBeanFactory(BeanFactory beanFactory) {
109151

110152
@Override
111153
public void setBeanClassLoader(ClassLoader classLoader) {
112-
this.groovyClassLoader = new GroovyClassLoader(classLoader);
154+
this.groovyClassLoader = buildGroovyClassLoader(classLoader);
113155
}
114156

115157
/**
@@ -118,12 +160,22 @@ public void setBeanClassLoader(ClassLoader classLoader) {
118160
public GroovyClassLoader getGroovyClassLoader() {
119161
synchronized (this.scriptClassMonitor) {
120162
if (this.groovyClassLoader == null) {
121-
this.groovyClassLoader = new GroovyClassLoader(ClassUtils.getDefaultClassLoader());
163+
this.groovyClassLoader = buildGroovyClassLoader(ClassUtils.getDefaultClassLoader());
122164
}
123165
return this.groovyClassLoader;
124166
}
125167
}
126168

169+
/**
170+
* Build a {@link GroovyClassLoader} for the given {@code ClassLoader}.
171+
* @param classLoader the ClassLoader to build a GroovyClassLoader for
172+
* @since 4.3.3
173+
*/
174+
protected GroovyClassLoader buildGroovyClassLoader(ClassLoader classLoader) {
175+
return (this.compilerConfiguration != null ?
176+
new GroovyClassLoader(classLoader, this.compilerConfiguration) : new GroovyClassLoader(classLoader));
177+
}
178+
127179

128180
@Override
129181
public String getScriptSourceLocator() {

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
@@ -477,6 +477,8 @@ public void testAnonymousScriptDetected() throws Exception {
477477
ApplicationContext ctx = new ClassPathXmlApplicationContext("groovy-with-xsd.xml", getClass());
478478
Map<?, Messenger> beans = ctx.getBeansOfType(Messenger.class);
479479
assertEquals(4, beans.size());
480+
assertTrue(ctx.getBean(MyBytecodeProcessor.class).processed.contains(
481+
"org.springframework.scripting.groovy.GroovyMessenger2"));
480482
}
481483

482484
@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+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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 org.codehaus.groovy.control.customizers.ImportCustomizer;
20+
21+
/**
22+
* @author Juergen Hoeller
23+
*/
24+
public class MyImportCustomizer extends ImportCustomizer {
25+
26+
public MyImportCustomizer() {
27+
addStarImports("org.springframework.scripting.groovy");
28+
}
29+
30+
}

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

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
33
xmlns:aop="http://www.springframework.org/schema/aop"
44
xmlns:lang="http://www.springframework.org/schema/lang"
5-
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
5+
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
66
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
77
http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.5.xsd">
88

@@ -24,7 +24,7 @@
2424
<lang:property name="message" value="Hello World!"/>
2525
</lang:groovy>
2626

27-
<lang:groovy id="calculator" depends-on="messenger" customizer-ref="customizer">
27+
<lang:groovy id="calculator" depends-on="messenger" customizer-ref="groovyObjectCustomizer">
2828
<lang:inline-script>
2929
package org.springframework.scripting.groovy;
3030
import org.springframework.scripting.Calculator
@@ -36,25 +36,32 @@ class GroovyCalculator implements Calculator {
3636
</lang:inline-script>
3737
</lang:groovy>
3838

39-
<lang:groovy id="customizer">
39+
<lang:groovy id="groovyObjectCustomizer" customizer-ref="importCustomizer">
4040
<lang:inline-script><![CDATA[
41-
import org.springframework.scripting.groovy.GroovyObjectCustomizer;
42-
4341
public class TestCustomizer implements GroovyObjectCustomizer {
44-
public void customize(GroovyObject o) {
45-
println "customizing ${o}.."
42+
public void customize(GroovyObject go) {
43+
println "customizing ${go}..."
4644
}
4745
}]]>
4846
</lang:inline-script>
4947
</lang:groovy>
5048

49+
<bean id="importCustomizer" class="org.springframework.scripting.groovy.MyImportCustomizer"/>
50+
5151
<lang:groovy id="refreshableMessenger" refresh-check-delay="5000"
5252
script-source="classpath:org/springframework/scripting/groovy/Messenger.groovy">
5353
<lang:property name="message" value="Hello World!"/>
5454
</lang:groovy>
5555

56-
<lang:groovy script-source="classpath:org/springframework/scripting/groovy/Messenger.groovy">
57-
<lang:property name="message" value="Hello World!"/>
58-
</lang:groovy>
56+
<lang:groovy script-source="classpath:org/springframework/scripting/groovy/Messenger.groovy"
57+
customizer-ref="compilerConfiguration">
58+
<lang:property name="message" value="Hello World!"/>
59+
</lang:groovy>
60+
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"/>
5966

6067
</beans>

0 commit comments

Comments
 (0)