Search in sources :

Example 56 with MetaClass

use of groovy.lang.MetaClass in project groovy by apache.

the class InvokerHelper method createScript.

public static Script createScript(Class scriptClass, Binding context) {
    Script script;
    if (scriptClass == null) {
        script = new NullScript(context);
    } else {
        try {
            if (Script.class.isAssignableFrom(scriptClass)) {
                try {
                    Constructor constructor = scriptClass.getConstructor(Binding.class);
                    script = (Script) constructor.newInstance(context);
                } catch (NoSuchMethodException e) {
                    // Fallback for non-standard "Script" classes.
                    script = (Script) scriptClass.newInstance();
                    script.setBinding(context);
                }
            } else {
                final GroovyObject object = (GroovyObject) scriptClass.newInstance();
                // it could just be a class, so let's wrap it in a Script
                // wrapper; though the bindings will be ignored
                script = new Script(context) {

                    public Object run() {
                        Object argsToPass = EMPTY_MAIN_ARGS;
                        try {
                            Object args = getProperty("args");
                            if (args instanceof String[]) {
                                argsToPass = args;
                            }
                        } catch (MissingPropertyException e) {
                        // They'll get empty args since none exist in the context.
                        }
                        object.invokeMethod("main", argsToPass);
                        return null;
                    }
                };
                Map variables = context.getVariables();
                MetaClass mc = getMetaClass(object);
                for (Object o : variables.entrySet()) {
                    Map.Entry entry = (Map.Entry) o;
                    String key = entry.getKey().toString();
                    // assume underscore variables are for the wrapper script
                    setPropertySafe(key.startsWith("_") ? script : object, mc, key, entry.getValue());
                }
            }
        } catch (Exception e) {
            throw new GroovyRuntimeException("Failed to create Script instance for class: " + scriptClass + ". Reason: " + e, e);
        }
    }
    return script;
}
Also used : Script(groovy.lang.Script) Constructor(java.lang.reflect.Constructor) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) MissingPropertyException(groovy.lang.MissingPropertyException) GString(groovy.lang.GString) MissingPropertyException(groovy.lang.MissingPropertyException) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) MissingMethodException(groovy.lang.MissingMethodException) InvocationTargetException(java.lang.reflect.InvocationTargetException) SpreadMapEvaluatingException(groovy.lang.SpreadMapEvaluatingException) IOException(java.io.IOException) GroovyObject(groovy.lang.GroovyObject) MetaClass(groovy.lang.MetaClass) GroovyObject(groovy.lang.GroovyObject) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) SpreadMap(groovy.lang.SpreadMap)

Example 57 with MetaClass

use of groovy.lang.MetaClass in project groovy by apache.

the class InvokerHelper method setProperties.

/**
     * Sets the properties on the given object
     */
public static void setProperties(Object object, Map map) {
    MetaClass mc = getMetaClass(object);
    for (Object o : map.entrySet()) {
        Map.Entry entry = (Map.Entry) o;
        String key = entry.getKey().toString();
        Object value = entry.getValue();
        setPropertySafe(object, mc, key, value);
    }
}
Also used : MetaClass(groovy.lang.MetaClass) GroovyObject(groovy.lang.GroovyObject) GString(groovy.lang.GString) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) SpreadMap(groovy.lang.SpreadMap)

Example 58 with MetaClass

use of groovy.lang.MetaClass in project groovy by apache.

the class InvokerHelper method invokeMethod.

/**
     * Invokes the given method on the object.
     */
public static Object invokeMethod(Object object, String methodName, Object arguments) {
    if (object == null) {
        object = NullObject.getNullObject();
    //throw new NullPointerException("Cannot invoke method " + methodName + "() on null object");
    }
    // if the object is a Class, call a static method from that class
    if (object instanceof Class) {
        Class theClass = (Class) object;
        MetaClass metaClass = metaRegistry.getMetaClass(theClass);
        return metaClass.invokeStaticMethod(object, methodName, asArray(arguments));
    }
    // it's an instance; check if it's a Java one
    if (!(object instanceof GroovyObject)) {
        return invokePojoMethod(object, methodName, arguments);
    }
    // a groovy instance (including builder, closure, ...)
    return invokePogoMethod(object, methodName, arguments);
}
Also used : MetaClass(groovy.lang.MetaClass) MetaClass(groovy.lang.MetaClass) GroovyObject(groovy.lang.GroovyObject)

Example 59 with MetaClass

use of groovy.lang.MetaClass in project groovy by apache.

the class InvokerHelper method invokeSuperMethod.

public static Object invokeSuperMethod(Object object, String methodName, Object arguments) {
    if (object == null) {
        throw new NullPointerException("Cannot invoke method " + methodName + "() on null object");
    }
    Class theClass = object.getClass();
    MetaClass metaClass = metaRegistry.getMetaClass(theClass.getSuperclass());
    return metaClass.invokeMethod(object, methodName, asArray(arguments));
}
Also used : MetaClass(groovy.lang.MetaClass) MetaClass(groovy.lang.MetaClass)

Example 60 with MetaClass

use of groovy.lang.MetaClass in project groovy by apache.

the class Node method setMetaClass.

/**
     * Extension point for subclasses to override the metaclass. The default
     * one supports the property and @ attribute notations.
     *
     * @param metaClass the original metaclass
     * @param nodeClass the class whose metaclass we wish to override (this class or a subclass)
     */
protected static void setMetaClass(final MetaClass metaClass, Class nodeClass) {
    // TODO Is protected static a bit of a smell?
    // TODO perhaps set nodeClass to be Class<? extends Node>
    final MetaClass newMetaClass = new DelegatingMetaClass(metaClass) {

        @Override
        public Object getAttribute(final Object object, final String attribute) {
            Node n = (Node) object;
            return n.get("@" + attribute);
        }

        @Override
        public void setAttribute(final Object object, final String attribute, final Object newValue) {
            Node n = (Node) object;
            n.attributes().put(attribute, newValue);
        }

        @Override
        public Object getProperty(Object object, String property) {
            if (object instanceof Node) {
                Node n = (Node) object;
                return n.get(property);
            }
            return super.getProperty(object, property);
        }

        @Override
        public void setProperty(Object object, String property, Object newValue) {
            if (property.startsWith("@")) {
                setAttribute(object, property.substring(1), newValue);
                return;
            }
            delegate.setProperty(object, property, newValue);
        }
    };
    GroovySystem.getMetaClassRegistry().setMetaClass(nodeClass, newMetaClass);
}
Also used : MetaClass(groovy.lang.MetaClass) DelegatingMetaClass(groovy.lang.DelegatingMetaClass) DelegatingMetaClass(groovy.lang.DelegatingMetaClass)

Aggregations

MetaClass (groovy.lang.MetaClass)64 DelegatingMetaClass (groovy.lang.DelegatingMetaClass)18 GroovyObject (groovy.lang.GroovyObject)17 ExpandoMetaClass (groovy.lang.ExpandoMetaClass)14 MetaClassImpl (groovy.lang.MetaClassImpl)10 AdaptingMetaClass (groovy.lang.AdaptingMetaClass)9 GString (groovy.lang.GString)6 Map (java.util.Map)6 MetaClassRegistry (groovy.lang.MetaClassRegistry)5 MetaMethod (groovy.lang.MetaMethod)5 MissingMethodException (groovy.lang.MissingMethodException)5 HashMap (java.util.HashMap)5 GroovyRuntimeException (groovy.lang.GroovyRuntimeException)4 MetaProperty (groovy.lang.MetaProperty)4 MissingPropertyException (groovy.lang.MissingPropertyException)4 Script (groovy.lang.Script)4 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 ClassInfo (org.codehaus.groovy.reflection.ClassInfo)4 Binding (groovy.lang.Binding)3