use of groovy.lang.Closure in project groovy-core by groovy.
the class GPathResult method getBody.
/**
* Creates a Closure representing the body of this GPathResult.
*
* @return the body of this GPathResult, converted to a <code>Closure</code>
*/
public Closure getBody() {
return new Closure(this.parent, this) {
public void doCall(Object[] args) {
final GroovyObject delegate = (GroovyObject) getDelegate();
final GPathResult thisObject = (GPathResult) getThisObject();
Node node = (Node) thisObject.getAt(0);
List children = node.children();
for (Object child : children) {
delegate.getProperty("mkp");
if (child instanceof Node) {
delegate.invokeMethod("yield", new Object[] { new NodeChild((Node) child, thisObject, "*", null) });
} else {
delegate.invokeMethod("yield", new Object[] { child });
}
}
}
};
}
use of groovy.lang.Closure in project groovy-core by groovy.
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-core by groovy.
the class Expando method hashCode.
/**
* This allows hashCode to be overridden by a closure <i>field</i> method attached
* to the expando object.
*
* @see java.lang.Object#hashCode()
*/
public int hashCode() {
Object method = getProperties().get("hashCode");
if (method != null && method instanceof Closure) {
// invoke overridden hashCode closure method
Closure closure = (Closure) method;
closure.setDelegate(this);
Integer ret = (Integer) closure.call();
return ret.intValue();
} else {
return super.hashCode();
}
}
use of groovy.lang.Closure in project groovy-core by groovy.
the class Expando method equals.
/**
* This allows equals to be overridden by a closure <i>field</i> method attached
* to the expando object.
*
* @see java.lang.Object#equals(java.lang.Object)
*/
public boolean equals(Object obj) {
Object method = getProperties().get("equals");
if (method != null && method instanceof Closure) {
// invoke overridden equals closure method
Closure closure = (Closure) method;
closure.setDelegate(this);
Boolean ret = (Boolean) closure.call(obj);
return ret.booleanValue();
} else {
return super.equals(obj);
}
}
use of groovy.lang.Closure in project groovy-core by groovy.
the class ImportCustomizerFactory method onNodeChildren.
@Override
public boolean onNodeChildren(final FactoryBuilderSupport builder, final Object node, final Closure childContent) {
if (node instanceof ImportCustomizer) {
Closure clone = (Closure) childContent.clone();
clone.setDelegate(new ImportHelper((ImportCustomizer) node));
clone.call();
}
return false;
}
Aggregations