Search in sources :

Example 56 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 57 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 58 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 59 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 60 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)213 GroovyShell (groovy.lang.GroovyShell)76 Script (groovy.lang.Script)55 Test (org.junit.Test)41 IOException (java.io.IOException)29 File (java.io.File)24 HashMap (java.util.HashMap)23 Closure (groovy.lang.Closure)22 CompilerConfiguration (org.codehaus.groovy.control.CompilerConfiguration)22 Map (java.util.Map)20 CompilationFailedException (org.codehaus.groovy.control.CompilationFailedException)13 ImportCustomizer (org.codehaus.groovy.control.customizers.ImportCustomizer)12 GroovyService (eu.esdihumboldt.util.groovy.sandbox.GroovyService)11 MissingPropertyException (groovy.lang.MissingPropertyException)11 GroovyClassLoader (groovy.lang.GroovyClassLoader)10 StringWriter (java.io.StringWriter)10 InstanceBuilder (eu.esdihumboldt.hale.common.instance.groovy.InstanceBuilder)9 InputStreamReader (java.io.InputStreamReader)9 Writer (java.io.Writer)9 InvocationTargetException (java.lang.reflect.InvocationTargetException)9