Search in sources :

Example 96 with MetaClass

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

the class OwnedMetaClass method setProperty.

@Override
public void setProperty(Class sender, Object receiver, String messageName, Object messageValue, boolean useSuper, boolean fromInsideClass) {
    final Object owner = getOwner();
    final MetaClass ownerMetaClass = getOwnerMetaClass(owner);
    ownerMetaClass.setProperty(sender, owner, messageName, messageValue, useSuper, fromInsideClass);
}
Also used : MetaClass(groovy.lang.MetaClass) DelegatingMetaClass(groovy.lang.DelegatingMetaClass) GroovyObject(groovy.lang.GroovyObject)

Example 97 with MetaClass

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

the class OwnedMetaClass method getAttribute.

@Override
public Object getAttribute(Object object, String attribute) {
    final Object owner = getOwner();
    final MetaClass ownerMetaClass = getOwnerMetaClass(owner);
    return ownerMetaClass.getAttribute(owner, attribute);
}
Also used : MetaClass(groovy.lang.MetaClass) DelegatingMetaClass(groovy.lang.DelegatingMetaClass) GroovyObject(groovy.lang.GroovyObject)

Example 98 with MetaClass

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

the class OwnedMetaClass method getMethods.

@Override
public List<MetaMethod> getMethods() {
    final Object owner = getOwner();
    final MetaClass ownerMetaClass = getOwnerMetaClass(owner);
    return ownerMetaClass.getMethods();
}
Also used : MetaClass(groovy.lang.MetaClass) DelegatingMetaClass(groovy.lang.DelegatingMetaClass) GroovyObject(groovy.lang.GroovyObject)

Example 99 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)

Example 100 with MetaClass

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

the class NodeList method setMetaClass.

protected static void setMetaClass(final Class nodelistClass, final MetaClass metaClass) {
    final MetaClass newMetaClass = new DelegatingMetaClass(metaClass) {

        @Override
        public Object getAttribute(final Object object, final String attribute) {
            NodeList nl = (NodeList) object;
            Iterator it = nl.iterator();
            List result = new ArrayList();
            while (it.hasNext()) {
                Node node = (Node) it.next();
                result.add(node.attributes().get(attribute));
            }
            return result;
        }

        @Override
        public void setAttribute(final Object object, final String attribute, final Object newValue) {
            for (Object o : (NodeList) object) {
                Node node = (Node) o;
                node.attributes().put(attribute, newValue);
            }
        }

        @Override
        public Object getProperty(Object object, String property) {
            if (object instanceof NodeList) {
                NodeList nl = (NodeList) object;
                return nl.getAt(property);
            }
            return super.getProperty(object, property);
        }
    };
    GroovySystem.getMetaClassRegistry().setMetaClass(nodelistClass, newMetaClass);
}
Also used : MetaClass(groovy.lang.MetaClass) DelegatingMetaClass(groovy.lang.DelegatingMetaClass) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) DelegatingMetaClass(groovy.lang.DelegatingMetaClass)

Aggregations

MetaClass (groovy.lang.MetaClass)141 DelegatingMetaClass (groovy.lang.DelegatingMetaClass)62 GroovyObject (groovy.lang.GroovyObject)62 ExpandoMetaClass (groovy.lang.ExpandoMetaClass)40 MetaClassImpl (groovy.lang.MetaClassImpl)18 MetaProperty (groovy.lang.MetaProperty)12 GroovyRuntimeException (groovy.lang.GroovyRuntimeException)11 MetaMethod (groovy.lang.MetaMethod)11 ArrayList (java.util.ArrayList)11 MixinInMetaClass (org.codehaus.groovy.reflection.MixinInMetaClass)11 AdaptingMetaClass (groovy.lang.AdaptingMetaClass)9 MetaClassRegistry (groovy.lang.MetaClassRegistry)9 Map (java.util.Map)9 GString (groovy.lang.GString)7 CachedClass (org.codehaus.groovy.reflection.CachedClass)7 ClassInfo (org.codehaus.groovy.reflection.ClassInfo)7 MissingMethodException (groovy.lang.MissingMethodException)6 MissingPropertyException (groovy.lang.MissingPropertyException)6 IOException (java.io.IOException)6 Method (java.lang.reflect.Method)6