Search in sources :

Example 21 with GroovyShell

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;
}
Also used : Binding(groovy.lang.Binding) BeanDefinitionParsingException(org.springframework.beans.factory.parsing.BeanDefinitionParsingException) Closure(groovy.lang.Closure) GroovyObject(groovy.lang.GroovyObject) Problem(org.springframework.beans.factory.parsing.Problem) GString(groovy.lang.GString) GroovyShell(groovy.lang.GroovyShell) Location(org.springframework.beans.factory.parsing.Location)

Example 22 with GroovyShell

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());
}
Also used : Binding(groovy.lang.Binding) UrlMapping(grails.web.mapping.UrlMapping) Script(groovy.lang.Script) Closure(groovy.lang.Closure) Map(java.util.Map) GroovyShell(groovy.lang.GroovyShell)

Example 23 with GroovyShell

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());
}
Also used : CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration) ArrayList(java.util.ArrayList) GroovyShell(groovy.lang.GroovyShell)

Example 24 with GroovyShell

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);
}
Also used : Binding(groovy.lang.Binding) GroovyScriptEngine(groovy.util.GroovyScriptEngine) File(java.io.File) GroovyShell(groovy.lang.GroovyShell)

Example 25 with GroovyShell

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);");
}
Also used : GroovyShell(groovy.lang.GroovyShell)

Aggregations

GroovyShell (groovy.lang.GroovyShell)86 Binding (groovy.lang.Binding)43 File (java.io.File)15 CompilerConfiguration (org.codehaus.groovy.control.CompilerConfiguration)14 Script (groovy.lang.Script)10 ImportCustomizer (org.codehaus.groovy.control.customizers.ImportCustomizer)10 Closure (groovy.lang.Closure)9 IOException (java.io.IOException)8 List (java.util.List)6 GroovyClassLoader (groovy.lang.GroovyClassLoader)5 GroovyObject (groovy.lang.GroovyObject)4 GroovyRuntimeException (groovy.lang.GroovyRuntimeException)4 AntBuilder (groovy.util.AntBuilder)4 InputStreamReader (java.io.InputStreamReader)4 StringWriter (java.io.StringWriter)4 BuildException (org.apache.tools.ant.BuildException)4 CompilationFailedException (org.codehaus.groovy.control.CompilationFailedException)4 UrlMapping (grails.web.mapping.UrlMapping)3 InputStream (java.io.InputStream)3 PrintWriter (java.io.PrintWriter)3