Search in sources :

Example 51 with GroovyShell

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

the class GroovyMain method processSockets.

/**
     * Process Sockets.
     */
private void processSockets() throws CompilationFailedException, IOException, URISyntaxException {
    GroovyShell groovy = new GroovyShell(conf);
    new GroovySocketServer(groovy, getScriptSource(isScriptFile, script), autoOutput, port);
}
Also used : GroovyShell(groovy.lang.GroovyShell)

Example 52 with GroovyShell

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

the class SeansBug method testBug.

public void testBug() throws Exception {
    String code = "for (i in 1..10) \n{\n  println(i)\n}";
    GroovyShell shell = new GroovyShell();
    shell.evaluate(code);
}
Also used : GroovyShell(groovy.lang.GroovyShell)

Example 53 with GroovyShell

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

the class InspectorTest method testClassPropsGroovy.

public void testClassPropsGroovy() {
    Object testObject = new GroovyShell().evaluate("class Test {def meth1(a,b){}}\nreturn new Test()");
    Inspector insp = new Inspector(testObject);
    String[] classProps = insp.getClassProps();
    assertEquals("package n/a", classProps[Inspector.CLASS_PACKAGE_IDX]);
    assertEquals("public class Test", classProps[Inspector.CLASS_CLASS_IDX]);
    assertEquals("implements GroovyObject ", classProps[Inspector.CLASS_INTERFACE_IDX]);
    assertEquals("extends Object", classProps[Inspector.CLASS_SUPERCLASS_IDX]);
    assertEquals("is Primitive: false, is Array: false, is Groovy: true", classProps[Inspector.CLASS_OTHER_IDX]);
}
Also used : GroovyShell(groovy.lang.GroovyShell)

Example 54 with GroovyShell

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

the class Eval method xyz.

/**
     * Evaluates the specified String expression and makes the first three parameters available inside
     * the script bound to variables named 'x', 'y', and 'z' respectively, returning the result. For 
     * example, this code executes without failure: 
     * <pre>
     * assert 10 == Eval.xyz(2, 4, 2, ' x * y + z')
     * </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 xyz(final Object x, final Object y, final Object z, final String expression) throws CompilationFailedException {
    Binding b = new Binding();
    b.setVariable("x", x);
    b.setVariable("y", y);
    b.setVariable("z", z);
    GroovyShell sh = new GroovyShell(b);
    return sh.evaluate(expression);
}
Also used : Binding(groovy.lang.Binding) GroovyShell(groovy.lang.GroovyShell)

Example 55 with GroovyShell

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

the class Eval method me.

/**
     * Evaluates the specified String expression and makes the parameter available inside
     * the script, returning the result. For example, this code binds the 'x' variable: 
     * <pre>
     * assert 10 == Eval.me('x', 2, ' x * 4 + 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 me(final String symbol, final Object object, final String expression) throws CompilationFailedException {
    Binding b = new Binding();
    b.setVariable(symbol, object);
    GroovyShell sh = new GroovyShell(b);
    return sh.evaluate(expression);
}
Also used : Binding(groovy.lang.Binding) 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