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;
}
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);
}
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));
}
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);
}
}
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;
}
Aggregations