use of groovy.lang.GroovyShell in project spring-framework by spring-projects.
the class GroovyBeanDefinitionReader method loadBeanDefinitions.
/**
* Load bean definitions from the specified Groovy script or XML file.
* <p>Note that {@code ".xml"} files will be parsed as XML content; all other kinds
* of resources will be parsed as Groovy scripts.
* @param encodedResource the resource descriptor for the Groovy script or XML file,
* allowing specification of an encoding to use for parsing the file
* @return the number of bean definitions found
* @throws BeanDefinitionStoreException in case of loading or parsing errors
*/
public int loadBeanDefinitions(EncodedResource encodedResource) throws BeanDefinitionStoreException {
// Check for XML files and redirect them to the "standard" XmlBeanDefinitionReader
String filename = encodedResource.getResource().getFilename();
if (StringUtils.endsWithIgnoreCase(filename, ".xml")) {
return this.standardXmlBeanDefinitionReader.loadBeanDefinitions(encodedResource);
}
Closure beans = new Closure(this) {
public Object call(Object[] args) {
invokeBeanDefiningClosure((Closure) args[0]);
return null;
}
};
Binding binding = new Binding() {
@Override
public void setVariable(String name, Object value) {
if (currentBeanDefinition != null) {
applyPropertyToBeanDefinition(name, value);
} else {
super.setVariable(name, value);
}
}
};
binding.setVariable("beans", beans);
int countBefore = getRegistry().getBeanDefinitionCount();
try {
GroovyShell shell = new GroovyShell(getResourceLoader().getClassLoader(), binding);
shell.evaluate(encodedResource.getReader(), "beans");
} catch (Throwable ex) {
throw new BeanDefinitionParsingException(new Problem("Error evaluating Groovy script: " + ex.getMessage(), new Location(encodedResource.getResource()), null, ex));
}
return getRegistry().getBeanDefinitionCount() - countBefore;
}
use of groovy.lang.GroovyShell in project grails-core by grails.
the class DefaultUrlMappingEvaluatorTests method testRedirectMappings.
public void testRedirectMappings() throws Exception {
GroovyShell shell = new GroovyShell();
Binding binding = new Binding();
Script script = shell.parse("mappings = {\n" + "\"/first\"(redirect:[controller: 'foo', action: 'bar'])\n" + "\"/second\"(redirect: '/bing/bang')\n" + "}");
script.setBinding(binding);
script.run();
Closure closure = (Closure) binding.getVariable("mappings");
List<UrlMapping> mappings = evaluator.evaluateMappings(closure);
assertEquals(2, mappings.size());
Object redirectInfo = mappings.get(0).getRedirectInfo();
assertTrue(redirectInfo instanceof Map);
Map redirectMap = (Map) redirectInfo;
assertEquals(2, redirectMap.size());
assertEquals("foo", redirectMap.get("controller"));
assertEquals("bar", redirectMap.get("action"));
assertEquals("/bing/bang", mappings.get(1).getRedirectInfo());
}
use of groovy.lang.GroovyShell in project groovy-core by groovy.
the class ClassicGroovyTestGeneratorHelper method evaluate.
/** evaluate the source text against the classic AST with the JSR parser implementation*/
public Object evaluate(String theSrcText, String testName) throws Exception {
// fail early with a direct message if possible')
parse(theSrcText, testName);
GroovyShell groovy = new GroovyShell(new CompilerConfiguration());
return groovy.run(theSrcText, "main", new ArrayList());
}
use of groovy.lang.GroovyShell in project groovy-core by groovy.
the class Groovy1567_Bug method testGroovyScriptEngineVsGroovyShell.
public void testGroovyScriptEngineVsGroovyShell() throws IOException, ResourceException, ScriptException {
// @todo refactor this path
File currentDir = new File("./src/test/groovy/bugs");
String file = "bug1567_script.groovy";
Binding binding = new Binding();
GroovyShell shell = new GroovyShell(binding);
String[] test = null;
Object result = shell.run(new File(currentDir, file), test);
String[] roots = new String[] { currentDir.getAbsolutePath() };
GroovyScriptEngine gse = new GroovyScriptEngine(roots);
binding = new Binding();
// a MME was ensued here stating no 't.start()' was available
// in the script
gse.run(file, binding);
}
use of groovy.lang.GroovyShell in project groovy-core by groovy.
the class Groovy2553Bug method testMe.
public void testMe() {
new GroovyShell().evaluate("groovy.bugs.Autobox.Util.printByte(\"1\", Byte.valueOf((byte)1));");
new GroovyShell().evaluate("groovy.bugs.Autobox.Util.printByte(\"1\", (byte)1);");
}
Aggregations