use of groovy.lang.Closure in project hudson-2.x by hudson.
the class BeanBuilder method methodMissing.
/**
* This method is invoked by Groovy when a method that's not defined in Java is invoked.
* We use that as a syntax for bean definition.
*/
public Object methodMissing(String name, Object arg) {
Object[] args = (Object[]) arg;
if (args.length == 0)
throw new MissingMethodException(name, getClass(), args);
if (args[0] instanceof Closure) {
// abstract bean definition
return invokeBeanDefiningMethod(name, args);
} else if (args[0] instanceof Class || args[0] instanceof RuntimeBeanReference || args[0] instanceof Map) {
return invokeBeanDefiningMethod(name, args);
} else if (args.length > 1 && args[args.length - 1] instanceof Closure) {
return invokeBeanDefiningMethod(name, args);
}
WebApplicationContext ctx = springConfig.getUnrefreshedApplicationContext();
MetaClass mc = DefaultGroovyMethods.getMetaClass(ctx);
if (!mc.respondsTo(ctx, name, args).isEmpty()) {
return mc.invokeMethod(ctx, name, args);
}
return this;
}
use of groovy.lang.Closure in project spock by spockframework.
the class PojoGestalt method createGestalt.
private PojoGestalt createGestalt(Type newType, Object[] args) {
Closure closure = null;
if (args != null && args.length > 0 && args[args.length - 1] instanceof Closure) {
closure = (Closure) args[args.length - 1];
args = CollectionUtil.copyArray(args, 0, args.length - 1);
}
// TODO: check that this succeeds (Type could be a TypeVariable etc.)
Class<?> newClazz = GenericTypeReflector.erase(newType);
Object newPojo = BuilderHelper.createInstance(newClazz, args);
ClosureBlueprint newBlueprint = closure == null ? null : new ClosureBlueprint(closure, newPojo);
return new PojoGestalt(newPojo, newType, newBlueprint, slotFactories);
}
use of groovy.lang.Closure in project spock by spockframework.
the class RequiresExtension method doVisit.
private void doVisit(Requires annotation, ISkippable skippable) {
Closure condition = createCondition(annotation.value());
Object result = evaluateCondition(condition);
if (!GroovyRuntimeUtil.isTruthy(result)) {
skippable.setSkipped(true);
}
}
use of groovy.lang.Closure in project grails-core by grails.
the class DefaultUrlMappingEvaluatorTests method testRedirectMappings.
public void testRedirectMappings() throws Exception {
GroovyShell shell = new GroovyShell();
Binding binding = new Binding();
Script script = shell.parse("mappings = {\n" + "\"/first\"(redirect:[controller: 'foo', action: 'bar'])\n" + "\"/second\"(redirect: '/bing/bang')\n" + "}");
script.setBinding(binding);
script.run();
Closure closure = (Closure) binding.getVariable("mappings");
List<UrlMapping> mappings = evaluator.evaluateMappings(closure);
assertEquals(2, mappings.size());
Object redirectInfo = mappings.get(0).getRedirectInfo();
assertTrue(redirectInfo instanceof Map);
Map redirectMap = (Map) redirectInfo;
assertEquals(2, redirectMap.size());
assertEquals("foo", redirectMap.get("controller"));
assertEquals("bar", redirectMap.get("action"));
assertEquals("/bing/bang", mappings.get(1).getRedirectInfo());
}
use of groovy.lang.Closure in project gradle by gradle.
the class DefaultConfigurableFileCollectionTest method canUseAClosureToSpecifyASingleFile.
@Test
public void canUseAClosureToSpecifyASingleFile() {
Closure closure = TestUtil.returns('a');
final File file = new File("1");
collection.from(closure);
context.checking(new Expectations() {
{
oneOf(resolverMock).resolve('a');
will(returnValue(file));
}
});
assertThat(collection.getFiles(), equalTo(toLinkedSet(file)));
}
Aggregations