Search in sources :

Example 1 with MixinInstanceMetaMethod

use of org.codehaus.groovy.runtime.metaclass.MixinInstanceMetaMethod in project groovy by apache.

the class ExpandoMetaClass method findMixinMethod.

public MetaMethod findMixinMethod(String methodName, Class[] arguments) {
    for (MixinInMetaClass mixin : mixinClasses) {
        final CachedClass mixinClass = mixin.getMixinClass();
        MetaClass metaClass = mixinClass.classInfo.getMetaClassForClass();
        if (metaClass == null) {
            metaClass = GroovySystem.getMetaClassRegistry().getMetaClass(mixinClass.getTheClass());
        }
        MetaMethod metaMethod = metaClass.pickMethod(methodName, arguments);
        if (metaMethod == null && metaClass instanceof MetaClassImpl) {
            MetaClassImpl mc = (MetaClassImpl) metaClass;
            for (CachedClass cl = mc.getTheCachedClass().getCachedSuperClass(); cl != null; cl = cl.getCachedSuperClass()) {
                metaMethod = mc.getMethodWithoutCaching(cl.getTheClass(), methodName, arguments, false);
                if (metaMethod != null)
                    break;
            }
        }
        if (metaMethod != null) {
            MetaMethod method = new MixinInstanceMetaMethod(metaMethod, mixin);
            if (method.getParameterTypes().length == 1 && !method.getParameterTypes()[0].isPrimitive) {
                MetaMethod noParam = pickMethod(methodName, EMPTY_CLASS_ARRAY);
                // if the current call itself is with empty arg class array, no need to recurse with 'new Class[0]'
                if (noParam == null && arguments.length != 0) {
                    try {
                        findMixinMethod(methodName, EMPTY_CLASS_ARRAY);
                    } catch (MethodSelectionException msex) {
                    /*
                             * Here we just additionally tried to find another no-arg mixin method of the same name and register that as well, if found.
                             * Safe to ignore a MethodSelectionException in this additional exercise. (GROOVY-4999)
                             */
                    }
                }
            }
            registerInstanceMethod(method);
            return method;
        }
    }
    return null;
}
Also used : ClosureStaticMetaMethod(org.codehaus.groovy.runtime.metaclass.ClosureStaticMetaMethod) MixinInstanceMetaMethod(org.codehaus.groovy.runtime.metaclass.MixinInstanceMetaMethod) ClosureMetaMethod(org.codehaus.groovy.runtime.metaclass.ClosureMetaMethod) MethodSelectionException(org.codehaus.groovy.runtime.metaclass.MethodSelectionException) OwnedMetaClass(org.codehaus.groovy.runtime.metaclass.OwnedMetaClass) MixedInMetaClass(org.codehaus.groovy.runtime.metaclass.MixedInMetaClass) MixinInMetaClass(org.codehaus.groovy.reflection.MixinInMetaClass) MixinInMetaClass(org.codehaus.groovy.reflection.MixinInMetaClass) CachedClass(org.codehaus.groovy.reflection.CachedClass) MixinInstanceMetaMethod(org.codehaus.groovy.runtime.metaclass.MixinInstanceMetaMethod)

Example 2 with MixinInstanceMetaMethod

use of org.codehaus.groovy.runtime.metaclass.MixinInstanceMetaMethod in project groovy-core by groovy.

the class ExpandoMetaClass method findMixinMethod.

public MetaMethod findMixinMethod(String methodName, Class[] arguments) {
    for (MixinInMetaClass mixin : mixinClasses) {
        final CachedClass mixinClass = mixin.getMixinClass();
        MetaClass metaClass = mixinClass.classInfo.getMetaClassForClass();
        if (metaClass == null) {
            metaClass = GroovySystem.getMetaClassRegistry().getMetaClass(mixinClass.getTheClass());
        }
        MetaMethod metaMethod = metaClass.pickMethod(methodName, arguments);
        if (metaMethod == null && metaClass instanceof MetaClassImpl) {
            MetaClassImpl mc = (MetaClassImpl) metaClass;
            for (CachedClass cl = mc.getTheCachedClass().getCachedSuperClass(); cl != null; cl = cl.getCachedSuperClass()) {
                metaMethod = mc.getMethodWithoutCaching(cl.getTheClass(), methodName, arguments, false);
                if (metaMethod != null)
                    break;
            }
        }
        if (metaMethod != null) {
            MetaMethod method = new MixinInstanceMetaMethod(metaMethod, mixin);
            if (method.getParameterTypes().length == 1 && !method.getParameterTypes()[0].isPrimitive) {
                MetaMethod noParam = pickMethod(methodName, EMPTY_CLASS_ARRAY);
                // if the current call itself is with empty arg class array, no need to recurse with 'new Class[0]'
                if (noParam == null && arguments.length != 0) {
                    try {
                        findMixinMethod(methodName, EMPTY_CLASS_ARRAY);
                    } catch (MethodSelectionException msex) {
                    /*
                             * Here we just additionally tried to find another no-arg mixin method of the same name and register that as well, if found.
                             * Safe to ignore a MethodSelectionException in this additional exercise. (GROOVY-4999)
                             */
                    }
                }
            }
            registerInstanceMethod(method);
            return method;
        }
    }
    return null;
}
Also used : ClosureStaticMetaMethod(org.codehaus.groovy.runtime.metaclass.ClosureStaticMetaMethod) MixinInstanceMetaMethod(org.codehaus.groovy.runtime.metaclass.MixinInstanceMetaMethod) ClosureMetaMethod(org.codehaus.groovy.runtime.metaclass.ClosureMetaMethod) MethodSelectionException(org.codehaus.groovy.runtime.metaclass.MethodSelectionException) OwnedMetaClass(org.codehaus.groovy.runtime.metaclass.OwnedMetaClass) MixedInMetaClass(org.codehaus.groovy.runtime.metaclass.MixedInMetaClass) MixinInMetaClass(org.codehaus.groovy.reflection.MixinInMetaClass) MixinInMetaClass(org.codehaus.groovy.reflection.MixinInMetaClass) CachedClass(org.codehaus.groovy.reflection.CachedClass) MixinInstanceMetaMethod(org.codehaus.groovy.runtime.metaclass.MixinInstanceMetaMethod)

Example 3 with MixinInstanceMetaMethod

use of org.codehaus.groovy.runtime.metaclass.MixinInstanceMetaMethod in project groovy-core by groovy.

the class MixinInMetaClass method mixinClassesToMetaClass.

public static void mixinClassesToMetaClass(MetaClass self, List<Class> categoryClasses) {
    final Class selfClass = self.getTheClass();
    if (self instanceof HandleMetaClass) {
        self = (MetaClass) ((HandleMetaClass) self).replaceDelegate();
    }
    if (!(self instanceof ExpandoMetaClass)) {
        if (self instanceof DelegatingMetaClass && ((DelegatingMetaClass) self).getAdaptee() instanceof ExpandoMetaClass) {
            self = ((DelegatingMetaClass) self).getAdaptee();
        } else {
            throw new GroovyRuntimeException("Can't mixin methods to meta class: " + self);
        }
    }
    ExpandoMetaClass mc = (ExpandoMetaClass) self;
    List<MetaMethod> arr = new ArrayList<MetaMethod>();
    for (Class categoryClass : categoryClasses) {
        final CachedClass cachedCategoryClass = ReflectionCache.getCachedClass(categoryClass);
        final MixinInMetaClass mixin = new MixinInMetaClass(mc, cachedCategoryClass);
        final MetaClass metaClass = GroovySystem.getMetaClassRegistry().getMetaClass(categoryClass);
        final List<MetaProperty> propList = metaClass.getProperties();
        for (MetaProperty prop : propList) if (self.getMetaProperty(prop.getName()) == null) {
            mc.registerBeanProperty(prop.getName(), new MixinInstanceMetaProperty(prop, mixin));
        }
        for (MetaProperty prop : cachedCategoryClass.getFields()) if (self.getMetaProperty(prop.getName()) == null) {
            mc.registerBeanProperty(prop.getName(), new MixinInstanceMetaProperty(prop, mixin));
        }
        for (MetaMethod method : metaClass.getMethods()) {
            final int mod = method.getModifiers();
            if (!Modifier.isPublic(mod))
                continue;
            if (method instanceof CachedMethod && ((CachedMethod) method).getCachedMethod().isSynthetic())
                continue;
            if (Modifier.isStatic(mod)) {
                if (method instanceof CachedMethod)
                    staticMethod(self, arr, (CachedMethod) method);
            } else if (method.getDeclaringClass().getTheClass() != Object.class || method.getName().equals("toString")) {
                //                    if (self.pickMethod(method.getName(), method.getNativeParameterTypes()) == null) {
                final MixinInstanceMetaMethod metaMethod = new MixinInstanceMetaMethod(method, mixin);
                arr.add(metaMethod);
            //                    }
            }
        }
    }
    for (Object res : arr) {
        final MetaMethod metaMethod = (MetaMethod) res;
        if (metaMethod.getDeclaringClass().isAssignableFrom(selfClass))
            mc.registerInstanceMethod(metaMethod);
        else {
            mc.registerSubclassInstanceMethod(metaMethod);
        }
    }
}
Also used : MixinInstanceMetaMethod(org.codehaus.groovy.runtime.metaclass.MixinInstanceMetaMethod) MetaMethod(groovy.lang.MetaMethod) NewInstanceMetaMethod(org.codehaus.groovy.runtime.metaclass.NewInstanceMetaMethod) HandleMetaClass(org.codehaus.groovy.runtime.HandleMetaClass) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) ArrayList(java.util.ArrayList) MetaClass(groovy.lang.MetaClass) ExpandoMetaClass(groovy.lang.ExpandoMetaClass) MixedInMetaClass(org.codehaus.groovy.runtime.metaclass.MixedInMetaClass) HandleMetaClass(org.codehaus.groovy.runtime.HandleMetaClass) DelegatingMetaClass(groovy.lang.DelegatingMetaClass) MixinInstanceMetaProperty(org.codehaus.groovy.runtime.metaclass.MixinInstanceMetaProperty) MetaClass(groovy.lang.MetaClass) ExpandoMetaClass(groovy.lang.ExpandoMetaClass) MixedInMetaClass(org.codehaus.groovy.runtime.metaclass.MixedInMetaClass) HandleMetaClass(org.codehaus.groovy.runtime.HandleMetaClass) DelegatingMetaClass(groovy.lang.DelegatingMetaClass) ExpandoMetaClass(groovy.lang.ExpandoMetaClass) MixinInstanceMetaProperty(org.codehaus.groovy.runtime.metaclass.MixinInstanceMetaProperty) MetaProperty(groovy.lang.MetaProperty) DelegatingMetaClass(groovy.lang.DelegatingMetaClass) MixinInstanceMetaMethod(org.codehaus.groovy.runtime.metaclass.MixinInstanceMetaMethod)

Example 4 with MixinInstanceMetaMethod

use of org.codehaus.groovy.runtime.metaclass.MixinInstanceMetaMethod in project groovy by apache.

the class MixinInMetaClass method mixinClassesToMetaClass.

public static void mixinClassesToMetaClass(MetaClass self, List<Class> categoryClasses) {
    final Class selfClass = self.getTheClass();
    if (self instanceof HandleMetaClass) {
        self = (MetaClass) ((HandleMetaClass) self).replaceDelegate();
    }
    if (!(self instanceof ExpandoMetaClass)) {
        if (self instanceof DelegatingMetaClass && ((DelegatingMetaClass) self).getAdaptee() instanceof ExpandoMetaClass) {
            self = ((DelegatingMetaClass) self).getAdaptee();
        } else {
            throw new GroovyRuntimeException("Can't mixin methods to meta class: " + self);
        }
    }
    ExpandoMetaClass mc = (ExpandoMetaClass) self;
    List<MetaMethod> arr = new ArrayList<MetaMethod>();
    for (Class categoryClass : categoryClasses) {
        final CachedClass cachedCategoryClass = ReflectionCache.getCachedClass(categoryClass);
        final MixinInMetaClass mixin = new MixinInMetaClass(mc, cachedCategoryClass);
        final MetaClass metaClass = GroovySystem.getMetaClassRegistry().getMetaClass(categoryClass);
        final List<MetaProperty> propList = metaClass.getProperties();
        for (MetaProperty prop : propList) if (self.getMetaProperty(prop.getName()) == null) {
            mc.registerBeanProperty(prop.getName(), new MixinInstanceMetaProperty(prop, mixin));
        }
        for (MetaProperty prop : cachedCategoryClass.getFields()) if (self.getMetaProperty(prop.getName()) == null) {
            mc.registerBeanProperty(prop.getName(), new MixinInstanceMetaProperty(prop, mixin));
        }
        for (MetaMethod method : metaClass.getMethods()) {
            final int mod = method.getModifiers();
            if (!Modifier.isPublic(mod))
                continue;
            if (method instanceof CachedMethod && ((CachedMethod) method).getCachedMethod().isSynthetic())
                continue;
            if (Modifier.isStatic(mod)) {
                if (method instanceof CachedMethod)
                    staticMethod(self, arr, (CachedMethod) method);
            } else if (method.getDeclaringClass().getTheClass() != Object.class || method.getName().equals("toString")) {
                //                    if (self.pickMethod(method.getName(), method.getNativeParameterTypes()) == null) {
                final MixinInstanceMetaMethod metaMethod = new MixinInstanceMetaMethod(method, mixin);
                arr.add(metaMethod);
            //                    }
            }
        }
    }
    for (Object res : arr) {
        final MetaMethod metaMethod = (MetaMethod) res;
        if (metaMethod.getDeclaringClass().isAssignableFrom(selfClass))
            mc.registerInstanceMethod(metaMethod);
        else {
            mc.registerSubclassInstanceMethod(metaMethod);
        }
    }
}
Also used : MixinInstanceMetaMethod(org.codehaus.groovy.runtime.metaclass.MixinInstanceMetaMethod) MetaMethod(groovy.lang.MetaMethod) NewInstanceMetaMethod(org.codehaus.groovy.runtime.metaclass.NewInstanceMetaMethod) HandleMetaClass(org.codehaus.groovy.runtime.HandleMetaClass) GroovyRuntimeException(groovy.lang.GroovyRuntimeException) ArrayList(java.util.ArrayList) MetaClass(groovy.lang.MetaClass) ExpandoMetaClass(groovy.lang.ExpandoMetaClass) MixedInMetaClass(org.codehaus.groovy.runtime.metaclass.MixedInMetaClass) HandleMetaClass(org.codehaus.groovy.runtime.HandleMetaClass) DelegatingMetaClass(groovy.lang.DelegatingMetaClass) MixinInstanceMetaProperty(org.codehaus.groovy.runtime.metaclass.MixinInstanceMetaProperty) MetaClass(groovy.lang.MetaClass) ExpandoMetaClass(groovy.lang.ExpandoMetaClass) MixedInMetaClass(org.codehaus.groovy.runtime.metaclass.MixedInMetaClass) HandleMetaClass(org.codehaus.groovy.runtime.HandleMetaClass) DelegatingMetaClass(groovy.lang.DelegatingMetaClass) ExpandoMetaClass(groovy.lang.ExpandoMetaClass) MixinInstanceMetaProperty(org.codehaus.groovy.runtime.metaclass.MixinInstanceMetaProperty) MetaProperty(groovy.lang.MetaProperty) DelegatingMetaClass(groovy.lang.DelegatingMetaClass) MixinInstanceMetaMethod(org.codehaus.groovy.runtime.metaclass.MixinInstanceMetaMethod)

Aggregations

MixedInMetaClass (org.codehaus.groovy.runtime.metaclass.MixedInMetaClass)4 MixinInstanceMetaMethod (org.codehaus.groovy.runtime.metaclass.MixinInstanceMetaMethod)4 DelegatingMetaClass (groovy.lang.DelegatingMetaClass)2 ExpandoMetaClass (groovy.lang.ExpandoMetaClass)2 GroovyRuntimeException (groovy.lang.GroovyRuntimeException)2 MetaClass (groovy.lang.MetaClass)2 MetaMethod (groovy.lang.MetaMethod)2 MetaProperty (groovy.lang.MetaProperty)2 ArrayList (java.util.ArrayList)2 CachedClass (org.codehaus.groovy.reflection.CachedClass)2 MixinInMetaClass (org.codehaus.groovy.reflection.MixinInMetaClass)2 HandleMetaClass (org.codehaus.groovy.runtime.HandleMetaClass)2 ClosureMetaMethod (org.codehaus.groovy.runtime.metaclass.ClosureMetaMethod)2 ClosureStaticMetaMethod (org.codehaus.groovy.runtime.metaclass.ClosureStaticMetaMethod)2 MethodSelectionException (org.codehaus.groovy.runtime.metaclass.MethodSelectionException)2 MixinInstanceMetaProperty (org.codehaus.groovy.runtime.metaclass.MixinInstanceMetaProperty)2 NewInstanceMetaMethod (org.codehaus.groovy.runtime.metaclass.NewInstanceMetaMethod)2 OwnedMetaClass (org.codehaus.groovy.runtime.metaclass.OwnedMetaClass)2