Search in sources :

Example 86 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 87 with Closure

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

the class JName1Test method testMetaClassNameHandlingWithClosures1.

public void testMetaClassNameHandlingWithClosures1() {
    final Tt1cgi obj = new Tt1cgi() {
    };
    // repeat test with subclass
    final Closure newX = new Closure(null) {

        public Object doCall(final Object params) {
            return "new x";
        }
    };
    final Closure newX1 = new Closure(null) {

        public Object doCall(final Object params) {
            return "new x1";
        }
    };
    final Closure newX2 = new Closure(null) {

        public Object doCall(final Object params) {
            return "new x2";
        }
    };
    final Closure newX3 = new Closure(null) {

        public Object doCall(final Object params) {
            return "new x3";
        }
    };
    assertTrue(((Closure) obj.getProperty("x")).call() == obj.getX().call());
    assertTrue(((Closure) obj.getMetaClass().getAttribute(obj, "x")).call() == obj.x.call());
    assertTrue(obj.invokeMethod("x", new Object[] {}) == obj.x());
    obj.setProperty("x", newX);
    obj.getMetaClass().setAttribute(obj, "x", newX1);
    assertTrue(((Closure) obj.getProperty("x")).call() == newX.call());
    assertTrue(((Closure) obj.getMetaClass().getAttribute(obj, "x")).call() == newX1.call());
    obj.setX(newX2);
    obj.x = newX3;
    assertTrue(((Closure) obj.getProperty("x")).call() == newX2.call());
    assertTrue(((Closure) obj.getMetaClass().getAttribute(obj, "x")).call() == newX3.call());
}
Also used : Closure(groovy.lang.Closure) Tt1cgi(gls.ch06.s05.testClasses.Tt1cgi)

Example 88 with Closure

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

the class AnnotationClosureJavaCompatibilityTest method verify.

public void verify(Class closureClass) {
    try {
        Constructor ctor = closureClass.getConstructor(Object.class, Object.class);
        Closure closure = (Closure) ctor.newInstance(null, null);
        assertEquals(3, closure.call());
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : Closure(groovy.lang.Closure) Constructor(java.lang.reflect.Constructor)

Example 89 with Closure

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

the class DefaultTypeTransformation method continueCastOnSAM.

private static Object continueCastOnSAM(Object object, Class type) {
    if (object instanceof Closure) {
        Method m = CachedSAMClass.getSAMMethod(type);
        if (m != null) {
            return CachedSAMClass.coerceToSAM((Closure) object, m, type);
        }
    }
    Object[] args = null;
    if (object instanceof Collection) {
        // let's try invoke the constructor with the list as arguments
        // such as for creating a Dimension, Point, Color etc.
        Collection collection = (Collection) object;
        args = collection.toArray();
    } else if (object instanceof Object[]) {
        args = (Object[]) object;
    } else if (object instanceof Map) {
        // emulate named params constructor
        args = new Object[1];
        args[0] = object;
    }
    Exception nested = null;
    Exception suppressed = null;
    if (args != null) {
        try {
            return InvokerHelper.invokeConstructorOf(type, args);
        } catch (InvokerInvocationException iie) {
            throw iie;
        } catch (GroovyRuntimeException e) {
            if (e.getMessage().contains("Could not find matching constructor for")) {
                try {
                    return InvokerHelper.invokeConstructorOf(type, object);
                } catch (InvokerInvocationException iie) {
                    throw iie;
                } catch (Exception ex) {
                    // let's ignore exception and return the original object
                    // as the caller has more context to be able to throw a more
                    // meaningful exception (but stash to get message later)
                    nested = e;
                    // keep the original exception as suppressed exception to allow easier failure analysis
                    suppressed = ex;
                }
            } else {
                nested = e;
            }
        } catch (Exception e) {
            // let's ignore exception and return the original object
            // as the caller has more context to be able to throw a more
            // meaningful exception (but stash to get message later)
            nested = e;
        }
    }
    GroovyCastException gce;
    if (nested != null) {
        gce = new GroovyCastException(object, type, nested);
    } else {
        gce = new GroovyCastException(object, type);
    }
    if (suppressed != null) {
        gce.addSuppressed(suppressed);
    }
    throw gce;
}
Also used : MethodClosure(org.codehaus.groovy.runtime.MethodClosure) Closure(groovy.lang.Closure) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) Collection(java.util.Collection) InvokerInvocationException(org.codehaus.groovy.runtime.InvokerInvocationException) NullObject(org.codehaus.groovy.runtime.NullObject) Method(java.lang.reflect.Method) Map(java.util.Map) InvokerInvocationException(org.codehaus.groovy.runtime.InvokerInvocationException) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) IOException(java.io.IOException)

Example 90 with Closure

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

the class DefaultGroovyMethods method use.

/**
 * Allows you to use a list of categories, specifying the list as varargs.
 * <code>use(CategoryClass1, CategoryClass2) { ... }</code>
 * This method saves having to wrap the category
 * classes in a list.
 *
 * @param self  any Object
 * @param array a list of category classes and a Closure
 * @return the value returned from the closure
 * @since 1.0
 */
public static Object use(Object self, Object[] array) {
    if (array.length < 2)
        throw new IllegalArgumentException("Expecting at least 2 arguments, a category class and a Closure");
    Closure closure;
    try {
        closure = (Closure) array[array.length - 1];
    } catch (ClassCastException e) {
        throw new IllegalArgumentException("Expecting a Closure to be the last argument");
    }
    List<Class> list = new ArrayList<>(array.length - 1);
    for (int i = 0; i < array.length - 1; ++i) {
        Class categoryClass;
        try {
            categoryClass = (Class) array[i];
        } catch (ClassCastException e) {
            throw new IllegalArgumentException("Expecting a Category Class for argument " + i);
        }
        list.add(categoryClass);
    }
    return GroovyCategorySupport.use(list, closure);
}
Also used : Closure(groovy.lang.Closure) ArrayList(java.util.ArrayList) ExpandoMetaClass(groovy.lang.ExpandoMetaClass) MetaClass(groovy.lang.MetaClass) MixinInMetaClass(org.codehaus.groovy.reflection.MixinInMetaClass) CachedSAMClass(org.codehaus.groovy.reflection.stdclasses.CachedSAMClass) DelegatingMetaClass(groovy.lang.DelegatingMetaClass)

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