Package: org.codehaus.groovy.runtime.metaclass

## [Java] Class MetaClassRegistryImpl

- org.codehaus.groovy.runtime.metaclass.MetaClassRegistryImpl

- All Implemented Interfaces and Traits:
    MetaClassRegistry

      public class MetaClassRegistryImpl
      extends Object
      implements MetaClassRegistry

  A registry of MetaClass instances which caches introspection and reflection information and allows methods to be dynamically added to existing classes at runtime

- - ### Field Summary

    Fields
    Modifiers | Name | Description
    --- | --- | ---
    `static int` | `DONT_LOAD_DEFAULT` | Flag to skip loading default metaclasses during registry initialization.
    `static String` | `EXTENSION_DISABLE_PROPERTY` | System property name used to disable specific extension modules.
    `static int` | `LOAD_DEFAULT` | Flag to load default metaclasses for standard Java and Groovy types when initializing the registry.
    `static String` | `MODULE_META_INF_FILE` | deprecated: Use ExtensionModuleScanner.MODULE_META_INF_FILE instead

  - ### Constructor Summary

    Constructors
    Constructor and description
    `MetaClassRegistryImpl()` Creates a new MetaClassRegistry with default settings.
    `MetaClassRegistryImpl(int loadDefault)` Creates a new MetaClassRegistry with optional default metaclass loading.
    `MetaClassRegistryImpl(boolean useAccessible)` Parameters: `useAccessible` - defines whether the AccessibleObject.setAccessible method will be called to enable access to all methods when using reflection
    `MetaClassRegistryImpl(int loadDefault, boolean useAccessible)` Creates a new MetaClassRegistry with customizable settings.

  - ### Methods Summary

    Methods
    Type Params | Return Type | Name and description
    --- | --- | ---
    `` | `public void` | `addMetaClassRegistryChangeEventListener(MetaClassRegistryChangeEventListener listener)` Adds a listener for constant metaclasses.
    `` | `public void` | `addNonRemovableMetaClassRegistryChangeEventListener(MetaClassRegistryChangeEventListener listener)` Adds a listener for constant metaclasses.
    `` | `protected void` | `fireConstantMetaClassUpdate(Object obj, Class c, MetaClass oldMC, MetaClass newMc)` Causes the execution of all registered listeners.
    `` | `public static MetaClassRegistry` | `getInstance(int includeExtension)` Singleton of MetaClassRegistry.
    `` | `public FastArray` | `getInstanceMethods()` Gets the instance methods registered in this registry.
    `` | `public final MetaClass` | `getMetaClass(Class theClass)` Gets the MetaClass for the given class.
    `` | `public MetaClass` | `getMetaClass(Object obj)` Gets the metaclass for the given object.
    `` | `public MetaClassCreationHandle` | `getMetaClassCreationHandler()` Gets a handle internally used to create MetaClass implementations WARNING: experimental code, likely to change soon
    `` | `public MetaClassRegistryChangeEventListener[]` | `getMetaClassRegistryChangeEventListeners()` Gets an array of all registered ConstantMetaClassListener instances.
    `` | `public ExtensionModuleRegistry` | `getModuleRegistry()` Gets the extension module registry.
    `` | `public FastArray` | `getStaticMethods()` Gets the static methods registered in this registry.
    `` | `public Iterator` | `iterator()` Returns an iterator to iterate over all constant metaclasses.
    `` | `public void` | `registerExtensionModuleFromProperties(Properties properties, ClassLoader classLoader, Map<CachedClass, List<MetaMethod>> map)` Registers extension module methods from the given properties.
    `` | `public void` | `removeMetaClass(Class theClass)` Removes the metaclass for the given class, resetting it to null.
    `` | `public void` | `removeMetaClassRegistryChangeEventListener(MetaClassRegistryChangeEventListener listener)` Removes a constant metaclass listener.
    `` | `public void` | `setMetaClass(Class theClass, MetaClass theMetaClass)` Sets the metaclass for the given class, replacing any existing metaclass.
    `` | `public void` | `setMetaClass(Object obj, MetaClass theMetaClass)` Sets the per-instance metaclass for the given object.
    `` | `public void` | `setMetaClassCreationHandle(MetaClassCreationHandle handle)` Sets a handle internally used to create MetaClass implementations.
    `` | `public boolean` | `useAccessible()` Returns whether reflection access modifiers are being set.

  - ### Inherited Methods Summary

    Inherited Methods
    Methods inherited from class | Name
    --- | ---
    `class Object` | `equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### public static final int DONT_LOAD_DEFAULT

      Flag to skip loading default metaclasses during registry initialization.

    - #### public static final String 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.

    - #### public static final int LOAD_DEFAULT

      Flag to load default metaclasses for standard Java and Groovy types when initializing the registry.

    - #### @Deprecated
      public static final String MODULE_META_INF_FILE

      deprecated:
        Use ExtensionModuleScanner.MODULE_META_INF_FILE instead

  - ### Constructor Detail

    - #### public MetaClassRegistryImpl()

      Creates a new MetaClassRegistry with default settings. Loads default metaclasses for standard Java and Groovy types.

    - #### 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

    - #### public MetaClassRegistryImpl(boolean useAccessible)

      Parameters:
        `useAccessible` - defines whether the AccessibleObject.setAccessible method will be called to enable access to all methods when using reflection

    - #### 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 loading
        `useAccessible` - whether to use AccessibleObject.setAccessible() for reflection access

  - ### Method Detail

    - #### public void addMetaClassRegistryChangeEventListener(MetaClassRegistryChangeEventListener listener)

      Adds a listener for constant metaclasses.

      Parameters:
        `listener` - the listener

    - #### public void addNonRemovableMetaClassRegistryChangeEventListener(MetaClassRegistryChangeEventListener listener)

      Adds a listener for constant metaclasses. This listener cannot be removed!

      Parameters:
        `listener` - the listener

    - #### protected void fireConstantMetaClassUpdate(Object obj, Class c, MetaClass oldMC, MetaClass newMc)

      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 class
        `oldMC` - the old MetaClass
        `newMc` - the new MetaClass

    - #### public static MetaClassRegistry getInstance(int includeExtension)

      Singleton of MetaClassRegistry.

      Parameters:
        includeExtension
      Returns:
        the registry

    - #### public FastArray getInstanceMethods()

      Gets the instance methods registered in this registry.

      Returns:
        a FastArray of all instance methods

    - #### public final MetaClass getMetaClass(Class theClass)

      Gets the MetaClass for the given class.

      Parameters:
        `theClass` - the class to get the metaclass for
      Returns:
        the metaclass for the class

    - #### public MetaClass getMetaClass(Object obj)

      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

    - #### public MetaClassCreationHandle getMetaClassCreationHandler()

      Gets a handle internally used to create MetaClass implementations WARNING: experimental code, likely to change soon

      Returns:
        the handle

    - #### public MetaClassRegistryChangeEventListener[] getMetaClassRegistryChangeEventListeners()

      Gets an array of all registered ConstantMetaClassListener instances.

    - #### public ExtensionModuleRegistry getModuleRegistry()

      Gets the extension module registry. The registry keeps track of all loaded extension modules.

      Returns:
        the extension module registry

    - #### public FastArray getStaticMethods()

      Gets the static methods registered in this registry.

      Returns:
        a FastArray of all static methods

    - #### public Iterator 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.

      Returns:
        the iterator.

    - #### 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 definitions
        `classLoader` - the class loader to use for loading extension modules
        `map` - the map to store the loaded meta methods

    - #### public void removeMetaClass(Class theClass)

      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.

      Parameters:
        `theClass` - the class to remove the metaclass for

    - #### public void removeMetaClassRegistryChangeEventListener(MetaClassRegistryChangeEventListener listener)

      Removes a constant metaclass listener.

      Parameters:
        `listener` - the listener

    - #### public void setMetaClass(Class theClass, MetaClass theMetaClass)

      Sets the metaclass for the given class, replacing any existing metaclass.

      Parameters:
        `theClass` - the class to set the metaclass for
        `theMetaClass` - the new metaclass

    - #### public void setMetaClass(Object obj, MetaClass theMetaClass)

      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 for
        `theMetaClass` - the new metaclass for this instance

    - #### public void setMetaClassCreationHandle(MetaClassCreationHandle handle)

      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

      Parameters:
        `handle` - the handle

    - #### 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