Search in sources :

Example 31 with Script

use of groovy.lang.Script in project ratpack by ratpack.

the class RatpackDslScriptCapture method apply.

public RatpackDslClosures apply(Path file, String scriptContent) throws Exception {
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    ScriptEngine<Script> scriptEngine = new ScriptEngine<>(classLoader, compileStatic, Script.class);
    return RatpackDslClosures.capture(function, file, () -> {
        Script script = scriptEngine.create(file.getFileName().toString(), file, scriptContent);
        script.setBinding(new Binding(args));
        script.run();
    });
}
Also used : Binding(groovy.lang.Binding) Script(groovy.lang.Script) ScriptEngine(ratpack.groovy.script.internal.ScriptEngine)

Example 32 with Script

use of groovy.lang.Script in project gremlin by tinkerpop.

the class GremlinGroovyScriptEngine method eval.

Object eval(final Class scriptClass, final ScriptContext context) throws ScriptException {
    this.checkClearCache();
    context.setAttribute("context", context, ScriptContext.ENGINE_SCOPE);
    java.io.Writer writer = context.getWriter();
    context.setAttribute("out", writer instanceof PrintWriter ? writer : new PrintWriter(writer), ScriptContext.ENGINE_SCOPE);
    Binding binding = new Binding() {

        public Object getVariable(String name) {
            synchronized (context) {
                int scope = context.getAttributesScope(name);
                if (scope != -1) {
                    return context.getAttribute(name, scope);
                }
                throw new MissingPropertyException(name, getClass());
            }
        }

        public void setVariable(String name, Object value) {
            synchronized (context) {
                int scope = context.getAttributesScope(name);
                if (scope == -1) {
                    scope = ScriptContext.ENGINE_SCOPE;
                }
                context.setAttribute(name, value, scope);
            }
        }
    };
    try {
        Script scriptObject = InvokerHelper.createScript(scriptClass, binding);
        Method[] methods = scriptClass.getMethods();
        Map<String, MethodClosure> closures = new HashMap<String, MethodClosure>();
        for (Method m : methods) {
            String name = m.getName();
            closures.put(name, new MethodClosure(scriptObject, name));
        }
        globalClosures.putAll(closures);
        final MetaClass oldMetaClass = scriptObject.getMetaClass();
        scriptObject.setMetaClass(new DelegatingMetaClass(oldMetaClass) {

            public Object invokeMethod(Object object, String name, Object args) {
                if (args == null) {
                    return invokeMethod(object, name, MetaClassHelper.EMPTY_ARRAY);
                } else if (args instanceof Tuple) {
                    return invokeMethod(object, name, ((Tuple) args).toArray());
                } else if (args instanceof Object[]) {
                    return invokeMethod(object, name, (Object[]) args);
                } else {
                    return invokeMethod(object, name, new Object[] { args });
                }
            }

            public Object invokeMethod(Object object, String name, Object[] args) {
                try {
                    return super.invokeMethod(object, name, args);
                } catch (MissingMethodException mme) {
                    return callGlobal(name, args, context);
                }
            }

            public Object invokeStaticMethod(Object object, String name, Object[] args) {
                try {
                    return super.invokeStaticMethod(object, name, args);
                } catch (MissingMethodException mme) {
                    return callGlobal(name, args, context);
                }
            }
        });
        return scriptObject.run();
    } catch (Exception e) {
        throw new ScriptException(e);
    }
}
Also used : Binding(groovy.lang.Binding) Script(groovy.lang.Script) CompiledScript(javax.script.CompiledScript) GroovyCompiledScript(org.codehaus.groovy.jsr223.GroovyCompiledScript) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) MissingPropertyException(groovy.lang.MissingPropertyException) Method(java.lang.reflect.Method) MethodClosure(org.codehaus.groovy.runtime.MethodClosure) MissingPropertyException(groovy.lang.MissingPropertyException) ScriptException(javax.script.ScriptException) MissingMethodException(groovy.lang.MissingMethodException) IOException(java.io.IOException) CompilationFailedException(org.codehaus.groovy.control.CompilationFailedException) SyntaxException(org.codehaus.groovy.syntax.SyntaxException) ScriptException(javax.script.ScriptException) MissingMethodException(groovy.lang.MissingMethodException) MetaClass(groovy.lang.MetaClass) DelegatingMetaClass(groovy.lang.DelegatingMetaClass) DelegatingMetaClass(groovy.lang.DelegatingMetaClass) Tuple(groovy.lang.Tuple) PrintWriter(java.io.PrintWriter)

Example 33 with Script

use of groovy.lang.Script in project groovy by apache.

the class InvokerHelperTest method testCreateScriptWithNullClass.

public void testCreateScriptWithNullClass() {
    Script script = InvokerHelper.createScript(null, new Binding(bindingVariables));
    assertEquals(bindingVariables, script.getBinding().getVariables());
}
Also used : Binding(groovy.lang.Binding) Script(groovy.lang.Script)

Example 34 with Script

use of groovy.lang.Script in project groovy by apache.

the class GroovyTestSuite method loadTestSuite.

public void loadTestSuite() throws Exception {
    String fileName = System.getProperty("test", file);
    if (fileName == null) {
        throw new RuntimeException("No filename given in the 'test' system property so cannot run a Groovy unit test");
    }
    System.out.println("Compiling: " + fileName);
    Class type = compile(fileName);
    String[] args = {};
    if (!Test.class.isAssignableFrom(type) && Script.class.isAssignableFrom(type)) {
        // let's treat the script as a Test
        addTest(new ScriptTestAdapter(type, args));
    } else {
        addTestSuite(type);
    }
}
Also used : Script(groovy.lang.Script) ScriptTestAdapter(org.codehaus.groovy.runtime.ScriptTestAdapter)

Example 35 with Script

use of groovy.lang.Script in project intellij-community by JetBrains.

the class GroovyScriptMacro method runIt.

private static Object runIt(Expression[] params, ExpressionContext context) {
    try {
        Result result = params[0].calculateResult(context);
        if (result == null)
            return result;
        String text = result.toString();
        GroovyShell shell = new GroovyShell();
        File possibleFile = new File(text);
        Script script = possibleFile.exists() ? shell.parse(possibleFile) : shell.parse(text);
        Binding binding = new Binding();
        for (int i = 1; i < params.length; ++i) {
            Result paramResult = params[i].calculateResult(context);
            Object value = null;
            if (paramResult instanceof ListResult) {
                value = ContainerUtil.map2List(((ListResult) paramResult).getComponents(), result1 -> result1.toString());
            } else if (paramResult != null) {
                value = paramResult.toString();
            }
            binding.setVariable("_" + i, value);
        }
        binding.setVariable("_editor", context.getEditor());
        script.setBinding(binding);
        Object o = script.run();
        return o != null ? StringUtil.convertLineSeparators(o.toString()) : null;
    } catch (Exception e) {
        return new TextResult(StringUtil.convertLineSeparators(e.getLocalizedMessage()));
    }
}
Also used : Binding(groovy.lang.Binding) LookupElementBuilder(com.intellij.codeInsight.lookup.LookupElementBuilder) LookupElement(com.intellij.codeInsight.lookup.LookupElement) StringUtil(com.intellij.openapi.util.text.StringUtil) com.intellij.codeInsight.template(com.intellij.codeInsight.template) Set(java.util.Set) ContainerUtil(com.intellij.util.containers.ContainerUtil) Script(groovy.lang.Script) GroovyShell(groovy.lang.GroovyShell) File(java.io.File) CodeInsightBundle(com.intellij.codeInsight.CodeInsightBundle) Function(com.intellij.util.Function) Binding(groovy.lang.Binding) NotNull(org.jetbrains.annotations.NotNull) LinkedHashSet(java.util.LinkedHashSet) Script(groovy.lang.Script) File(java.io.File) GroovyShell(groovy.lang.GroovyShell)

Aggregations

Script (groovy.lang.Script)42 Binding (groovy.lang.Binding)21 IOException (java.io.IOException)12 GroovyShell (groovy.lang.GroovyShell)10 CompilationFailedException (org.codehaus.groovy.control.CompilationFailedException)9 MissingMethodException (groovy.lang.MissingMethodException)6 GroovyRuntimeException (groovy.lang.GroovyRuntimeException)5 PrintWriter (java.io.PrintWriter)5 Closure (groovy.lang.Closure)4 GroovyClassLoader (groovy.lang.GroovyClassLoader)4 MetaClass (groovy.lang.MetaClass)4 MissingPropertyException (groovy.lang.MissingPropertyException)4 File (java.io.File)4 UrlMapping (grails.web.mapping.UrlMapping)3 DelegatingMetaClass (groovy.lang.DelegatingMetaClass)3 Tuple (groovy.lang.Tuple)3 Method (java.lang.reflect.Method)3 List (java.util.List)3 CompiledScript (javax.script.CompiledScript)3 ScriptException (javax.script.ScriptException)3