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