Search in sources :

Example 1 with ExpandoMetaClass

use of groovy.lang.ExpandoMetaClass in project spock by spockframework.

the class ConfineMetaClassChangesInterceptor method intercept.

public void intercept(IMethodInvocation invocation) throws Throwable {
    MetaClassRegistry registry = GroovySystem.getMetaClassRegistry();
    for (Class<?> clazz : classes) {
        originalMetaClasses.add(registry.getMetaClass(clazz));
        MetaClass temporaryMetaClass = new ExpandoMetaClass(clazz, true, true);
        temporaryMetaClass.initialize();
        registry.setMetaClass(clazz, temporaryMetaClass);
    }
    try {
        invocation.proceed();
    } finally {
        for (MetaClass original : originalMetaClasses) registry.setMetaClass(original.getTheClass(), original);
    }
}
Also used : MetaClassRegistry(groovy.lang.MetaClassRegistry) MetaClass(groovy.lang.MetaClass) ExpandoMetaClass(groovy.lang.ExpandoMetaClass) ExpandoMetaClass(groovy.lang.ExpandoMetaClass)

Example 2 with ExpandoMetaClass

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

the class AbstractGrailsPluginManager method doDynamicMethods.

public void doDynamicMethods() {
    checkInitialised();
    Class<?>[] allClasses = application.getAllClasses();
    if (allClasses != null) {
        for (Class<?> c : allClasses) {
            ExpandoMetaClass emc = new ExpandoMetaClass(c, true, true);
            emc.initialize();
        }
        ApplicationContext ctx = applicationContext;
        for (GrailsPlugin plugin : pluginList) {
            if (!plugin.isEnabled(ctx.getEnvironment().getActiveProfiles()))
                continue;
            plugin.doWithDynamicMethods(ctx);
        }
    }
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) GrailsPlugin(grails.plugins.GrailsPlugin) ExpandoMetaClass(groovy.lang.ExpandoMetaClass) ExpandoMetaClass(groovy.lang.ExpandoMetaClass)

Example 3 with ExpandoMetaClass

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

the class AbstractGrailsPluginManager method informOfClassChange.

public void informOfClassChange(File file, @SuppressWarnings("rawtypes") Class cls) {
    if (file.getName().equals(CONFIG_FILE)) {
        ConfigSlurper configSlurper = getConfigSlurper(application);
        ConfigObject c;
        try {
            c = configSlurper.parse(file.toURI().toURL());
            application.getConfig().merge(c);
            final Map flat = c.flatten();
            application.getConfig().merge(flat);
            application.configChanged();
            informPluginsOfConfigChange();
        } catch (Exception e) {
            // ignore
            LOG.debug("Error in changing Config", e);
        }
    } else {
        if (cls != null) {
            MetaClassRegistry registry = GroovySystem.getMetaClassRegistry();
            registry.removeMetaClass(cls);
            ExpandoMetaClass newMc = new ExpandoMetaClass(cls, true, true);
            newMc.initialize();
            registry.setMetaClass(cls, newMc);
            Enhanced en = AnnotationUtils.findAnnotation(cls, Enhanced.class);
            if (en != null) {
                Class<?>[] mixinClasses = en.mixins();
                if (mixinClasses != null) {
                    DefaultGroovyMethods.mixin(newMc, mixinClasses);
                }
            }
        }
        for (GrailsPlugin grailsPlugin : pluginList) {
            if (grailsPlugin.hasInterestInChange(file.getAbsolutePath())) {
                try {
                    if (cls == null) {
                        grailsPlugin.notifyOfEvent(GrailsPlugin.EVENT_ON_CHANGE, new FileSystemResource(file));
                    } else {
                        grailsPlugin.notifyOfEvent(GrailsPlugin.EVENT_ON_CHANGE, cls);
                    }
                    Environment.setCurrentReloadError(null);
                } catch (Exception e) {
                    LOG.error("Plugin " + grailsPlugin + " could not reload changes to file [" + file + "]: " + e.getMessage(), e);
                    Environment.setCurrentReloadError(e);
                }
            }
        }
    }
}
Also used : MetaClassRegistry(groovy.lang.MetaClassRegistry) GrailsPlugin(grails.plugins.GrailsPlugin) Enhanced(grails.artefact.Enhanced) ExpandoMetaClass(groovy.lang.ExpandoMetaClass) FileSystemResource(org.springframework.core.io.FileSystemResource) ConfigObject(groovy.util.ConfigObject) ExpandoMetaClass(groovy.lang.ExpandoMetaClass) HashMap(java.util.HashMap) Map(java.util.Map) NavigableMap(org.grails.config.NavigableMap) BeansException(org.springframework.beans.BeansException) PluginException(grails.plugins.exceptions.PluginException) ConfigSlurper(groovy.util.ConfigSlurper)

Example 4 with ExpandoMetaClass

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

the class GrailsMetaClassUtils method getExpandoMetaClass.

public static ExpandoMetaClass getExpandoMetaClass(Class<?> aClass) {
    MetaClassRegistry registry = getRegistry();
    MetaClass mc = registry.getMetaClass(aClass);
    if (mc instanceof ExpandoMetaClass) {
        ExpandoMetaClass emc = (ExpandoMetaClass) mc;
        // make permanent
        registry.setMetaClass(aClass, emc);
        return emc;
    }
    registry.removeMetaClass(aClass);
    mc = registry.getMetaClass(aClass);
    if (mc instanceof ExpandoMetaClass) {
        return (ExpandoMetaClass) mc;
    }
    ExpandoMetaClass emc = new ExpandoMetaClass(aClass, true, true);
    emc.initialize();
    registry.setMetaClass(aClass, emc);
    return emc;
}
Also used : MetaClassRegistry(groovy.lang.MetaClassRegistry) MetaClass(groovy.lang.MetaClass) ExpandoMetaClass(groovy.lang.ExpandoMetaClass) AdaptingMetaClass(groovy.lang.AdaptingMetaClass) DelegatingMetaClass(groovy.lang.DelegatingMetaClass) ExpandoMetaClass(groovy.lang.ExpandoMetaClass)

Example 5 with ExpandoMetaClass

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

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