use of groovy.lang.GroovyObject in project groovy by apache.
the class Demo method compile.
protected GroovyObject compile(String fileName) throws Exception {
Class groovyClass = loader.parseClass(new GroovyCodeSource(new File(fileName)));
GroovyObject object = (GroovyObject) groovyClass.newInstance();
assertTrue(object != null);
return object;
}
use of groovy.lang.GroovyObject in project groovy by apache.
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.GroovyObject 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.GroovyObject in project atlas by alibaba.
the class ClosureFactory method buildClosure.
public static Closure<?> buildClosure(String... strings) throws IOException {
Closure<?> closure = null;
// Create a method returning a closure
StringBuilder sb = new StringBuilder("def closure() { { script -> ");
sb.append(StringUtils.join(strings, "\n"));
sb.append(" } }");
// Create an anonymous class for the method
GroovyClassLoader loader = new GroovyClassLoader();
Class<?> groovyClass = loader.parseClass(sb.toString());
try {
// Create an instance of the class
GroovyObject groovyObject = (GroovyObject) groovyClass.newInstance();
// Invoke the object's method and thus obtain the closure
closure = (Closure<?>) groovyObject.invokeMethod("closure", null);
} catch (Throwable e) {
throw new RuntimeException(e);
} finally {
loader.close();
}
return closure;
}
use of groovy.lang.GroovyObject in project qi4j-sdk by Qi4j.
the class GroovyMixin method invokeAsObject.
private Object invokeAsObject(Method method, Object[] args, URL groovySource) throws Throwable {
try {
Class declaringClass = method.getDeclaringClass();
GroovyObject groovyObject = groovyObjects.get(declaringClass);
if (groovyObject == null) {
InputStream is = null;
final Class groovyClass;
try {
is = groovySource.openStream();
StringBuilder sourceBuilder = new StringBuilder();
Inputs.text(groovySource).transferTo(Outputs.text(sourceBuilder));
GroovyClassLoader groovyClassLoader = new GroovyClassLoader(declaringClass.getClassLoader());
groovyClass = groovyClassLoader.parseClass(sourceBuilder.toString());
} finally {
if (is != null) {
is.close();
}
}
groovyObject = (GroovyObject) groovyClass.newInstance();
if (hasProperty(groovyObject, "This")) {
groovyObject.setProperty("This", me);
}
groovyObjects.put(declaringClass, groovyObject);
}
return groovyObject.invokeMethod(method.getName(), args);
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}
Aggregations