Search in sources :

Example 31 with GroovyShell

use of groovy.lang.GroovyShell in project groovy-core by groovy.

the class Eval method xy.

/**
     * Evaluates the specified String expression and makes the first two parameters available inside
     * the script bound to variables named 'x' and 'y' respectively, returning the result. For example, 
     * this code executes without failure: 
     * <pre>
     * assert 10 == Eval.xy(2, 4, ' x * y + 2')
     * </pre>
     * @param expression the Groovy expression to evaluate
     * @return the result of the expression
     * @throws CompilationFailedException if expression is not valid Groovy
     */
public static Object xy(final Object x, final Object y, final String expression) throws CompilationFailedException {
    Binding b = new Binding();
    b.setVariable("x", x);
    b.setVariable("y", y);
    GroovyShell sh = new GroovyShell(b);
    return sh.evaluate(expression);
}
Also used : Binding(groovy.lang.Binding) GroovyShell(groovy.lang.GroovyShell)

Example 32 with GroovyShell

use of groovy.lang.GroovyShell in project groovy-core by groovy.

the class FileSystemCompiler method generateCompilerConfigurationFromOptions.

public static CompilerConfiguration generateCompilerConfigurationFromOptions(CommandLine cli) throws IOException {
    //
    // Setup the configuration data
    CompilerConfiguration configuration = new CompilerConfiguration();
    if (cli.hasOption("classpath")) {
        configuration.setClasspath(cli.getOptionValue("classpath"));
    }
    if (cli.hasOption('d')) {
        configuration.setTargetDirectory(cli.getOptionValue('d'));
    }
    if (cli.hasOption("encoding")) {
        configuration.setSourceEncoding(cli.getOptionValue("encoding"));
    }
    if (cli.hasOption("basescript")) {
        configuration.setScriptBaseClass(cli.getOptionValue("basescript"));
    }
    // joint compilation parameters
    if (cli.hasOption('j')) {
        Map<String, Object> compilerOptions = new HashMap<String, Object>();
        String[] opts = cli.getOptionValues("J");
        compilerOptions.put("namedValues", opts);
        opts = cli.getOptionValues("F");
        compilerOptions.put("flags", opts);
        configuration.setJointCompilationOptions(compilerOptions);
    }
    if (cli.hasOption("indy")) {
        configuration.getOptimizationOptions().put("int", false);
        configuration.getOptimizationOptions().put("indy", true);
    }
    if (cli.hasOption("configscript")) {
        String path = cli.getOptionValue("configscript");
        File groovyConfigurator = new File(path);
        Binding binding = new Binding();
        binding.setVariable("configuration", configuration);
        CompilerConfiguration configuratorConfig = new CompilerConfiguration();
        ImportCustomizer customizer = new ImportCustomizer();
        customizer.addStaticStars("org.codehaus.groovy.control.customizers.builder.CompilerCustomizationBuilder");
        configuratorConfig.addCompilationCustomizers(customizer);
        GroovyShell shell = new GroovyShell(binding, configuratorConfig);
        shell.evaluate(groovyConfigurator);
    }
    return configuration;
}
Also used : Binding(groovy.lang.Binding) CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration) ImportCustomizer(org.codehaus.groovy.control.customizers.ImportCustomizer) GroovyShell(groovy.lang.GroovyShell)

Example 33 with GroovyShell

use of groovy.lang.GroovyShell in project groovy-core by groovy.

the class UnimplementedSyntaxTest method test_StaticImport1.

// -----------------------
// feature: static imports
// -----------------------
// TODO: move somewhere else
public void test_StaticImport1() throws Exception {
    //GROOVY-3711: The following call now results in a valid script class node, so foo.Bar needs to get resolved.
    GroovyShell groovyShell = new GroovyShell();
    compile("package foo; class Bar{}", groovyShell);
    assertNotNull(compile("import static foo.Bar.mooky", groovyShell));
}
Also used : GroovyShell(groovy.lang.GroovyShell)

Example 34 with GroovyShell

use of groovy.lang.GroovyShell in project groovy-core by groovy.

the class UnimplementedSyntaxTest method test_StaticImport2.

public void test_StaticImport2() throws Exception {
    //GROOVY-3711: The following call now results in a valid script class node, so foo.Bar needs to get resolved.
    GroovyShell groovyShell = new GroovyShell();
    compile("package foo; class Bar{}", groovyShell);
    assertNotNull(compile("import static foo.Bar.*", groovyShell));
}
Also used : GroovyShell(groovy.lang.GroovyShell)

Example 35 with GroovyShell

use of groovy.lang.GroovyShell in project groovy-core by groovy.

the class GroovyAssert method assertScript.

/**
     * Asserts that the script runs without any exceptions
     *
     * @param script the script that should pass without any exception thrown
     */
public static void assertScript(final String script) throws Exception {
    GroovyShell shell = new GroovyShell();
    shell.evaluate(script, genericScriptName());
}
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