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