groovy.lang.ExpandoMetaClassCreationHandle Maven / Gradle / Ivy
Show all versions of spotless-ext-greclipse Show documentation
/*
* Copyright 2003-2007 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package groovy.lang;
import groovy.lang.MetaClassRegistry.MetaClassCreationHandle;
import org.codehaus.groovy.reflection.ClassInfo;
/**
* A handle for the MetaClassRegistry that changes all classes loaded into the Grails VM
* to use ExpandoMetaClass instances
*
*
The handle should be registered with the Groovy runtime before Groovy loads, for example
* in your main method.
*
* GroovySystem.metaClassRegistry.metaClassCreationHandle = new ExpandoMetaClassCreationHandle()
*
* @see groovy.lang.MetaClassRegistry
* @see groovy.lang.MetaClassRegistry.MetaClassCreationHandle
* @see org.codehaus.groovy.runtime.metaclass.MetaClassRegistryImpl#setMetaClassCreationHandle(groovy.lang.MetaClassRegistry.MetaClassCreationHandle)
*
* @author Graeme Rocher
* @since 1.5
*/
public class ExpandoMetaClassCreationHandle extends MetaClassCreationHandle {
public static final ExpandoMetaClassCreationHandle instance = new ExpandoMetaClassCreationHandle();
/* (non-Javadoc)
* @see groovy.lang.MetaClassRegistry.MetaClassCreationHandle#create(java.lang.Class, groovy.lang.MetaClassRegistry)
*/
protected MetaClass createNormalMetaClass(Class theClass, MetaClassRegistry registry) {
if(theClass != ExpandoMetaClass.class) {
return new ExpandoMetaClass(theClass, true, true);
}
else {
return super.createNormalMetaClass(theClass, registry);
}
}
/**
* Registers a modified ExpandoMetaClass with the creation handle
*
* @param emc The EMC
*/
public void registerModifiedMetaClass(ExpandoMetaClass emc) {
final Class klazz = emc.getJavaClass();
GroovySystem.getMetaClassRegistry().setMetaClass(klazz,emc);
}
public boolean hasModifiedMetaClass(ExpandoMetaClass emc) {
return emc.getClassInfo().getModifiedExpando() != null;
}
/**
*
Enables the ExpandoMetaClassCreationHandle with the registry
*
* ExpandoMetaClassCreationHandle.enable();
*
*/
public static void enable() {
final MetaClassRegistry metaClassRegistry = GroovySystem.getMetaClassRegistry();
synchronized (metaClassRegistry) {
if (metaClassRegistry.getMetaClassCreationHandler() != instance) {
ClassInfo.clearModifiedExpandos();
metaClassRegistry.setMetaClassCreationHandle(instance);
}
}
}
public static void disable() {
final MetaClassRegistry metaClassRegistry = GroovySystem.getMetaClassRegistry();
synchronized (metaClassRegistry) {
if (metaClassRegistry.getMetaClassCreationHandler() == instance) {
ClassInfo.clearModifiedExpandos();
metaClassRegistry.setMetaClassCreationHandle(new MetaClassCreationHandle());
}
}
}
}