Search in sources :

Example 41 with Binding

use of groovy.lang.Binding in project LogHub by fbacchella.

the class TestGroovy method testGroovy2.

@SuppressWarnings("unchecked")
@Test
public void testGroovy2() throws InstantiationException, IllegalAccessException, NoSuchMethodException, SecurityException, IllegalArgumentException, InvocationTargetException, IOException {
    String script = "event.a.b * 2";
    try (GroovyClassLoader groovyClassLoader = new GroovyClassLoader()) {
        Class<Script> theParsedClass = groovyClassLoader.parseClass(script);
        Script groovyScript = theParsedClass.newInstance();
        Binding groovyBinding = new Binding();
        Event event = new EventInstance(ConnectionContext.EMPTY);
        event.put("a", Collections.singletonMap("b", 1));
        groovyBinding.setVariable("event", event);
        groovyScript.setBinding(groovyBinding);
        System.out.println(groovyScript.run().getClass());
    }
}
Also used : GroovyClassLoader(groovy.lang.GroovyClassLoader) Binding(groovy.lang.Binding) Script(groovy.lang.Script) Test(org.junit.Test)

Example 42 with Binding

use of groovy.lang.Binding in project LogHub by fbacchella.

the class TestGroovy method testGroovy1.

@Test
public void testGroovy1() {
    GroovyShell groovyShell = new GroovyShell(getClass().getClassLoader());
    Script groovyScript = groovyShell.parse("event.a * 2");
    Binding groovyBinding = new Binding();
    Event event = new EventInstance(ConnectionContext.EMPTY);
    event.put("a", 1);
    groovyBinding.setVariable("event", event);
    groovyScript.setBinding(groovyBinding);
    System.out.println(groovyScript.run().getClass());
}
Also used : Binding(groovy.lang.Binding) Script(groovy.lang.Script) GroovyShell(groovy.lang.GroovyShell) Test(org.junit.Test)

Example 43 with Binding

use of groovy.lang.Binding in project freeplane by freeplane.

the class GroovyScript method createBindingForCompilation.

private Binding createBindingForCompilation() {
    final Binding binding = new Binding();
    binding.setVariable("c", null);
    binding.setVariable("node", null);
    return binding;
}
Also used : Binding(groovy.lang.Binding)

Example 44 with Binding

use of groovy.lang.Binding in project freeplane by freeplane.

the class GroovyScript method compileAndCache.

private Script compileAndCache(final ScriptingSecurityManager scriptingSecurityManager) throws Throwable {
    if (!groovyPatched) {
        GroovyPatcher.apply(GroovyObject.class);
        groovyPatched = true;
    }
    if (compileTimeStrategy.canUseOldCompiledScript()) {
        scriptClassLoader.setSecurityManager(scriptingSecurityManager);
        return compiledScript;
    }
    removeOldScript();
    errorsInScript = null;
    if (script instanceof Script) {
        return (Script) script;
    } else {
        try {
            final Binding binding = createBindingForCompilation();
            scriptClassLoader = ScriptClassLoader.createClassLoader();
            scriptClassLoader.setSecurityManager(scriptingSecurityManager);
            final GroovyShell shell = new GroovyShell(scriptClassLoader, binding, createCompilerConfiguration());
            compileTimeStrategy.scriptCompileStart();
            if (script instanceof String) {
                compiledScript = shell.parse((String) script);
            } else if (script instanceof File) {
                compiledScript = shell.parse((File) script);
            } else {
                throw new IllegalArgumentException();
            }
            compileTimeStrategy.scriptCompiled();
            return compiledScript;
        } catch (Throwable e) {
            errorsInScript = e;
            throw e;
        }
    }
}
Also used : Binding(groovy.lang.Binding) Script(groovy.lang.Script) File(java.io.File)

Example 45 with Binding

use of groovy.lang.Binding in project beakerx by twosigma.

the class GroovyEvaluator method reloadClassloader.

private void reloadClassloader() {
    this.beakerxUrlClassLoader = newParentClassLoader(getClasspath());
    this.icz = new ImportCustomizer();
    this.groovyClassLoader = newEvaluator(getImports(), getClasspath(), getOutDir(), icz, beakerxUrlClassLoader);
    this.scriptBinding = new Binding();
}
Also used : Binding(groovy.lang.Binding) ImportCustomizer(org.codehaus.groovy.control.customizers.ImportCustomizer) GroovyClassLoaderFactory.addImportPathToImportCustomizer(com.twosigma.beakerx.groovy.evaluator.GroovyClassLoaderFactory.addImportPathToImportCustomizer)

Aggregations

Binding (groovy.lang.Binding)219 GroovyShell (groovy.lang.GroovyShell)77 Script (groovy.lang.Script)58 Test (org.junit.Test)39 IOException (java.io.IOException)30 File (java.io.File)24 HashMap (java.util.HashMap)24 Closure (groovy.lang.Closure)23 CompilerConfiguration (org.codehaus.groovy.control.CompilerConfiguration)23 Map (java.util.Map)21 GroovyService (eu.esdihumboldt.util.groovy.sandbox.GroovyService)13 CompilationFailedException (org.codehaus.groovy.control.CompilationFailedException)13 ImportCustomizer (org.codehaus.groovy.control.customizers.ImportCustomizer)12 MissingPropertyException (groovy.lang.MissingPropertyException)11 GroovyClassLoader (groovy.lang.GroovyClassLoader)10 InputStreamReader (java.io.InputStreamReader)10 StringWriter (java.io.StringWriter)10 Writer (java.io.Writer)10 InstanceBuilder (eu.esdihumboldt.hale.common.instance.groovy.InstanceBuilder)9 InvocationTargetException (java.lang.reflect.InvocationTargetException)9