Search in sources :

Example 11 with Closure

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

the class GroovyTypeCheckingExtensionSupport method handleIncompatibleReturnType.

@Override
public boolean handleIncompatibleReturnType(final ReturnStatement returnStatement, ClassNode inferredReturnType) {
    setHandled(false);
    List<Closure> list = eventHandlers.get("handleIncompatibleReturnType");
    if (list != null) {
        for (Closure closure : list) {
            safeCall(closure, returnStatement, inferredReturnType);
        }
    }
    return handled;
}
Also used : Closure(groovy.lang.Closure)

Example 12 with Closure

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

the class GroovyTypeCheckingExtensionSupport method handleAmbiguousMethods.

@Override
@SuppressWarnings("unchecked")
public List<MethodNode> handleAmbiguousMethods(final List<MethodNode> nodes, final Expression origin) {
    List<Closure> onMethodSelection = eventHandlers.get("handleAmbiguousMethods");
    List<MethodNode> methodList = nodes;
    if (onMethodSelection != null) {
        Iterator<Closure> iterator = onMethodSelection.iterator();
        while (methodList.size() > 1 && iterator.hasNext()) {
            final Closure closure = iterator.next();
            Object result = safeCall(closure, methodList, origin);
            if (result != null) {
                if (result instanceof MethodNode) {
                    methodList = Collections.singletonList((MethodNode) result);
                } else if (result instanceof Collection) {
                    methodList = new LinkedList<MethodNode>((Collection<? extends MethodNode>) result);
                } else {
                    throw new GroovyBugError("Type checking extension returned unexpected method list: " + result);
                }
            }
        }
    }
    return methodList;
}
Also used : Closure(groovy.lang.Closure) MethodNode(org.codehaus.groovy.ast.MethodNode) GroovyBugError(org.codehaus.groovy.GroovyBugError) Collection(java.util.Collection) LinkedList(java.util.LinkedList)

Example 13 with Closure

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

the class GroovyTypeCheckingExtensionSupport method beforeMethodCall.

@Override
public boolean beforeMethodCall(final MethodCall call) {
    setHandled(false);
    List<Closure> onMethodSelection = eventHandlers.get("beforeMethodCall");
    if (onMethodSelection != null) {
        for (Closure closure : onMethodSelection) {
            safeCall(closure, call);
        }
    }
    return handled;
}
Also used : Closure(groovy.lang.Closure)

Example 14 with Closure

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

the class BindPathSnooper method createBinding.

public FullBinding createBinding(SourceBinding source, TargetBinding target) {
    if (source != this) {
        throw new RuntimeException("Source binding must the Trigger Binding as well");
    }
    final BindPathSnooper delegate = new BindPathSnooper();
    try {
        // create our own local copy of the closure
        final Class closureClass = closure.getClass();
        // do in privileged block since we may be looking at private stuff
        Closure closureLocalCopy = java.security.AccessController.doPrivileged(new PrivilegedAction<Closure>() {

            public Closure run() {
                // assume closures have only 1 constructor, of the form (Object, Reference*)
                Constructor constructor = closureClass.getConstructors()[0];
                int paramCount = constructor.getParameterTypes().length;
                Object[] args = new Object[paramCount];
                args[0] = delegate;
                for (int i = 1; i < paramCount; i++) {
                    args[i] = new Reference(new BindPathSnooper());
                }
                try {
                    boolean acc = constructor.isAccessible();
                    constructor.setAccessible(true);
                    Closure localCopy = (Closure) constructor.newInstance(args);
                    if (!acc) {
                        constructor.setAccessible(false);
                    }
                    localCopy.setResolveStrategy(Closure.DELEGATE_ONLY);
                    for (Field f : closureClass.getDeclaredFields()) {
                        acc = f.isAccessible();
                        f.setAccessible(true);
                        if (f.getType() == Reference.class) {
                            delegate.fields.put(f.getName(), (BindPathSnooper) ((Reference) f.get(localCopy)).get());
                        }
                        if (!acc) {
                            f.setAccessible(false);
                        }
                    }
                    return localCopy;
                } catch (Exception e) {
                    throw new RuntimeException("Error snooping closure", e);
                }
            }
        });
        try {
            closureLocalCopy.call();
        } catch (DeadEndException e) {
            // we want this exception exposed.
            throw e;
        } catch (Exception e) {
        //LOGME
        // ignore it, likely failing because we are faking out properties
        // such as a call to Math.min(int, BindPathSnooper)
        }
    } catch (Exception e) {
        e.printStackTrace(System.out);
        throw new RuntimeException("A closure expression binding could not be created because of " + e.getClass().getName() + ":\n\t" + e.getMessage());
    }
    List<BindPath> rootPaths = new ArrayList<BindPath>();
    for (Map.Entry<String, BindPathSnooper> entry : delegate.fields.entrySet()) {
        BindPath bp = createBindPath(entry.getKey(), entry.getValue());
        bp.currentObject = closure;
        rootPaths.add(bp);
    }
    PropertyPathFullBinding fb = new PropertyPathFullBinding();
    fb.setSourceBinding(new ClosureSourceBinding(closure));
    fb.setTargetBinding(target);
    fb.bindPaths = rootPaths.toArray(new BindPath[rootPaths.size()]);
    return fb;
}
Also used : Closure(groovy.lang.Closure) Constructor(java.lang.reflect.Constructor) Reference(groovy.lang.Reference) ArrayList(java.util.ArrayList) Field(java.lang.reflect.Field) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 15 with Closure

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

the class GPathResult method getBody.

/**
     * Creates a Closure representing the body of this GPathResult.
     *
     * @return the body of this GPathResult, converted to a <code>Closure</code>
     */
public Closure getBody() {
    return new Closure(this.parent, this) {

        public void doCall(Object[] args) {
            final GroovyObject delegate = (GroovyObject) getDelegate();
            final GPathResult thisObject = (GPathResult) getThisObject();
            Node node = (Node) thisObject.getAt(0);
            List children = node.children();
            for (Object child : children) {
                delegate.getProperty("mkp");
                if (child instanceof Node) {
                    delegate.invokeMethod("yield", new Object[] { new NodeChild((Node) child, thisObject, "*", null) });
                } else {
                    delegate.invokeMethod("yield", new Object[] { child });
                }
            }
        }
    };
}
Also used : Closure(groovy.lang.Closure) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) GroovyObject(groovy.lang.GroovyObject) GroovyObject(groovy.lang.GroovyObject)

Aggregations

Closure (groovy.lang.Closure)211 Map (java.util.Map)40 GroovyObject (groovy.lang.GroovyObject)27 HashMap (java.util.HashMap)27 Binding (groovy.lang.Binding)21 ArrayList (java.util.ArrayList)21 List (java.util.List)19 GString (groovy.lang.GString)17 GroovyShell (groovy.lang.GroovyShell)13 MethodClosure (org.codehaus.groovy.runtime.MethodClosure)13 LinkedHashMap (java.util.LinkedHashMap)12 Test (org.junit.Test)12 File (java.io.File)10 LinkedList (java.util.LinkedList)10 FileType (groovy.io.FileType)8 FileVisitResult (groovy.io.FileVisitResult)8 MetaClass (groovy.lang.MetaClass)8 Collection (java.util.Collection)8 Script (groovy.lang.Script)7 IOException (java.io.IOException)7