Search in sources :

Example 11 with MetaClassImpl

use of groovy.lang.MetaClassImpl 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 12 with MetaClassImpl

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

the class CachedClass method addNewMopMethods.

public void addNewMopMethods(List<MetaMethod> arr) {
    final MetaClass metaClass = classInfo.getStrongMetaClass();
    if (metaClass != null) {
        if (metaClass.getClass() == MetaClassImpl.class) {
            classInfo.setStrongMetaClass(null);
            List<MetaMethod> res = new ArrayList<>();
            Collections.addAll(res, classInfo.newMetaMethods);
            res.addAll(arr);
            updateSetNewMopMethods(res);
            MetaClassImpl answer = new MetaClassImpl(((MetaClassImpl) metaClass).getRegistry(), metaClass.getTheClass());
            answer.initialize();
            classInfo.setStrongMetaClass(answer);
            return;
        }
        if (metaClass.getClass() == ExpandoMetaClass.class) {
            ExpandoMetaClass emc = (ExpandoMetaClass) metaClass;
            classInfo.setStrongMetaClass(null);
            updateAddNewMopMethods(arr);
            ExpandoMetaClass newEmc = new ExpandoMetaClass(metaClass.getTheClass());
            for (MetaMethod mm : emc.getExpandoMethods()) {
                newEmc.registerInstanceMethod(mm);
            }
            newEmc.initialize();
            classInfo.setStrongMetaClass(newEmc);
            return;
        }
        throw new GroovyRuntimeException("Can't add methods to class " + getTheClass().getName() + ". Strong custom meta class already set.");
    }
    classInfo.setWeakMetaClass(null);
    updateAddNewMopMethods(arr);
}
Also used : MetaMethod(groovy.lang.MetaMethod) MetaClass(groovy.lang.MetaClass) ExpandoMetaClass(groovy.lang.ExpandoMetaClass) ClosureMetaClass(org.codehaus.groovy.runtime.metaclass.ClosureMetaClass) ArrayList(java.util.ArrayList) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) ExpandoMetaClass(groovy.lang.ExpandoMetaClass) MetaClassImpl(groovy.lang.MetaClassImpl)

Example 13 with MetaClassImpl

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

the class CachedClass method setNewMopMethods.

public void setNewMopMethods(List<MetaMethod> arr) {
    final MetaClass metaClass = classInfo.getStrongMetaClass();
    if (metaClass != null) {
        if (metaClass.getClass() == MetaClassImpl.class) {
            classInfo.setStrongMetaClass(null);
            updateSetNewMopMethods(arr);
            MetaClassImpl mci = new MetaClassImpl(metaClass.getTheClass());
            mci.initialize();
            classInfo.setStrongMetaClass(mci);
            return;
        }
        if (metaClass.getClass() == ExpandoMetaClass.class) {
            classInfo.setStrongMetaClass(null);
            updateSetNewMopMethods(arr);
            ExpandoMetaClass newEmc = new ExpandoMetaClass(metaClass.getTheClass());
            newEmc.initialize();
            classInfo.setStrongMetaClass(newEmc);
            return;
        }
        throw new GroovyRuntimeException("Can't add methods to class " + getTheClass().getName() + ". Strong custom meta class already set.");
    }
    classInfo.setWeakMetaClass(null);
    updateSetNewMopMethods(arr);
}
Also used : MetaClass(groovy.lang.MetaClass) ExpandoMetaClass(groovy.lang.ExpandoMetaClass) ClosureMetaClass(org.codehaus.groovy.runtime.metaclass.ClosureMetaClass) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) ExpandoMetaClass(groovy.lang.ExpandoMetaClass) MetaClassImpl(groovy.lang.MetaClassImpl)

Example 14 with MetaClassImpl

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

the class AbstractCallSite method createPojoMetaClassGetPropertySite.

private CallSite createPojoMetaClassGetPropertySite(final Object receiver) {
    final MetaClass metaClass = InvokerHelper.getMetaClass(receiver);
    CallSite site;
    if (metaClass.getClass() != MetaClassImpl.class || GroovyCategorySupport.hasCategoryInCurrentThread()) {
        site = new PojoMetaClassGetPropertySite(this);
    } else {
        final MetaProperty effective = ((MetaClassImpl) metaClass).getEffectiveGetMetaProperty(receiver.getClass(), receiver, name, false);
        if (effective != null) {
            if (effective instanceof CachedField)
                site = new GetEffectivePojoFieldSite(this, (MetaClassImpl) metaClass, (CachedField) effective);
            else
                site = new GetEffectivePojoPropertySite(this, (MetaClassImpl) metaClass, effective);
        } else {
            site = new PojoMetaClassGetPropertySite(this);
        }
    }
    array.array[index] = site;
    return site;
}
Also used : MetaClass(groovy.lang.MetaClass) MetaProperty(groovy.lang.MetaProperty) CachedField(org.codehaus.groovy.reflection.CachedField) MetaClassImpl(groovy.lang.MetaClassImpl)

Example 15 with MetaClassImpl

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

the class CallSiteArray method createCallCurrentSite.

private static CallSite createCallCurrentSite(CallSite callSite, GroovyObject receiver, Object[] args, Class sender) {
    CallSite site;
    if (receiver instanceof GroovyInterceptable) {
        site = new PogoInterceptableSite(callSite);
    } else {
        MetaClass metaClass = receiver.getMetaClass();
        Class theClass = metaClass.getTheClass();
        if (receiver.getClass() != theClass && !theClass.isInterface()) {
            site = new PogoInterceptableSite(callSite);
        } else if (metaClass instanceof MetaClassImpl) {
            site = ((MetaClassImpl) metaClass).createPogoCallCurrentSite(callSite, sender, args);
        } else {
            site = new PogoMetaClassSite(callSite, metaClass);
        }
    }
    replaceCallSite(callSite, site);
    return site;
}
Also used : MetaClass(groovy.lang.MetaClass) GroovyInterceptable(groovy.lang.GroovyInterceptable) MetaClass(groovy.lang.MetaClass) MetaClassImpl(groovy.lang.MetaClassImpl)

Aggregations

MetaClassImpl (groovy.lang.MetaClassImpl)18 MetaClass (groovy.lang.MetaClass)17 ExpandoMetaClass (groovy.lang.ExpandoMetaClass)7 CachedClass (org.codehaus.groovy.reflection.CachedClass)4 ClosureMetaClass (org.codehaus.groovy.runtime.metaclass.ClosureMetaClass)4 GroovyInterceptable (groovy.lang.GroovyInterceptable)3 MetaMethod (groovy.lang.MetaMethod)3 ProxyMetaClass (groovy.lang.ProxyMetaClass)3 AdaptingMetaClass (groovy.lang.AdaptingMetaClass)2 GroovyRuntimeException (groovy.lang.GroovyRuntimeException)2 MetaProperty (groovy.lang.MetaProperty)2 CachedField (org.codehaus.groovy.reflection.CachedField)2 ClassInfo (org.codehaus.groovy.reflection.ClassInfo)2 CachedSAMClass (org.codehaus.groovy.reflection.stdclasses.CachedSAMClass)2 GroovyObject (groovy.lang.GroovyObject)1 MetaBeanProperty (groovy.lang.MetaBeanProperty)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Map (java.util.Map)1