Search in sources :

Example 56 with GroovyObject

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;
}
Also used : GroovyCodeSource(groovy.lang.GroovyCodeSource) File(java.io.File) GroovyObject(groovy.lang.GroovyObject)

Example 57 with GroovyObject

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 });
                }
            }
        }
    };
}
Also used : Closure(groovy.lang.Closure) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) GroovyObject(groovy.lang.GroovyObject) GroovyObject(groovy.lang.GroovyObject)

Example 58 with GroovyObject

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);
    }
}
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 59 with GroovyObject

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

Example 60 with GroovyObject

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;
    }
}
Also used : GroovyClassLoader(groovy.lang.GroovyClassLoader) InputStream(java.io.InputStream) MissingPropertyException(groovy.lang.MissingPropertyException) GroovyObject(groovy.lang.GroovyObject)

Aggregations

GroovyObject (groovy.lang.GroovyObject)142 GroovyClassLoader (groovy.lang.GroovyClassLoader)15 Closure (groovy.lang.Closure)11 File (java.io.File)8 Map (java.util.Map)8 Decorator (com.opensymphony.module.sitemesh.Decorator)7 GroovyRuntimeException (groovy.lang.GroovyRuntimeException)7 MetaClass (groovy.lang.MetaClass)7 HashMap (java.util.HashMap)7 GrailsWebRequest (org.grails.web.servlet.mvc.GrailsWebRequest)7 Page (com.opensymphony.module.sitemesh.Page)5 HTMLPageParser (com.opensymphony.module.sitemesh.parser.HTMLPageParser)5 Config (grails.config.Config)5 InvocationTargetException (java.lang.reflect.InvocationTargetException)5 ServletContext (javax.servlet.ServletContext)5 PropertySourcesConfig (org.grails.config.PropertySourcesConfig)5 MockApplicationContext (org.grails.support.MockApplicationContext)5 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)5 MockServletConfig (org.springframework.mock.web.MockServletConfig)5 ArrayList (java.util.ArrayList)4