Search in sources :

Example 6 with ExpandoMetaClass

use of groovy.lang.ExpandoMetaClass in project grails-core by grails.

the class GrailsMetaClassUtils method getMetaClass.

public static MetaClass getMetaClass(Object instance) {
    if (instance instanceof GroovyObject) {
        GroovyObject groovyObject = (GroovyObject) instance;
        MetaClass metaClass = groovyObject.getMetaClass();
        metaClass = unwrapDelegatingMetaClass(metaClass);
        if (!(metaClass instanceof ExpandoMetaClass)) {
            metaClass = getExpandoMetaClass(instance.getClass());
            groovyObject.setMetaClass(metaClass);
        }
        return metaClass;
    }
    return getExpandoMetaClass(instance.getClass());
}
Also used : MetaClass(groovy.lang.MetaClass) ExpandoMetaClass(groovy.lang.ExpandoMetaClass) AdaptingMetaClass(groovy.lang.AdaptingMetaClass) DelegatingMetaClass(groovy.lang.DelegatingMetaClass) ExpandoMetaClass(groovy.lang.ExpandoMetaClass) GroovyObject(groovy.lang.GroovyObject)

Example 7 with ExpandoMetaClass

use of groovy.lang.ExpandoMetaClass in project grails-core by grails.

the class GrailsClassUtils method getExpandoMetaClass.

@SuppressWarnings("rawtypes")
public static MetaClass getExpandoMetaClass(Class clazz) {
    MetaClassRegistry registry = GroovySystem.getMetaClassRegistry();
    Assert.isTrue(registry.getMetaClassCreationHandler() instanceof ExpandoMetaClassCreationHandle, "Grails requires an instance of [ExpandoMetaClassCreationHandle] to be set in Groovy's MetaClassRegistry! (current is : " + registry.getMetaClassCreationHandler() + ")");
    MetaClass mc = registry.getMetaClass(clazz);
    AdaptingMetaClass adapter = null;
    if (mc instanceof AdaptingMetaClass) {
        adapter = (AdaptingMetaClass) mc;
        mc = ((AdaptingMetaClass) mc).getAdaptee();
    }
    if (!(mc instanceof ExpandoMetaClass)) {
        // removes cached version
        registry.removeMetaClass(clazz);
        mc = registry.getMetaClass(clazz);
        if (adapter != null) {
            adapter.setAdaptee(mc);
        }
    }
    Assert.isTrue(mc instanceof ExpandoMetaClass, "BUG! Method must return an instance of [ExpandoMetaClass]!");
    return mc;
}
Also used : MetaClassRegistry(groovy.lang.MetaClassRegistry) ExpandoMetaClass(groovy.lang.ExpandoMetaClass) MetaClass(groovy.lang.MetaClass) AdaptingMetaClass(groovy.lang.AdaptingMetaClass) ExpandoMetaClassCreationHandle(groovy.lang.ExpandoMetaClassCreationHandle) ExpandoMetaClass(groovy.lang.ExpandoMetaClass) AdaptingMetaClass(groovy.lang.AdaptingMetaClass)

Example 8 with ExpandoMetaClass

use of groovy.lang.ExpandoMetaClass in project grails-core by grails.

the class MetaClassRegistryCleaner method updateConstantMetaClass.

public void updateConstantMetaClass(MetaClassRegistryChangeEvent cmcu) {
    if (!cleaning) {
        MetaClass oldMetaClass = cmcu.getOldMetaClass();
        Class classToUpdate = cmcu.getClassToUpdate();
        Object instanceToUpdate = cmcu.getInstance();
        if (instanceToUpdate == null && (cmcu.getNewMetaClass() instanceof ExpandoMetaClass)) {
            updateMetaClassOfClass(oldMetaClass, classToUpdate);
        } else if (instanceToUpdate != null) {
            updateMetaClassOfInstance(oldMetaClass, instanceToUpdate);
        }
    }
}
Also used : MetaClass(groovy.lang.MetaClass) ExpandoMetaClass(groovy.lang.ExpandoMetaClass) MetaClass(groovy.lang.MetaClass) ExpandoMetaClass(groovy.lang.ExpandoMetaClass) ExpandoMetaClass(groovy.lang.ExpandoMetaClass)

Example 9 with ExpandoMetaClass

use of groovy.lang.ExpandoMetaClass 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 10 with ExpandoMetaClass

use of groovy.lang.ExpandoMetaClass 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

ExpandoMetaClass (groovy.lang.ExpandoMetaClass)10 MetaClass (groovy.lang.MetaClass)8 DelegatingMetaClass (groovy.lang.DelegatingMetaClass)5 MetaClassRegistry (groovy.lang.MetaClassRegistry)5 AdaptingMetaClass (groovy.lang.AdaptingMetaClass)4 GrailsPlugin (grails.plugins.GrailsPlugin)2 GroovyObject (groovy.lang.GroovyObject)2 GroovyRuntimeException (groovy.lang.GroovyRuntimeException)2 MetaMethod (groovy.lang.MetaMethod)2 MetaProperty (groovy.lang.MetaProperty)2 ArrayList (java.util.ArrayList)2 HandleMetaClass (org.codehaus.groovy.runtime.HandleMetaClass)2 MixedInMetaClass (org.codehaus.groovy.runtime.metaclass.MixedInMetaClass)2 MixinInstanceMetaMethod (org.codehaus.groovy.runtime.metaclass.MixinInstanceMetaMethod)2 MixinInstanceMetaProperty (org.codehaus.groovy.runtime.metaclass.MixinInstanceMetaProperty)2 NewInstanceMetaMethod (org.codehaus.groovy.runtime.metaclass.NewInstanceMetaMethod)2 Enhanced (grails.artefact.Enhanced)1 PluginException (grails.plugins.exceptions.PluginException)1 Closure (groovy.lang.Closure)1 ClosureInvokingMethod (groovy.lang.ClosureInvokingMethod)1