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;
}
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);
}
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();
}
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;
}
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;
}
Aggregations