private MetaClass getMetaClassUnderLock() { MetaClass answer = getStrongMetaClass(); if (answer!=null) return answer; answer = getWeakMetaClass(); final MetaClassRegistry metaClassRegistry = GroovySystem.getMetaClassRegistry(); MetaClassRegistry.MetaClassCreationHandle mccHandle = metaClassRegistry.getMetaClassCreationHandler(); if (isValidWeakMetaClass(answer, mccHandle)) { return answer; } answer = mccHandle.create(classRef.get(), metaClassRegistry); answer.initialize(); if (GroovySystem.isKeepJavaMetaClasses()) { setStrongMetaClass(answer); } else { setWeakMetaClass(answer); } return answer; }
/** * Sets the metaclass for a given class. * * @param self the class whose metaclass we wish to set * @param metaClass the new MetaClass * @since 1.6.0 */ public static void setMetaClass(Class self, MetaClass metaClass) { final MetaClassRegistry metaClassRegistry = GroovySystem.getMetaClassRegistry(); if (metaClass == null) metaClassRegistry.removeMetaClass(self); else { if (metaClass instanceof HandleMetaClass) { metaClassRegistry.setMetaClass(self, ((HandleMetaClass)metaClass).getAdaptee()); } else { metaClassRegistry.setMetaClass(self, metaClass); } if (self==NullObject.class) { NullObject.getNullObject().setMetaClass(metaClass); } } }
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; }
/** * if EMC.enableGlobally() is OFF, return whatever the cached answer is. * but if EMC.enableGlobally() is ON and the cached answer is not an EMC, come up with a fresh answer */ private static boolean isValidWeakMetaClass(MetaClass metaClass, MetaClassRegistry.MetaClassCreationHandle mccHandle) { if(metaClass==null) return false; boolean enableGloballyOn = (mccHandle instanceof ExpandoMetaClassCreationHandle); boolean cachedAnswerIsEMC = (metaClass instanceof ExpandoMetaClass); return (!enableGloballyOn || cachedAnswerIsEMC); }
/** * Adds a "metaClass" property to all class objects so you can use the syntax * <code>String.metaClass.myMethod = { println "foo" }</code> * * @param c The java.lang.Class instance * @return An MetaClass instance * @since 1.5.0 */ public static MetaClass getMetaClass(Class c) { MetaClassRegistry metaClassRegistry = GroovySystem.getMetaClassRegistry(); MetaClass mc = metaClassRegistry.getMetaClass(c); if (mc instanceof ExpandoMetaClass || mc instanceof DelegatingMetaClass && ((DelegatingMetaClass) mc).getAdaptee() instanceof ExpandoMetaClass) return mc; else { return new HandleMetaClass(mc); } }
/** * Singleton of MetaClassRegistry. * * @param includeExtension * @return the registry */ public static synchronized MetaClassRegistry getInstance(int includeExtension) { if (includeExtension != DONT_LOAD_DEFAULT) { if (instanceInclude == null) { instanceInclude = new MetaClassRegistryImpl(); } return instanceInclude; } else { if (instanceExclude == null) { instanceExclude = new MetaClassRegistryImpl(DONT_LOAD_DEFAULT); } return instanceExclude; } }
private void addAssociationToTarget(String name, Object target, Object obj) { if (obj == null) { return; } MetaClassRegistry reg = GroovySystem.getMetaClassRegistry(); MetaClass mc = reg.getMetaClass(target.getClass()); final String addMethodName = "addTo" + GrailsNameUtils.getClassNameRepresentation(name); mc.invokeMethod(target, addMethodName, obj); }
/** * @param adaptee the MetaClass to decorate with interceptability */ public MockProxyMetaClass(MetaClassRegistry registry, Class theClass, MetaClass adaptee) { this(registry, theClass, adaptee, false); }
/** * @param adaptee the MetaClass to decorate with interceptability */ public MockProxyMetaClass(MetaClassRegistry registry, Class theClass, MetaClass adaptee, boolean interceptConstruction) { super(registry, theClass, adaptee); this.interceptConstruction = interceptConstruction; }
/** * convenience factory method allowing interceptConstruction to be set. */ public static MockProxyMetaClass make(Class theClass, boolean interceptConstruction) { MetaClassRegistry metaRegistry = GroovySystem.getMetaClassRegistry(); MetaClass meta = metaRegistry.getMetaClass(theClass); return new MockProxyMetaClass(metaRegistry, theClass, meta, interceptConstruction); }
public ClosureMetaClass(MetaClassRegistry registry, Class theClass) { super(registry, theClass); }
public static MetaClassRegistry getMetaRegistry() { return metaRegistry; }
@Override protected synchronized MetaClass createNormalMetaClass(Class theClass, MetaClassRegistry registry) { VertxExtensionMethodBoostrap.install(registry); return super.createNormalMetaClass(theClass, registry); }