Search in sources :

Example 61 with Closure

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

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)

Example 62 with Closure

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

the class ObjectGraphBuilder method setNewInstanceResolver.

/**
 * Sets the current NewInstanceResolver.<br>
 * It will assign DefaultNewInstanceResolver if null.<br>
 * It accepts a NewInstanceResolver instance or a Closure.
 */
public void setNewInstanceResolver(final Object newInstanceResolver) {
    if (newInstanceResolver instanceof NewInstanceResolver) {
        this.newInstanceResolver = (NewInstanceResolver) newInstanceResolver;
    } else if (newInstanceResolver instanceof Closure) {
        final ObjectGraphBuilder self = this;
        this.newInstanceResolver = new NewInstanceResolver() {

            public Object newInstance(Class klass, Map attributes) throws InstantiationException, IllegalAccessException {
                Closure cls = (Closure) newInstanceResolver;
                cls.setDelegate(self);
                return cls.call(new Object[] { klass, attributes });
            }
        };
    } else {
        this.newInstanceResolver = new DefaultNewInstanceResolver();
    }
}
Also used : Closure(groovy.lang.Closure) HashMap(java.util.HashMap) Map(java.util.Map)

Example 63 with Closure

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

the class Expando method hashCode.

/**
 * This allows hashCode to be overridden by a closure <i>field</i> method attached
 * to the expando object.
 *
 * @see java.lang.Object#hashCode()
 */
public int hashCode() {
    Object method = getProperties().get("hashCode");
    if (method != null && method instanceof Closure) {
        // invoke overridden hashCode closure method
        Closure closure = (Closure) method;
        closure.setDelegate(this);
        Integer ret = (Integer) closure.call();
        return ret.intValue();
    } else {
        return super.hashCode();
    }
}
Also used : Closure(groovy.lang.Closure)

Example 64 with Closure

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

the class Expando method equals.

/**
 * This allows equals to be overridden by a closure <i>field</i> method attached
 * to the expando object.
 *
 * @see java.lang.Object#equals(java.lang.Object)
 */
public boolean equals(Object obj) {
    Object method = getProperties().get("equals");
    if (method != null && method instanceof Closure) {
        // invoke overridden equals closure method
        Closure closure = (Closure) method;
        closure.setDelegate(this);
        Boolean ret = (Boolean) closure.call(obj);
        return ret.booleanValue();
    } else {
        return super.equals(obj);
    }
}
Also used : Closure(groovy.lang.Closure)

Example 65 with Closure

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

the class ImportCustomizerFactory method onNodeChildren.

@Override
public boolean onNodeChildren(final FactoryBuilderSupport builder, final Object node, final Closure childContent) {
    if (node instanceof ImportCustomizer) {
        Closure clone = (Closure) childContent.clone();
        clone.setDelegate(new ImportHelper((ImportCustomizer) node));
        clone.call();
    }
    return false;
}
Also used : Closure(groovy.lang.Closure) ImportCustomizer(org.codehaus.groovy.control.customizers.ImportCustomizer)

Aggregations

Closure (groovy.lang.Closure)251 Map (java.util.Map)55 HashMap (java.util.HashMap)38 ArrayList (java.util.ArrayList)37 GroovyObject (groovy.lang.GroovyObject)33 List (java.util.List)33 Binding (groovy.lang.Binding)22 GString (groovy.lang.GString)21 LinkedHashMap (java.util.LinkedHashMap)20 LinkedList (java.util.LinkedList)19 Collection (java.util.Collection)17 GroovyShell (groovy.lang.GroovyShell)14 Test (org.junit.Test)14 MethodClosure (org.codehaus.groovy.runtime.MethodClosure)13 FileType (groovy.io.FileType)12 FileVisitResult (groovy.io.FileVisitResult)12 File (java.io.File)12 Iterator (java.util.Iterator)10 GroovyBugError (org.codehaus.groovy.GroovyBugError)10 IOException (java.io.IOException)9