Class MetaClassRegistryImpl

java.lang.Object
org.codehaus.groovy.runtime.metaclass.MetaClassRegistryImpl
All Implemented Interfaces:
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 Details

    • MODULE_META_INF_FILE

      @Deprecated public static final String MODULE_META_INF_FILE
      See Also:
    • EXTENSION_DISABLE_PROPERTY

      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.
      See Also:
    • LOAD_DEFAULT

      public static final int LOAD_DEFAULT
      Flag 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_DEFAULT
      Flag 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 the AccessibleObject.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 loading
      useAccessible - 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 definitions
      classLoader - the class loader to use for loading extension modules
      map - the map to store the loaded meta methods
    • getModuleRegistry

      public ExtensionModuleRegistry getModuleRegistry()
      Gets the extension module registry. The registry keeps track of all loaded extension modules.
      Returns:
      the extension module registry
    • getMetaClass

      public final MetaClass getMetaClass(Class theClass)
      Gets the MetaClass for the given class.
      Specified by:
      getMetaClass in interface MetaClassRegistry
      Parameters:
      theClass - the class to get the metaclass for
      Returns:
      the metaclass for the class
    • getMetaClass

      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
    • removeMetaClass

      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.
      Specified by:
      removeMetaClass in interface MetaClassRegistry
      Parameters:
      theClass - the class to remove the metaclass for
    • setMetaClass

      public void setMetaClass(Class theClass, MetaClass theMetaClass)
      Sets the metaclass for the given class, replacing any existing metaclass.
      Specified by:
      setMetaClass in interface MetaClassRegistry
      Parameters:
      theClass - the class to set the metaclass for
      theMetaClass - the new metaclass
    • setMetaClass

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

      public MetaClassRegistry.MetaClassCreationHandle getMetaClassCreationHandler()
      Gets a handle internally used to create MetaClass implementations WARNING: experimental code, likely to change soon
      Specified by:
      getMetaClassCreationHandler in interface MetaClassRegistry
      Returns:
      the handle
    • setMetaClassCreationHandle

      public void setMetaClassCreationHandle(MetaClassRegistry.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
      Specified by:
      setMetaClassCreationHandle in interface MetaClassRegistry
      Parameters:
      handle - the handle
    • addMetaClassRegistryChangeEventListener

      public void addMetaClassRegistryChangeEventListener(MetaClassRegistryChangeEventListener listener)
      Adds a listener for constant metaclasses.
      Specified by:
      addMetaClassRegistryChangeEventListener in interface MetaClassRegistry
      Parameters:
      listener - the listener
    • addNonRemovableMetaClassRegistryChangeEventListener

      public void addNonRemovableMetaClassRegistryChangeEventListener(MetaClassRegistryChangeEventListener listener)
      Adds a listener for constant metaclasses. This listener cannot be removed!
      Specified by:
      addNonRemovableMetaClassRegistryChangeEventListener in interface MetaClassRegistry
      Parameters:
      listener - the listener
    • removeMetaClassRegistryChangeEventListener

      public void removeMetaClassRegistryChangeEventListener(MetaClassRegistryChangeEventListener listener)
      Removes a constant metaclass listener.
      Specified by:
      removeMetaClassRegistryChangeEventListener in interface MetaClassRegistry
      Parameters:
      listener - the listener
    • fireConstantMetaClassUpdate

      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
    • getMetaClassRegistryChangeEventListeners

      public MetaClassRegistryChangeEventListener[] getMetaClassRegistryChangeEventListeners()
      Gets an array of all registered ConstantMetaClassListener instances.
      Specified by:
      getMetaClassRegistryChangeEventListeners in interface MetaClassRegistry
      Returns:
      an array containing all change listener
    • getInstance

      public static MetaClassRegistry getInstance(int includeExtension)
      Singleton of MetaClassRegistry.
      Parameters:
      includeExtension -
      Returns:
      the registry
    • getInstanceMethods

      public FastArray getInstanceMethods()
      Gets the instance methods registered in this registry.
      Returns:
      a FastArray of all instance methods
    • getStaticMethods

      public FastArray getStaticMethods()
      Gets the static methods registered in this registry.
      Returns:
      a FastArray of all static methods
    • iterator

      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.
      Specified by:
      iterator in interface MetaClassRegistry
      Returns:
      the iterator.