Search in sources :

Example 16 with Closure

use of groovy.lang.Closure in project groovy by apache.

the class Node method build.

public void build(final GroovyObject builder, final Map namespaceMap, final Map<String, String> namespaceTagHints) {
    if (this.replacementNodeStack.empty()) {
        final Closure rest = new Closure(null) {

            public Object doCall(final Object o) {
                buildChildren(builder, namespaceMap, namespaceTagHints);
                return null;
            }
        };
        if (this.namespaceURI.length() == 0 && this.attributeNamespaces.isEmpty()) {
            builder.invokeMethod(this.name, new Object[] { this.attributes, rest });
        } else {
            final List newTags = new LinkedList();
            builder.getProperty("mkp");
            final List namespaces = (List) builder.invokeMethod("getNamespaces", new Object[] {});
            final Map current = (Map) namespaces.get(0);
            final Map pending = (Map) namespaces.get(1);
            if (this.attributeNamespaces.isEmpty()) {
                builder.getProperty(getTagFor(this.namespaceURI, current, pending, namespaceMap, namespaceTagHints, newTags, builder));
                builder.invokeMethod(this.name, new Object[] { this.attributes, rest });
            } else {
                final Map attributesWithNamespaces = new HashMap(this.attributes);
                for (Object key : this.attributes.keySet()) {
                    final Object attributeNamespaceURI = this.attributeNamespaces.get(key);
                    if (attributeNamespaceURI != null) {
                        attributesWithNamespaces.put(getTagFor(attributeNamespaceURI, current, pending, namespaceMap, namespaceTagHints, newTags, builder) + "$" + key, attributesWithNamespaces.remove(key));
                    }
                }
                builder.getProperty(getTagFor(this.namespaceURI, current, pending, namespaceMap, namespaceTagHints, newTags, builder));
                builder.invokeMethod(this.name, new Object[] { attributesWithNamespaces, rest });
            }
            // remove the new tags we had to define for this element
            if (!newTags.isEmpty()) {
                final Iterator iter = newTags.iterator();
                do {
                    pending.remove(iter.next());
                } while (iter.hasNext());
            }
        }
    } else {
        ((ReplacementNode) this.replacementNodeStack.peek()).build(builder, namespaceMap, namespaceTagHints);
    }
}
Also used : Closure(groovy.lang.Closure) HashMap(java.util.HashMap) Iterator(java.util.Iterator) GroovyObject(groovy.lang.GroovyObject) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) HashMap(java.util.HashMap) Map(java.util.Map) LinkedList(java.util.LinkedList)

Example 17 with Closure

use of groovy.lang.Closure in project groovy by apache.

the class Builder method fettleMethodMap.

private static Map fettleMethodMap(final Closure defaultGenerator, final Map methodMap) {
    final Map newMethodMap = new HashMap();
    for (Object o : methodMap.keySet()) {
        final Object key = o;
        final Object value = methodMap.get(key);
        if ((value instanceof Closure)) {
            newMethodMap.put(key, value);
        } else {
            newMethodMap.put(key, defaultGenerator.curry((Object[]) value));
        }
    }
    return newMethodMap;
}
Also used : Closure(groovy.lang.Closure) HashMap(java.util.HashMap) Map(java.util.Map) HashMap(java.util.HashMap)

Example 18 with Closure

use of groovy.lang.Closure in project cucumber-jvm by cucumber.

the class Hooks method addHook.

private static void addHook(Object[] tagsExpressionsAndBody, boolean before) {
    long timeoutMillis = DEFAULT_TIMEOUT;
    int order = DEFAULT_ORDER;
    boolean timeoutSet = false;
    boolean orderSet = false;
    Closure body = null;
    List<String> tagExpressions = new ArrayList<String>();
    for (Object o : tagsExpressionsAndBody) {
        if (o instanceof String) {
            tagExpressions.add((String) o);
        } else if (o instanceof Long) {
            if (timeoutSet) {
                throw new CucumberException("Two timeout (Long) arguments found; " + Long.toString(timeoutMillis) + ", and; " + Long.toString((Long) o));
            }
            timeoutMillis = (Long) o;
            timeoutSet = true;
        } else if (o instanceof Integer) {
            if (orderSet) {
                throw new CucumberException("Two order (Integer) arguments found; " + Integer.toString(order) + ", and; " + Integer.toString((Integer) o));
            }
            order = (Integer) o;
            orderSet = true;
        } else if (o instanceof Closure) {
            body = (Closure) o;
        } else {
            throw new CucumberException("An argument of the type " + o.getClass().getName() + " found, " + (before ? "Before" : "After") + " only allows the argument types " + "String - Tag, Long - timeout, Integer - order, and Closure");
        }
    }
    TagExpression tagExpression = new TagExpression(tagExpressions);
    if (before) {
        GroovyBackend.getInstance().addBeforeHook(tagExpression, timeoutMillis, order, body);
    } else {
        GroovyBackend.getInstance().addAfterHook(tagExpression, timeoutMillis, order, body);
    }
}
Also used : Closure(groovy.lang.Closure) ArrayList(java.util.ArrayList) CucumberException(cucumber.runtime.CucumberException) TagExpression(gherkin.TagExpression)

Example 19 with Closure

use of groovy.lang.Closure in project cucumber-jvm by cucumber.

the class GroovyStackTraceTest method setUp.

@Before
public void setUp() throws Throwable {
    Closure body = new MethodClosure("the owner", "length");
    groovyStepDefinition = new GroovyStepDefinition(null, 0, body, null, new ExceptionThrowingBackend());
}
Also used : MethodClosure(org.codehaus.groovy.runtime.MethodClosure) Closure(groovy.lang.Closure) MethodClosure(org.codehaus.groovy.runtime.MethodClosure) Before(org.junit.Before)

Example 20 with Closure

use of groovy.lang.Closure in project enumerable by hraberg.

the class ScalaTest method interactingWithGroovy.

@Test
public void interactingWithGroovy() throws Exception {
    ScriptEngine groovy = GroovyTest.getGroovyEngine();
    Closure<?> closure = (Closure<?>) groovy.eval("{ n, m -> n * m }");
    Function2<Object, Object, Object> times = toFunction(LambdaGroovy.toFn2(closure));
    assertEquals(6, times.apply(2, 3));
    scala.bind("timesGroovy", "Function2[Any, Any, Any]", times);
    assertEquals(120, scala.eval("List(1, 2, 3, 4, 5).reduceLeft(timesGroovy)"));
}
Also used : Closure(groovy.lang.Closure) ScriptEngine(javax.script.ScriptEngine) GroovyTest(org.enumerable.lambda.support.groovy.GroovyTest) JRubyTest(org.enumerable.lambda.support.jruby.JRubyTest) Test(org.junit.Test) ClojureTest(org.enumerable.lambda.support.clojure.ClojureTest) JavaScriptTest(org.enumerable.lambda.support.javascript.JavaScriptTest)

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