Search in sources :

Example 41 with Closure

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

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 42 with Closure

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

the class ResourceGroovyMethods method traverse.

/**
     * Invokes the closure specified with key 'visit' in the options Map
     * for each descendant file in this directory tree. Convenience method
     * for {@link #traverse(java.io.File, java.util.Map, groovy.lang.Closure)} allowing the 'visit' closure
     * to be included in the options Map rather than as a parameter.
     *
     * @param self    a File
     * @param options a Map of options to alter the traversal behavior
     * @throws FileNotFoundException    if the given directory does not exist
     * @throws IllegalArgumentException if the provided File object does not represent a directory or illegal filter combinations are supplied
     * @see #traverse(java.io.File, java.util.Map, groovy.lang.Closure)
     * @since 1.7.1
     */
public static void traverse(final File self, final Map<String, Object> options) throws FileNotFoundException, IllegalArgumentException {
    final Closure visit = (Closure) options.remove("visit");
    traverse(self, options, visit);
}
Also used : Closure(groovy.lang.Closure)

Example 43 with Closure

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

the class InvokeGroovyMethodTest method testInvokeMethodNoParams.

// Method invocation tests
//-------------------------------------------------------------------------
public void testInvokeMethodNoParams() throws Throwable {
    buffer = new StringBuffer();
    List list = new ArrayList();
    list.add("abc");
    list.add("def");
    InvokerHelper.invokeMethod(list, "each", new Closure(this) {

        protected Object doCall(Object arguments) {
            buffer.append(arguments.toString());
            return null;
        }
    });
    assertEquals("buffer", "abcdef", buffer.toString());
}
Also used : Closure(groovy.lang.Closure) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList)

Example 44 with Closure

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

the class MethodFailureTest method testMethodWhichCallsTheFailingMethodInsideAClosure.

public void testMethodWhichCallsTheFailingMethodInsideAClosure() {
    MockGroovyObject object = new MockGroovyObject();
    try {
        object.invokeMethod("callClosure", new Closure(this) {

            protected Object doCall(GroovyObject object) {
                return object.invokeMethod("nonExistentMethod", "hello");
            }
        });
        fail("Should have thrown an exception");
    } catch (GroovyRuntimeException e) {
    // expected
    }
}
Also used : Closure(groovy.lang.Closure) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) GroovyObject(groovy.lang.GroovyObject) GroovyObject(groovy.lang.GroovyObject)

Example 45 with Closure

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

the class CachedSAMClass method coerceToSAM.

@SuppressWarnings("unchecked")
public static Object coerceToSAM(Closure argument, Method method, Class clazz, boolean isInterface) {
    if (argument != null && clazz.isAssignableFrom(argument.getClass())) {
        return argument;
    }
    if (isInterface) {
        if (Traits.isTrait(clazz)) {
            Map<String, Closure> impl = Collections.singletonMap(method.getName(), argument);
            return ProxyGenerator.INSTANCE.instantiateAggregate(impl, Collections.singletonList(clazz));
        }
        return Proxy.newProxyInstance(clazz.getClassLoader(), new Class[] { clazz }, new ConvertedClosure(argument));
    } else {
        Map<String, Object> m = new HashMap<String, Object>();
        m.put(method.getName(), argument);
        return ProxyGenerator.INSTANCE.instantiateAggregateFromBaseClass(m, clazz);
    }
}
Also used : ConvertedClosure(org.codehaus.groovy.runtime.ConvertedClosure) Closure(groovy.lang.Closure) ConvertedClosure(org.codehaus.groovy.runtime.ConvertedClosure) HashMap(java.util.HashMap)

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