Search in sources :

Example 21 with Closure

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;
}
Also used : MissingMethodException(groovy.lang.MissingMethodException) Closure(groovy.lang.Closure) MetaClass(groovy.lang.MetaClass) GroovyObject(groovy.lang.GroovyObject) MetaClass(groovy.lang.MetaClass) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference) HashMap(java.util.HashMap) Map(java.util.Map) ManagedMap(org.springframework.beans.factory.support.ManagedMap) WebApplicationContext(org.springframework.web.context.WebApplicationContext)

Example 22 with Closure

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);
}
Also used : Closure(groovy.lang.Closure)

Example 23 with Closure

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);
    }
}
Also used : Closure(groovy.lang.Closure)

Example 24 with Closure

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());
}
Also used : Binding(groovy.lang.Binding) UrlMapping(grails.web.mapping.UrlMapping) Script(groovy.lang.Script) Closure(groovy.lang.Closure) Map(java.util.Map) GroovyShell(groovy.lang.GroovyShell)

Example 25 with Closure

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)));
}
Also used : Expectations(org.jmock.Expectations) Closure(groovy.lang.Closure) File(java.io.File) Test(org.junit.Test)

Aggregations

Closure (groovy.lang.Closure)211 Map (java.util.Map)40 GroovyObject (groovy.lang.GroovyObject)27 HashMap (java.util.HashMap)27 Binding (groovy.lang.Binding)21 ArrayList (java.util.ArrayList)21 List (java.util.List)19 GString (groovy.lang.GString)17 GroovyShell (groovy.lang.GroovyShell)13 MethodClosure (org.codehaus.groovy.runtime.MethodClosure)13 LinkedHashMap (java.util.LinkedHashMap)12 Test (org.junit.Test)12 File (java.io.File)10 LinkedList (java.util.LinkedList)10 FileType (groovy.io.FileType)8 FileVisitResult (groovy.io.FileVisitResult)8 MetaClass (groovy.lang.MetaClass)8 Collection (java.util.Collection)8 Script (groovy.lang.Script)7 IOException (java.io.IOException)7