Search in sources :

Example 1 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 2 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 3 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 4 with GroovyObject

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

the class GroovyWorld method findWorldWithMethod.

private GroovyObject findWorldWithMethod(String methodName, Object arguments) {
    Object[] args = unwrapMethodArguments(arguments);
    if (worlds.isEmpty()) {
        throw new MissingMethodException(methodName, this.getClass(), args);
    }
    if (worlds.size() == 1) {
        return worlds.get(0);
    }
    GroovyObject worldWithMethod = null;
    for (GroovyObject world : worlds) {
        if (world.getMetaClass().getMetaMethod(methodName, args) != null) {
            if (worldWithMethod == null) {
                worldWithMethod = world;
            } else {
                throw new RuntimeException("Multiple method call: " + methodName);
            }
        }
    }
    if (worldWithMethod == null) {
        throw new MissingMethodException(methodName, this.getClass(), args);
    }
    return worldWithMethod;
}
Also used : MissingMethodException(groovy.lang.MissingMethodException) GroovyObject(groovy.lang.GroovyObject) GroovyObject(groovy.lang.GroovyObject)

Example 5 with GroovyObject

use of groovy.lang.GroovyObject in project runesource by PureCS.

the class PluginHandler method loadPlugins.

/**
     * Loads all plugins.
     *
     * @param pluginsFile a text file containing a list of plugins to load
     * @throws Exception
     */
public static void loadPlugins(String pluginsFile) throws Exception {
    loadedPluginFile = pluginsFile;
    File file = new File(pluginsFile);
    BufferedReader reader = new BufferedReader(new FileReader(file));
    String pluginName;
    while ((pluginName = reader.readLine()) != null) {
        if (pluginName.trim().length() == 0)
            continue;
        // Loading the plugin instance
        Class cls = classLoader.parseClass(new File(PLUGIN_DIRECTORY + pluginName + ".groovy"));
        GroovyObject obj = (GroovyObject) cls.newInstance();
        Plugin plugin = (Plugin) obj;
        plugin.setInstance(obj);
        register(pluginName.replace('/', '.'), plugin);
    }
}
Also used : BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader) File(java.io.File) GroovyObject(groovy.lang.GroovyObject)

Aggregations

GroovyObject (groovy.lang.GroovyObject)165 GroovyClassLoader (groovy.lang.GroovyClassLoader)18 Closure (groovy.lang.Closure)16 MetaClass (groovy.lang.MetaClass)15 GroovyRuntimeException (groovy.lang.GroovyRuntimeException)11 ExpandoMetaClass (groovy.lang.ExpandoMetaClass)9 File (java.io.File)9 Map (java.util.Map)9 ArrayList (java.util.ArrayList)8 HashMap (java.util.HashMap)8 List (java.util.List)8 Decorator (com.opensymphony.module.sitemesh.Decorator)7 GrailsWebRequest (org.grails.web.servlet.mvc.GrailsWebRequest)7 DelegatingMetaClass (groovy.lang.DelegatingMetaClass)6 Page (com.opensymphony.module.sitemesh.Page)5 HTMLPageParser (com.opensymphony.module.sitemesh.parser.HTMLPageParser)5 Config (grails.config.Config)5 LinkedList (java.util.LinkedList)5 ServletContext (javax.servlet.ServletContext)5 PropertySourcesConfig (org.grails.config.PropertySourcesConfig)5