Search in sources :

Example 81 with MetaClass

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

the class MetaClassRegistryImpl method setMetaClass.

public void setMetaClass(Object obj, MetaClass theMetaClass) {
    Class theClass = obj.getClass();
    final ClassInfo info = ClassInfo.getClassInfo(theClass);
    MetaClass oldMC = null;
    info.lock();
    try {
        oldMC = info.getPerInstanceMetaClass(obj);
        info.setPerInstanceMetaClass(obj, theMetaClass);
    } finally {
        info.unlock();
    }
    fireConstantMetaClassUpdate(obj, theClass, oldMC, theMetaClass);
}
Also used : ExpandoMetaClass(groovy.lang.ExpandoMetaClass) MetaClass(groovy.lang.MetaClass) ExpandoMetaClass(groovy.lang.ExpandoMetaClass) MetaClass(groovy.lang.MetaClass) CachedClass(org.codehaus.groovy.reflection.CachedClass) ClassInfo(org.codehaus.groovy.reflection.ClassInfo)

Example 82 with MetaClass

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

the class ClosureMetaClass method getStaticMetaClass.

private static synchronized MetaClass getStaticMetaClass() {
    if (classMetaClass == null) {
        classMetaClass = new MetaClassImpl(Class.class);
        classMetaClass.initialize();
    }
    return classMetaClass;
}
Also used : ExpandoMetaClass(groovy.lang.ExpandoMetaClass) ProxyMetaClass(groovy.lang.ProxyMetaClass) MetaClass(groovy.lang.MetaClass) CachedClass(org.codehaus.groovy.reflection.CachedClass) MetaClassImpl(groovy.lang.MetaClassImpl)

Example 83 with MetaClass

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

the class ClosureMetaClass method getDelegateMethod.

private MetaMethod getDelegateMethod(final Closure closure, final Object delegate, final String methodName, final Class[] argClasses) {
    if (delegate == closure || delegate == null)
        return null;
    if (delegate instanceof Class) {
        for (Class superClass = (Class) delegate; superClass != Object.class && superClass != null; superClass = superClass.getSuperclass()) {
            MetaClass mc = registry.getMetaClass(superClass);
            MetaMethod method = mc.getStaticMetaMethod(methodName, argClasses);
            if (method != null)
                return method;
        }
        return null;
    } else if (delegate instanceof GroovyInterceptable) {
        MetaClass delegateMetaClass = lookupObjectMetaClass(delegate);
        // GROOVY-3015: must route calls through GroovyObject#invokeMethod(String,Object)
        MetaMethod interceptMethod = delegateMetaClass.pickMethod("invokeMethod", new Class[] { String.class, Object.class });
        return new TransformMetaMethod(interceptMethod) {

            @Override
            public Object invoke(final Object object, final Object[] arguments) {
                return super.invoke(object, new Object[] { methodName, arguments });
            }
        };
    } else {
        MetaClass delegateMetaClass = lookupObjectMetaClass(delegate);
        MetaMethod method = delegateMetaClass.pickMethod(methodName, argClasses);
        if (method != null) {
            return method;
        }
        if (delegateMetaClass instanceof ExpandoMetaClass) {
            method = ((ExpandoMetaClass) delegateMetaClass).findMixinMethod(methodName, argClasses);
            if (method != null) {
                onMixinMethodFound(method);
                return method;
            }
        }
        if (delegateMetaClass instanceof MetaClassImpl) {
            method = MetaClassImpl.findMethodInClassHierarchy(getTheClass(), methodName, argClasses, this);
            if (method != null) {
                onSuperMethodFoundInHierarchy(method);
                return method;
            }
        }
        return method;
    }
}
Also used : MetaMethod(groovy.lang.MetaMethod) ExpandoMetaClass(groovy.lang.ExpandoMetaClass) ProxyMetaClass(groovy.lang.ProxyMetaClass) MetaClass(groovy.lang.MetaClass) ExpandoMetaClass(groovy.lang.ExpandoMetaClass) ProxyMetaClass(groovy.lang.ProxyMetaClass) MetaClass(groovy.lang.MetaClass) CachedClass(org.codehaus.groovy.reflection.CachedClass) GroovyObject(groovy.lang.GroovyObject) GroovyInterceptable(groovy.lang.GroovyInterceptable) ExpandoMetaClass(groovy.lang.ExpandoMetaClass) MetaClassImpl(groovy.lang.MetaClassImpl)

Example 84 with MetaClass

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

the class ClosureMetaClass method lookupObjectMetaClass.

private MetaClass lookupObjectMetaClass(final Object object) {
    if (object instanceof GroovyObject) {
        GroovyObject go = (GroovyObject) object;
        return go.getMetaClass();
    }
    Class ownerClass = object.getClass();
    if (ownerClass == Class.class) {
        ownerClass = (Class) object;
        return registry.getMetaClass(ownerClass);
    }
    MetaClass metaClass = InvokerHelper.getMetaClass(object);
    return metaClass;
}
Also used : ExpandoMetaClass(groovy.lang.ExpandoMetaClass) ProxyMetaClass(groovy.lang.ProxyMetaClass) MetaClass(groovy.lang.MetaClass) ExpandoMetaClass(groovy.lang.ExpandoMetaClass) ProxyMetaClass(groovy.lang.ProxyMetaClass) MetaClass(groovy.lang.MetaClass) CachedClass(org.codehaus.groovy.reflection.CachedClass) GroovyObject(groovy.lang.GroovyObject)

Example 85 with MetaClass

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

the class OwnedMetaClass method invokeMethod.

@Override
public Object invokeMethod(Class sender, Object receiver, String methodName, Object[] arguments, boolean isCallToSuper, boolean fromInsideClass) {
    final Object owner = getOwner();
    final MetaClass ownerMetaClass = getOwnerMetaClass(owner);
    return ownerMetaClass.invokeMethod(sender, owner, methodName, arguments, isCallToSuper, fromInsideClass);
}
Also used : MetaClass(groovy.lang.MetaClass) DelegatingMetaClass(groovy.lang.DelegatingMetaClass) GroovyObject(groovy.lang.GroovyObject)

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