Class MetaClassRegistryImpl
java.lang.Object
org.codehaus.groovy.runtime.metaclass.MetaClassRegistryImpl
- All Implemented Interfaces:
MetaClassRegistry
A registry of MetaClass instances which caches introspection and
reflection information and allows methods to be dynamically added to
existing classes at runtime
-
Nested Class Summary
Nested classes/interfaces inherited from interface groovy.lang.MetaClassRegistry
MetaClassRegistry.MetaClassCreationHandle -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intFlag to skip loading default metaclasses during registry initialization.static final StringSystem property name used to disable specific extension modules.static final intFlag to load default metaclasses for standard Java and Groovy types when initializing the registry.static final StringDeprecated. -
Constructor Summary
ConstructorsConstructorDescriptionCreates a new MetaClassRegistry with default settings.MetaClassRegistryImpl(boolean useAccessible) MetaClassRegistryImpl(int loadDefault) Creates a new MetaClassRegistry with optional default metaclass loading.MetaClassRegistryImpl(int loadDefault, boolean useAccessible) Creates a new MetaClassRegistry with customizable settings. -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds a listener for constant metaclasses.voidAdds a listener for constant metaclasses.protected voidfireConstantMetaClassUpdate(Object obj, Class c, MetaClass oldMC, MetaClass newMc) Causes the execution of all registered listeners.static MetaClassRegistrygetInstance(int includeExtension) Singleton of MetaClassRegistry.Gets the instance methods registered in this registry.final MetaClassgetMetaClass(Class theClass) Gets the MetaClass for the given class.getMetaClass(Object obj) Gets the metaclass for the given object.Gets a handle internally used to create MetaClass implementations WARNING: experimental code, likely to change soonGets an array of all registered ConstantMetaClassListener instances.Gets the extension module registry.Gets the static methods registered in this registry.iterator()Returns an iterator to iterate over all constant metaclasses.voidregisterExtensionModuleFromProperties(Properties properties, ClassLoader classLoader, Map<CachedClass, List<MetaMethod>> map) Registers extension module methods from the given properties.voidremoveMetaClass(Class theClass) Removes the metaclass for the given class, resetting it to null.voidRemoves a constant metaclass listener.voidsetMetaClass(Class theClass, MetaClass theMetaClass) Sets the metaclass for the given class, replacing any existing metaclass.voidsetMetaClass(Object obj, MetaClass theMetaClass) Sets the per-instance metaclass for the given object.voidSets a handle internally used to create MetaClass implementations.booleanReturns whether reflection access modifiers are being set.
-
Field Details
-
MODULE_META_INF_FILE
Deprecated.UseExtensionModuleScanner.MODULE_META_INF_FILEinstead- See Also:
-
EXTENSION_DISABLE_PROPERTY
System property name used to disable specific extension modules. When set, the value should be a comma-separated list of extension module names to disable.- See Also:
-
LOAD_DEFAULT
public static final int LOAD_DEFAULTFlag to load default metaclasses for standard Java and Groovy types when initializing the registry.- See Also:
-
DONT_LOAD_DEFAULT
public static final int DONT_LOAD_DEFAULTFlag to skip loading default metaclasses during registry initialization.- See Also:
-
-
Constructor Details
-
MetaClassRegistryImpl
public MetaClassRegistryImpl()Creates a new MetaClassRegistry with default settings. Loads default metaclasses for standard Java and Groovy types. -
MetaClassRegistryImpl
public MetaClassRegistryImpl(int loadDefault) Creates a new MetaClassRegistry with optional default metaclass loading.- Parameters:
loadDefault- either LOAD_DEFAULT to load default metaclasses or DONT_LOAD_DEFAULT to skip loading
-
MetaClassRegistryImpl
public MetaClassRegistryImpl(boolean useAccessible) - Parameters:
useAccessible- defines whether theAccessibleObject.setAccessible(boolean)method will be called to enable access to all methods when using reflection
-
MetaClassRegistryImpl
public MetaClassRegistryImpl(int loadDefault, boolean useAccessible) Creates a new MetaClassRegistry with customizable settings.- Parameters:
loadDefault- either LOAD_DEFAULT to load default metaclasses or DONT_LOAD_DEFAULT to skip loadinguseAccessible- whether to use AccessibleObject.setAccessible() for reflection access
-
-
Method Details
-
registerExtensionModuleFromProperties
public void registerExtensionModuleFromProperties(Properties properties, ClassLoader classLoader, Map<CachedClass, List<MetaMethod>> map) Registers extension module methods from the given properties. This method scans for extension modules defined in properties and registers their methods.- Parameters:
properties- the properties containing extension module definitionsclassLoader- the class loader to use for loading extension modulesmap- the map to store the loaded meta methods
-
getModuleRegistry
Gets the extension module registry. The registry keeps track of all loaded extension modules.- Returns:
- the extension module registry
-
getMetaClass
Gets the MetaClass for the given class.- Specified by:
getMetaClassin interfaceMetaClassRegistry- Parameters:
theClass- the class to get the metaclass for- Returns:
- the metaclass for the class
-
getMetaClass
Gets the metaclass for the given object. For Class objects, returns the metaclass for that class; for other objects, returns the instance-specific metaclass if set, otherwise the metaclass for the object's class.- Parameters:
obj- the object to get the metaclass for- Returns:
- the metaclass for the object
-
removeMetaClass
Removes the metaclass for the given class, resetting it to null. This forces a new metaclass to be created the next time one is needed.- Specified by:
removeMetaClassin interfaceMetaClassRegistry- Parameters:
theClass- the class to remove the metaclass for
-
setMetaClass
Sets the metaclass for the given class, replacing any existing metaclass.- Specified by:
setMetaClassin interfaceMetaClassRegistry- Parameters:
theClass- the class to set the metaclass fortheMetaClass- the new metaclass
-
setMetaClass
Sets the per-instance metaclass for the given object. This allows individual instances to have their own custom metaclass implementation.- Parameters:
obj- the object to set the metaclass fortheMetaClass- the new metaclass for this instance
-
useAccessible
public boolean useAccessible()Returns whether reflection access modifiers are being set. When true, the AccessibleObject.setAccessible(true) method will be called when using reflection to access methods and fields.- Returns:
- true if using accessible reflection, false otherwise
-
getMetaClassCreationHandler
Gets a handle internally used to create MetaClass implementations WARNING: experimental code, likely to change soon- Specified by:
getMetaClassCreationHandlerin interfaceMetaClassRegistry- Returns:
- the handle
-
setMetaClassCreationHandle
Sets a handle internally used to create MetaClass implementations. When replacing the handle with a custom version, you should reuse the old handle to keep custom logic and to use the default logic as fall back. WARNING: experimental code, likely to change soon- Specified by:
setMetaClassCreationHandlein interfaceMetaClassRegistry- Parameters:
handle- the handle
-
addMetaClassRegistryChangeEventListener
Adds a listener for constant metaclasses.- Specified by:
addMetaClassRegistryChangeEventListenerin interfaceMetaClassRegistry- Parameters:
listener- the listener
-
addNonRemovableMetaClassRegistryChangeEventListener
public void addNonRemovableMetaClassRegistryChangeEventListener(MetaClassRegistryChangeEventListener listener) Adds a listener for constant metaclasses. This listener cannot be removed!- Specified by:
addNonRemovableMetaClassRegistryChangeEventListenerin interfaceMetaClassRegistry- Parameters:
listener- the listener
-
removeMetaClassRegistryChangeEventListener
public void removeMetaClassRegistryChangeEventListener(MetaClassRegistryChangeEventListener listener) Removes a constant metaclass listener.- Specified by:
removeMetaClassRegistryChangeEventListenerin interfaceMetaClassRegistry- Parameters:
listener- the listener
-
fireConstantMetaClassUpdate
Causes the execution of all registered listeners. This method is used mostly internal to kick of the listener notification. It can also be used by subclasses to achieve the same.- Parameters:
obj- object instance if the MetaClass change is on a per-instance metaclass (or null if global)c- the classoldMC- the old MetaClassnewMc- the new MetaClass
-
getMetaClassRegistryChangeEventListeners
Gets an array of all registered ConstantMetaClassListener instances.- Specified by:
getMetaClassRegistryChangeEventListenersin interfaceMetaClassRegistry- Returns:
- an array containing all change listener
-
getInstance
Singleton of MetaClassRegistry.- Parameters:
includeExtension-- Returns:
- the registry
-
getInstanceMethods
Gets the instance methods registered in this registry.- Returns:
- a FastArray of all instance methods
-
getStaticMethods
Gets the static methods registered in this registry.- Returns:
- a FastArray of all static methods
-
iterator
Returns an iterator to iterate over all constant metaclasses. This iterator can be seen as making a snapshot of the current state of the registry. The snapshot will include all metaclasses that has been used unless they are already collected. Collected metaclasses will be skipped automatically, so you can expect that each element of the iteration is not null. Calling this method is thread safe, the usage of the iterator is not.- Specified by:
iteratorin interfaceMetaClassRegistry- Returns:
- the iterator.
-
ExtensionModuleScanner.MODULE_META_INF_FILEinstead