Search in sources :

Example 6 with MetaClassRegistry

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

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

the class ProxyUnwrappingMarshaller method unwrap.

private Object unwrap(Object o) {
    if (o == null)
        return o;
    final MetaClassRegistry registry = GroovySystem.getMetaClassRegistry();
    MetaClass mc = registry.getMetaClass(o.getClass());
    final Object hibernateLazyInitializer = mc.getProperty(o, HIBERNATE_LAZY_INITIALIZER_PROP);
    return registry.getMetaClass(hibernateLazyInitializer.getClass()).getProperty(hibernateLazyInitializer, IMPLEMENTATION_PROP);
}
Also used : MetaClassRegistry(groovy.lang.MetaClassRegistry) MetaClass(groovy.lang.MetaClass)

Example 8 with MetaClassRegistry

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

the class ProfilingGrailsPluginManager method doDynamicMethods.

@Override
public void doDynamicMethods() {
    long time = System.currentTimeMillis();
    System.out.println("doWithDynamicMethods started");
    checkInitialised();
    // remove common meta classes just to be sure
    MetaClassRegistry registry = GroovySystem.getMetaClassRegistry();
    for (Class<?> COMMON_CLASS : COMMON_CLASSES) {
        registry.removeMetaClass(COMMON_CLASS);
    }
    for (GrailsPlugin plugin : pluginList) {
        if (plugin.supportsCurrentScopeAndEnvironment()) {
            try {
                long pluginTime = System.currentTimeMillis();
                System.out.println("doWithDynamicMethods for plugin [" + plugin.getName() + "] started");
                plugin.doWithDynamicMethods(applicationContext);
                System.out.println("doWithDynamicMethods for plugin [" + plugin.getName() + "] took " + (System.currentTimeMillis() - pluginTime));
            } catch (Throwable t) {
                throw new GrailsConfigurationException("Error configuring dynamic methods for plugin " + plugin + ": " + t.getMessage(), t);
            }
        }
    }
    System.out.println("doWithDynamicMethods took " + (System.currentTimeMillis() - time));
}
Also used : MetaClassRegistry(groovy.lang.MetaClassRegistry) GrailsPlugin(grails.plugins.GrailsPlugin) GrailsConfigurationException(org.grails.core.exceptions.GrailsConfigurationException)

Example 9 with MetaClassRegistry

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

the class DefaultGrailsApplication method configureLoadedClasses.

/**
     * Configures the loaded classes within the GrailsApplication instance using the
     * registered ArtefactHandler instances.
     *
     * @param classes The classes to configure
     */
protected void configureLoadedClasses(Class<?>[] classes) {
    initArtefactHandlers();
    artefactInfo.clear();
    allArtefactClasses.clear();
    allArtefactClassesArray = null;
    allClasses = classes;
    // first load the domain classes
    log.debug("Going to inspect artefact classes.");
    MetaClassRegistry metaClassRegistry = GroovySystem.getMetaClassRegistry();
    for (final Class<?> theClass : classes) {
        log.debug("Inspecting [" + theClass.getName() + "]");
        // start fresh
        metaClassRegistry.removeMetaClass(theClass);
        if (allArtefactClasses.contains(theClass)) {
            continue;
        }
        // check what kind of artefact it is and add to corrent data structure
        for (ArtefactHandler artefactHandler : artefactHandlers) {
            if (artefactHandler.isArtefact(theClass)) {
                log.debug("Adding artefact " + theClass + " of kind " + artefactHandler.getType());
                GrailsClass gclass = addArtefact(artefactHandler.getType(), theClass);
                // Also maintain set of all artefacts (!= all classes loaded)
                allArtefactClasses.add(theClass);
                // Update per-artefact cache
                DefaultArtefactInfo info = getArtefactInfo(artefactHandler.getType(), true);
                info.addGrailsClass(gclass);
                break;
            }
        }
    }
    refreshArtefactGrailsClassCaches();
    allArtefactClassesArray = allArtefactClasses.toArray(new Class[allArtefactClasses.size()]);
    // Tell all artefact handlers to init now we've worked out which classes are which artefacts
    for (ArtefactHandler artefactHandler : artefactHandlers) {
        initializeArtefacts(artefactHandler);
    }
}
Also used : MetaClassRegistry(groovy.lang.MetaClassRegistry) DomainClassArtefactHandler(org.grails.core.artefact.DomainClassArtefactHandler) CachedClass(org.codehaus.groovy.reflection.CachedClass)

Example 10 with MetaClassRegistry

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

the class MetaClassRegistryCleaner method createAndRegister.

public static MetaClassRegistryCleaner createAndRegister() {
    MetaClassRegistry metaClassRegistry = GroovySystem.getMetaClassRegistry();
    MetaClassRegistryChangeEventListener[] listeners = metaClassRegistry.getMetaClassRegistryChangeEventListeners();
    boolean registered = false;
    for (MetaClassRegistryChangeEventListener listener : listeners) {
        if (listener == INSTANCE) {
            registered = true;
            break;
        }
    }
    if (!registered) {
        GroovySystem.getMetaClassRegistry().addMetaClassRegistryChangeEventListener(INSTANCE);
    }
    return INSTANCE;
}
Also used : MetaClassRegistry(groovy.lang.MetaClassRegistry) MetaClassRegistryChangeEventListener(groovy.lang.MetaClassRegistryChangeEventListener)

Aggregations

MetaClassRegistry (groovy.lang.MetaClassRegistry)10 ExpandoMetaClass (groovy.lang.ExpandoMetaClass)5 MetaClass (groovy.lang.MetaClass)5 AdaptingMetaClass (groovy.lang.AdaptingMetaClass)3 GrailsPlugin (grails.plugins.GrailsPlugin)2 DelegatingMetaClass (groovy.lang.DelegatingMetaClass)2 GrailsConfigurationException (org.grails.core.exceptions.GrailsConfigurationException)2 Enhanced (grails.artefact.Enhanced)1 PluginException (grails.plugins.exceptions.PluginException)1 Closure (groovy.lang.Closure)1 ClosureInvokingMethod (groovy.lang.ClosureInvokingMethod)1 ExpandoMetaClassCreationHandle (groovy.lang.ExpandoMetaClassCreationHandle)1 GroovyObject (groovy.lang.GroovyObject)1 MetaClassRegistryChangeEventListener (groovy.lang.MetaClassRegistryChangeEventListener)1 ConfigObject (groovy.util.ConfigObject)1 ConfigSlurper (groovy.util.ConfigSlurper)1 Constructor (java.lang.reflect.Constructor)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 CachedClass (org.codehaus.groovy.reflection.CachedClass)1