Search in sources :

Example 66 with Closure

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

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, type.isInterface());
        }
    }
    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;
    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;
                }
            } 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);
    }
    throw gce;
}
Also used : Closure(groovy.lang.Closure) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) Method(java.lang.reflect.Method) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) IOException(java.io.IOException)

Example 67 with Closure

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

the class ClosureStaticMetaMethod method invoke.

public Object invoke(Object object, Object[] arguments) {
    Closure cloned = (Closure) callable.clone();
    cloned.setDelegate(object);
    return cloned.call(arguments);
}
Also used : Closure(groovy.lang.Closure)

Example 68 with Closure

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

the class AbstractTypeCheckingExtension method scopeExit.

public TypeCheckingScope scopeExit(Closure code) {
    TypeCheckingScope scope = scopeData.peek();
    Closure copy = code.rehydrate(scope, this, this);
    copy.call();
    return scopeExit();
}
Also used : Closure(groovy.lang.Closure)

Example 69 with Closure

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

the class AbstractTypeCheckingExtension method newScope.

public TypeCheckingScope newScope(Closure code) {
    TypeCheckingScope scope = newScope();
    Closure callback = code.rehydrate(scope, this, this);
    callback.call();
    return scope;
}
Also used : Closure(groovy.lang.Closure)

Example 70 with Closure

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

the class GroovyTypeCheckingExtensionSupport method handleUnresolvedAttribute.

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

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