Search in sources :

Example 1 with AdaptingMetaClass

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

the class GrailsMetaClassUtils method copyExpandoMetaClass.

/**
     * Copies the ExpandoMetaClass dynamic methods and properties from one Class to another.
     *
     * @param fromClass The source class
     * @param toClass  The destination class
     * @param removeSource Whether to remove the source class after completion. True if yes
     */
@SuppressWarnings({ "unchecked", "rawtypes" })
public static void copyExpandoMetaClass(Class<?> fromClass, Class<?> toClass, boolean removeSource) {
    MetaClassRegistry registry = getRegistry();
    MetaClass oldMetaClass = registry.getMetaClass(fromClass);
    AdaptingMetaClass adapter = null;
    ExpandoMetaClass emc;
    if (oldMetaClass instanceof AdaptingMetaClass) {
        adapter = ((AdaptingMetaClass) oldMetaClass);
        emc = (ExpandoMetaClass) adapter.getAdaptee();
        if (LOG.isDebugEnabled()) {
            LOG.debug("Obtained adapted MetaClass [" + emc + "] from AdapterMetaClass instance [" + adapter + "]");
        }
        if (removeSource) {
            registry.removeMetaClass(fromClass);
        }
    } else {
        emc = (ExpandoMetaClass) oldMetaClass;
        if (LOG.isDebugEnabled()) {
            LOG.debug("No adapter MetaClass found, using original [" + emc + "]");
        }
    }
    ExpandoMetaClass replacement = new ExpandoMetaClass(toClass, true, true);
    for (Object obj : emc.getExpandoMethods()) {
        if (obj instanceof ClosureInvokingMethod) {
            ClosureInvokingMethod cim = (ClosureInvokingMethod) obj;
            Closure callable = cim.getClosure();
            if (!cim.isStatic()) {
                replacement.setProperty(cim.getName(), callable);
            } else {
                ((GroovyObject) replacement.getProperty(ExpandoMetaClass.STATIC_QUALIFIER)).setProperty(cim.getName(), callable);
            }
        }
    }
    for (Object o : emc.getExpandoProperties()) {
        if (o instanceof ThreadManagedMetaBeanProperty) {
            ThreadManagedMetaBeanProperty mbp = (ThreadManagedMetaBeanProperty) o;
            replacement.setProperty(mbp.getName(), mbp.getInitialValue());
        }
    }
    replacement.initialize();
    if (adapter == null) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Adding MetaClass for class [" + toClass + "] MetaClass [" + replacement + "]");
        }
        registry.setMetaClass(toClass, replacement);
    } else {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Adding MetaClass for class [" + toClass + "] MetaClass [" + replacement + "] with adapter [" + adapter + "]");
        }
        try {
            Constructor c = adapter.getClass().getConstructor(new Class[] { MetaClass.class });
            MetaClass newAdapter = (MetaClass) BeanUtils.instantiateClass(c, new Object[] { replacement });
            registry.setMetaClass(toClass, newAdapter);
        } catch (NoSuchMethodException e) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Exception thrown constructing new MetaClass adapter when reloading: " + e.getMessage(), e);
            }
        }
    }
}
Also used : MetaClassRegistry(groovy.lang.MetaClassRegistry) MetaClass(groovy.lang.MetaClass) ExpandoMetaClass(groovy.lang.ExpandoMetaClass) AdaptingMetaClass(groovy.lang.AdaptingMetaClass) DelegatingMetaClass(groovy.lang.DelegatingMetaClass) Closure(groovy.lang.Closure) ThreadManagedMetaBeanProperty(org.codehaus.groovy.runtime.metaclass.ThreadManagedMetaBeanProperty) Constructor(java.lang.reflect.Constructor) ClosureInvokingMethod(groovy.lang.ClosureInvokingMethod) GroovyObject(groovy.lang.GroovyObject) ExpandoMetaClass(groovy.lang.ExpandoMetaClass) AdaptingMetaClass(groovy.lang.AdaptingMetaClass) GroovyObject(groovy.lang.GroovyObject)

Example 2 with AdaptingMetaClass

use of groovy.lang.AdaptingMetaClass 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 3 with AdaptingMetaClass

use of groovy.lang.AdaptingMetaClass in project groovy-core by groovy.

the class Selector method getMetaClassImpl.

/**
     * Returns the MetaClassImpl if the given MetaClass is one of
     * MetaClassImpl, AdaptingMetaClass or ClosureMetaClass. If
     * none of these cases matches, this method returns null.
     */
private static MetaClassImpl getMetaClassImpl(MetaClass mc, boolean includeEMC) {
    Class mcc = mc.getClass();
    boolean valid = mcc == MetaClassImpl.class || mcc == AdaptingMetaClass.class || mcc == ClosureMetaClass.class || (includeEMC && mcc == ExpandoMetaClass.class);
    if (!valid) {
        if (LOG_ENABLED)
            LOG.info("meta class is neither MetaClassImpl, nor AdoptingMetaClass, nor ClosureMetaClass, normal method selection path disabled.");
        return null;
    }
    if (LOG_ENABLED)
        LOG.info("meta class is a recognized MetaClassImpl");
    return (MetaClassImpl) mc;
}
Also used : CachedSAMClass(org.codehaus.groovy.reflection.stdclasses.CachedSAMClass) AdaptingMetaClass(groovy.lang.AdaptingMetaClass) ExpandoMetaClass(groovy.lang.ExpandoMetaClass) ClosureMetaClass(org.codehaus.groovy.runtime.metaclass.ClosureMetaClass) MetaClass(groovy.lang.MetaClass) ExpandoMetaClass(groovy.lang.ExpandoMetaClass) MetaClassImpl(groovy.lang.MetaClassImpl) AdaptingMetaClass(groovy.lang.AdaptingMetaClass)

Example 4 with AdaptingMetaClass

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

the class Selector method getMetaClassImpl.

/**
     * Returns the MetaClassImpl if the given MetaClass is one of
     * MetaClassImpl, AdaptingMetaClass or ClosureMetaClass. If
     * none of these cases matches, this method returns null.
     */
private static MetaClassImpl getMetaClassImpl(MetaClass mc, boolean includeEMC) {
    Class mcc = mc.getClass();
    boolean valid = mcc == MetaClassImpl.class || mcc == AdaptingMetaClass.class || mcc == ClosureMetaClass.class || (includeEMC && mcc == ExpandoMetaClass.class);
    if (!valid) {
        if (LOG_ENABLED)
            LOG.info("meta class is neither MetaClassImpl, nor AdoptingMetaClass, nor ClosureMetaClass, normal method selection path disabled.");
        return null;
    }
    if (LOG_ENABLED)
        LOG.info("meta class is a recognized MetaClassImpl");
    return (MetaClassImpl) mc;
}
Also used : CachedSAMClass(org.codehaus.groovy.reflection.stdclasses.CachedSAMClass) AdaptingMetaClass(groovy.lang.AdaptingMetaClass) ExpandoMetaClass(groovy.lang.ExpandoMetaClass) ClosureMetaClass(org.codehaus.groovy.runtime.metaclass.ClosureMetaClass) MetaClass(groovy.lang.MetaClass) ExpandoMetaClass(groovy.lang.ExpandoMetaClass) MetaClassImpl(groovy.lang.MetaClassImpl) AdaptingMetaClass(groovy.lang.AdaptingMetaClass)

Aggregations

AdaptingMetaClass (groovy.lang.AdaptingMetaClass)4 ExpandoMetaClass (groovy.lang.ExpandoMetaClass)4 MetaClass (groovy.lang.MetaClass)4 MetaClassImpl (groovy.lang.MetaClassImpl)2 MetaClassRegistry (groovy.lang.MetaClassRegistry)2 CachedSAMClass (org.codehaus.groovy.reflection.stdclasses.CachedSAMClass)2 ClosureMetaClass (org.codehaus.groovy.runtime.metaclass.ClosureMetaClass)2 Closure (groovy.lang.Closure)1 ClosureInvokingMethod (groovy.lang.ClosureInvokingMethod)1 DelegatingMetaClass (groovy.lang.DelegatingMetaClass)1 ExpandoMetaClassCreationHandle (groovy.lang.ExpandoMetaClassCreationHandle)1 GroovyObject (groovy.lang.GroovyObject)1 Constructor (java.lang.reflect.Constructor)1 ThreadManagedMetaBeanProperty (org.codehaus.groovy.runtime.metaclass.ThreadManagedMetaBeanProperty)1