Consistent support for CompilationCustomizers as well as custom CompilerConfiguration

Issue: SPR-14585
(cherry picked from commit 6a0d9d3)
This commit is contained in:
Juergen Hoeller
2016-08-17 11:29:49 +02:00
parent d057099c17
commit fbeff475b3
8 changed files with 212 additions and 24 deletions

View File

@@ -19,6 +19,7 @@ package org.springframework.scripting.groovy;
import java.util.HashMap;
import java.util.Map;
import org.codehaus.groovy.control.customizers.ImportCustomizer;
import org.junit.Test;
import org.springframework.core.io.ClassPathResource;
@@ -58,6 +59,26 @@ public class GroovyScriptEvaluatorTests {
assertEquals(6, result);
}
@Test
public void testGroovyScriptWithCompilerConfiguration() {
GroovyScriptEvaluator evaluator = new GroovyScriptEvaluator();
MyBytecodeProcessor processor = new MyBytecodeProcessor();
evaluator.getCompilerConfiguration().setBytecodePostprocessor(processor);
Object result = evaluator.evaluate(new StaticScriptSource("return 3 * 2"));
assertEquals(6, result);
assertTrue(processor.processed.contains("Script1"));
}
@Test
public void testGroovyScriptWithImportCustomizer() {
GroovyScriptEvaluator evaluator = new GroovyScriptEvaluator();
ImportCustomizer importCustomizer = new ImportCustomizer();
importCustomizer.addStarImports("org.springframework.util");
evaluator.setCompilationCustomizers(importCustomizer);
Object result = evaluator.evaluate(new StaticScriptSource("return ResourceUtils.CLASSPATH_URL_PREFIX"));
assertEquals("classpath:", result);
}
@Test
public void testGroovyScriptFromStringUsingJsr223() {
StandardScriptEvaluator evaluator = new StandardScriptEvaluator();

View File

@@ -477,6 +477,8 @@ public class GroovyScriptFactoryTests {
ApplicationContext ctx = new ClassPathXmlApplicationContext("groovy-with-xsd.xml", getClass());
Map<?, Messenger> beans = ctx.getBeansOfType(Messenger.class);
assertEquals(4, beans.size());
assertTrue(ctx.getBean(MyBytecodeProcessor.class).processed.contains(
"org.springframework.scripting.groovy.GroovyMessenger2"));
}
@Test

View File

@@ -0,0 +1,37 @@
/*
* Copyright 2002-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.scripting.groovy;
import java.util.HashSet;
import java.util.Set;
import org.codehaus.groovy.control.BytecodeProcessor;
/**
* @author Juergen Hoeller
*/
public class MyBytecodeProcessor implements BytecodeProcessor {
public final Set<String> processed = new HashSet<String>();
@Override
public byte[] processBytecode(String name, byte[] original) {
this.processed.add(name);
return original;
}
}

View File

@@ -0,0 +1,30 @@
/*
* Copyright 2002-2016 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.scripting.groovy;
import org.codehaus.groovy.control.customizers.ImportCustomizer;
/**
* @author Juergen Hoeller
*/
public class MyImportCustomizer extends ImportCustomizer {
public MyImportCustomizer() {
addStarImports("org.springframework.scripting.groovy");
}
}