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);
}
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;
}
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));
}
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));
}
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());
}
Aggregations