24
24
import groovy .lang .MetaClass ;
25
25
import groovy .lang .Script ;
26
26
import org .codehaus .groovy .control .CompilationFailedException ;
27
+ import org .codehaus .groovy .control .CompilerConfiguration ;
28
+ import org .codehaus .groovy .control .customizers .CompilationCustomizer ;
27
29
28
30
import org .springframework .beans .factory .BeanClassLoaderAware ;
29
31
import org .springframework .beans .factory .BeanFactory ;
@@ -57,7 +59,9 @@ public class GroovyScriptFactory implements ScriptFactory, BeanFactoryAware, Bea
57
59
58
60
private final String scriptSourceLocator ;
59
61
60
- private final GroovyObjectCustomizer groovyObjectCustomizer ;
62
+ private GroovyObjectCustomizer groovyObjectCustomizer ;
63
+
64
+ private CompilerConfiguration compilerConfiguration ;
61
65
62
66
private GroovyClassLoader groovyClassLoader ;
63
67
@@ -80,27 +84,46 @@ public class GroovyScriptFactory implements ScriptFactory, BeanFactoryAware, Bea
80
84
* Interpreted by the post-processor that actually creates the script.
81
85
*/
82
86
public GroovyScriptFactory (String scriptSourceLocator ) {
83
- this (scriptSourceLocator , null );
87
+ Assert .hasText (scriptSourceLocator , "'scriptSourceLocator' must not be empty" );
88
+ this .scriptSourceLocator = scriptSourceLocator ;
84
89
}
85
90
86
91
/**
87
92
* Create a new GroovyScriptFactory for the given script source,
88
93
* specifying a strategy interface that can create a custom MetaClass
89
94
* to supply missing methods and otherwise change the behavior of the object.
90
- * <p>We don't need to specify script interfaces here, since
91
- * a Groovy script defines its Java interfaces itself.
92
95
* @param scriptSourceLocator a locator that points to the source of the script.
93
96
* Interpreted by the post-processor that actually creates the script.
94
97
* @param groovyObjectCustomizer a customizer that can set a custom metaclass
95
98
* or make other changes to the GroovyObject created by this factory
96
99
* (may be {@code null})
100
+ * @see GroovyObjectCustomizer#customize
97
101
*/
98
102
public GroovyScriptFactory (String scriptSourceLocator , GroovyObjectCustomizer groovyObjectCustomizer ) {
99
- Assert .hasText (scriptSourceLocator , "'scriptSourceLocator' must not be empty" );
100
- this .scriptSourceLocator = scriptSourceLocator ;
103
+ this (scriptSourceLocator );
101
104
this .groovyObjectCustomizer = groovyObjectCustomizer ;
102
105
}
103
106
107
+ /**
108
+ * Create a new GroovyScriptFactory for the given script source,
109
+ * specifying a strategy interface that can customize Groovy's compilation
110
+ * process within the underlying GroovyClassLoader.
111
+ * @param scriptSourceLocator a locator that points to the source of the script.
112
+ * 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})
115
+ * @since 4.3.3
116
+ * @see CompilerConfiguration#addCompilationCustomizers
117
+ * @see org.codehaus.groovy.control.customizers.ImportCustomizer
118
+ */
119
+ public GroovyScriptFactory (String scriptSourceLocator , CompilationCustomizer compilationCustomizer ) {
120
+ this (scriptSourceLocator );
121
+ if (compilationCustomizer != null ) {
122
+ this .compilerConfiguration = new CompilerConfiguration ();
123
+ this .compilerConfiguration .addCompilationCustomizers (compilationCustomizer );
124
+ }
125
+ }
126
+
104
127
105
128
@ Override
106
129
public void setBeanFactory (BeanFactory beanFactory ) {
@@ -111,7 +134,7 @@ public void setBeanFactory(BeanFactory beanFactory) {
111
134
112
135
@ Override
113
136
public void setBeanClassLoader (ClassLoader classLoader ) {
114
- this .groovyClassLoader = new GroovyClassLoader (classLoader );
137
+ this .groovyClassLoader = buildGroovyClassLoader (classLoader );
115
138
}
116
139
117
140
/**
@@ -120,12 +143,22 @@ public void setBeanClassLoader(ClassLoader classLoader) {
120
143
public GroovyClassLoader getGroovyClassLoader () {
121
144
synchronized (this .scriptClassMonitor ) {
122
145
if (this .groovyClassLoader == null ) {
123
- this .groovyClassLoader = new GroovyClassLoader (ClassUtils .getDefaultClassLoader ());
146
+ this .groovyClassLoader = buildGroovyClassLoader (ClassUtils .getDefaultClassLoader ());
124
147
}
125
148
return this .groovyClassLoader ;
126
149
}
127
150
}
128
151
152
+ /**
153
+ * Build a {@link GroovyClassLoader} for the given {@code ClassLoader}.
154
+ * @param classLoader the ClassLoader to build a GroovyClassLoader for
155
+ * @since 4.3.3
156
+ */
157
+ protected GroovyClassLoader buildGroovyClassLoader (ClassLoader classLoader ) {
158
+ return (this .compilerConfiguration != null ?
159
+ new GroovyClassLoader (classLoader , this .compilerConfiguration ) : new GroovyClassLoader (classLoader ));
160
+ }
161
+
129
162
130
163
@ Override
131
164
public String getScriptSourceLocator () {
0 commit comments