Search in sources :

Example 21 with MissingMethodException

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

the class Groovy method parseAndRunScript.

private void parseAndRunScript(GroovyShell shell, String txt, Object mavenPom, String scriptName, File scriptFile, AntBuilder builder) {
    try {
        final Script script;
        if (scriptFile != null) {
            script = shell.parse(scriptFile);
        } else {
            script = shell.parse(txt, scriptName);
        }
        final Project project = getProject();
        script.setProperty("ant", builder);
        script.setProperty("project", project);
        script.setProperty("properties", new AntProjectPropertiesDelegate(project));
        script.setProperty("target", getOwningTarget());
        script.setProperty("task", this);
        script.setProperty("args", cmdline.getCommandline());
        if (mavenPom != null) {
            script.setProperty("pom", mavenPom);
        }
        script.run();
    } catch (final MissingMethodException mme) {
        // not a script, try running through run method but properties will not be available
        if (scriptFile != null) {
            try {
                shell.run(scriptFile, cmdline.getCommandline());
            } catch (IOException e) {
                processError(e);
            }
        } else {
            shell.run(txt, scriptName, cmdline.getCommandline());
        }
    } catch (final CompilationFailedException e) {
        processError(e);
    } catch (IOException e) {
        processError(e);
    }
}
Also used : Script(groovy.lang.Script) Project(org.apache.tools.ant.Project) MissingMethodException(groovy.lang.MissingMethodException) CompilationFailedException(org.codehaus.groovy.control.CompilationFailedException) IOException(java.io.IOException)

Example 22 with MissingMethodException

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

the class GroovyScriptEngineImpl method eval.

// package-privates
Object eval(Class scriptClass, final ScriptContext ctx) throws ScriptException {
    // Only initialize once.
    if (null == ctx.getAttribute("context", ScriptContext.ENGINE_SCOPE)) {
        // add context to bindings
        ctx.setAttribute("context", ctx, ScriptContext.ENGINE_SCOPE);
        // direct output to ctx.getWriter
        // If we're wrapping with a PrintWriter here,
        // enable autoFlush because otherwise it might not get done!
        final Writer writer = ctx.getWriter();
        ctx.setAttribute("out", (writer instanceof PrintWriter) ? writer : new PrintWriter(writer, true), ScriptContext.ENGINE_SCOPE);
    // Not going to do this after all (at least for now).
    // Scripts can use context.{reader, writer, errorWriter}.
    // That is a modern version of System.{in, out, err} or Console.{reader, writer}().
    // 
    // // New I/O names consistent with ScriptContext and java.io.Console.
    // 
    // ctx.setAttribute("writer", writer, ScriptContext.ENGINE_SCOPE);
    // 
    // // Direct errors to ctx.getErrorWriter
    // final Writer errorWriter = ctx.getErrorWriter();
    // ctx.setAttribute("errorWriter", (errorWriter instanceof PrintWriter) ?
    // errorWriter :
    // new PrintWriter(errorWriter),
    // ScriptContext.ENGINE_SCOPE);
    // 
    // // Get input from ctx.getReader
    // // We don't wrap with BufferedReader here because we expect that if
    // // the host wants that they do it.  Either way Groovy scripts will
    // // always have readLine because the GDK supplies it for Reader.
    // ctx.setAttribute("reader", ctx.getReader(), ScriptContext.ENGINE_SCOPE);
    }
    // Fix for GROOVY-3669: Can't use several times the same JSR-223 ScriptContext for differents groovy script
    if (ctx.getWriter() != null) {
        ctx.setAttribute("out", new PrintWriter(ctx.getWriter(), true), ScriptContext.ENGINE_SCOPE);
    }
    /*
         * We use the following Binding instance so that global variable lookup
         * will be done in the current ScriptContext instance.
         */
    Binding binding = new Binding(ctx.getBindings(ScriptContext.ENGINE_SCOPE)) {

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

        @Override
        public void setVariable(String name, Object value) {
            synchronized (ctx) {
                int scope = ctx.getAttributesScope(name);
                if (scope == -1) {
                    scope = ScriptContext.ENGINE_SCOPE;
                }
                ctx.setAttribute(name, value, scope);
            }
        }
    };
    try {
        // then simply return that class
        if (!Script.class.isAssignableFrom(scriptClass)) {
            return scriptClass;
        } else {
            // it's a script
            Script scriptObject = InvokerHelper.createScript(scriptClass, binding);
            // save all current closures into global closures map
            Method[] methods = scriptClass.getMethods();
            for (Method m : methods) {
                String name = m.getName();
                globalClosures.put(name, new MethodClosure(scriptObject, name));
            }
            MetaClass oldMetaClass = scriptObject.getMetaClass();
            /*
                * We override the MetaClass of this script object so that we can
                * forward calls to global closures (of previous or future "eval" calls)
                * This gives the illusion of working on the same "global" scope.
                */
            scriptObject.setMetaClass(new DelegatingMetaClass(oldMetaClass) {

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

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

                @Override
                public Object invokeStaticMethod(Object object, String name, Object[] args) {
                    try {
                        return super.invokeStaticMethod(object, name, args);
                    } catch (MissingMethodException mme) {
                        return callGlobal(name, args, ctx);
                    }
                }
            });
            return scriptObject.run();
        }
    } catch (Exception e) {
        throw new ScriptException(e);
    } finally {
        // Fix for GROOVY-3669: Can't use several times the same JSR-223 ScriptContext for different groovy script
        // Groovy's scripting engine implementation adds those two variables in the binding
        // but should clean up afterwards
        ctx.removeAttribute("context", ScriptContext.ENGINE_SCOPE);
        ctx.removeAttribute("out", ScriptContext.ENGINE_SCOPE);
    }
}
Also used : Binding(groovy.lang.Binding) Script(groovy.lang.Script) CompiledScript(javax.script.CompiledScript) MissingPropertyException(groovy.lang.MissingPropertyException) String(java.lang.String) 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) PrintWriter(java.io.PrintWriter) Writer(java.io.Writer) Tuple(groovy.lang.Tuple) PrintWriter(java.io.PrintWriter)

Example 23 with MissingMethodException

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

the class AttributesProxy method findValues.

public List<? extends Convertible> findValues(Closure<Boolean> closure) {
    try {
        final NodeAttributeTableModel attributeTableModel = getNodeAttributeTableModel();
        if (attributeTableModel == null) {
            return Collections.emptyList();
        }
        final ArrayList<Convertible> result = new ArrayList<Convertible>(attributeTableModel.getRowCount());
        for (final Attribute a : attributeTableModel.getAttributes()) {
            final Object bool = closure.call(new Object[] { a.getName(), a.getValue() });
            if (result == null) {
                throw new RuntimeException("findValues(): closure returned null instead of boolean/Boolean");
            }
            if ((Boolean) bool)
                result.add(ProxyUtils.attributeValueToConvertible(getDelegate(), getScriptContext(), a.getValue()));
        }
        return result;
    } catch (final MissingMethodException e) {
        throw new RuntimeException("findValues(): closure needs to accept two args and must return boolean/Boolean" + " e.g. findValues{k,v -> k != 'TOTAL'}", e);
    } catch (final ClassCastException e) {
        throw new RuntimeException("findValues(): closure returned " + e.getMessage() + " instead of boolean/Boolean");
    }
}
Also used : MissingMethodException(groovy.lang.MissingMethodException) Attribute(org.freeplane.features.attribute.Attribute) NodeAttributeTableModel(org.freeplane.features.attribute.NodeAttributeTableModel) ArrayList(java.util.ArrayList) IFormattedObject(org.freeplane.features.format.IFormattedObject)

Aggregations

MissingMethodException (groovy.lang.MissingMethodException)23 Closure (groovy.lang.Closure)6 GroovyObject (groovy.lang.GroovyObject)6 Script (groovy.lang.Script)6 IOException (java.io.IOException)6 CompilationFailedException (org.codehaus.groovy.control.CompilationFailedException)6 MetaClass (groovy.lang.MetaClass)5 List (java.util.List)5 MissingPropertyException (groovy.lang.MissingPropertyException)4 ArrayList (java.util.ArrayList)4 Map (java.util.Map)4 MethodClosure (org.codehaus.groovy.runtime.MethodClosure)4 Binding (groovy.lang.Binding)3 DelegatingMetaClass (groovy.lang.DelegatingMetaClass)3 Tuple (groovy.lang.Tuple)3 PrintWriter (java.io.PrintWriter)3 Method (java.lang.reflect.Method)3 CompiledScript (javax.script.CompiledScript)3 ScriptException (javax.script.ScriptException)3 GroovyInterceptable (groovy.lang.GroovyInterceptable)2