Package: org.codehaus.groovy.runtime.metaclass

## [Java] Class ClosureMetaClass

- org.codehaus.groovy.runtime.metaclass.ClosureMetaClass

-     public final class ClosureMetaClass
      extends MetaClassImpl

  A metaclass for closures generated by the Groovy compiler. These classes have special characteristics this MetaClass uses. One of these is that a generated Closure has only additional doCall methods, all other methods are in the Closure class as well. To use this fact this MetaClass uses a MetaClass for Closure as static field And delegates calls to this MetaClass if needed. This allows a lean implementation for this MetaClass. Multiple generated closures will then use the same MetaClass for Closure. For static dispatching this class uses the MetaClass of Class, again all instances of this class will share that MetaClass. The Class MetaClass is initialized lazy, because most operations do not need this MetaClass.

  The Closure and Class metaclasses are not replaceable.

  This MetaClass is for internal usage only!

  Since:
    1.5

- - Inherited fields
    Fields inherited from class | Fields
    --- | ---
    `class MetaClassImpl` | `EMPTY_ARGUMENTS, INVOKE_METHOD_METHOD, METHOD_MISSING, PROPERTY_MISSING, STATIC_METHOD_MISSING, STATIC_PROPERTY_MISSING, getPropertyMethod, invokeMethodMethod, isGroovyObject, isMap, metaMethodIndex, registry, setPropertyMethod, theCachedClass, theClass`

  - ### Constructor Summary

    Constructors
    Constructor and description
    `ClosureMetaClass(MetaClassRegistry registry, Class theClass)` Constructs a new ClosureMetaClass for the given closure class.

  - ### Methods Summary

    Methods
    Type Params | Return Type | Name and description
    --- | --- | ---
    `` | `public void` | `addMetaBeanProperty(MetaBeanProperty mp)` Adds a meta bean property to this metaclass.
    `` | `public void` | `addMetaMethod(MetaMethod method)` Adds a meta method to this metaclass.
    `` | `public void` | `addNewInstanceMethod(Method method)` Adds a new instance method to this metaclass.
    `` | `public void` | `addNewStaticMethod(Method method)` Adds a new static method to this metaclass.
    `` | `protected void` | `applyPropertyDescriptors(PropertyDescriptor[] propertyDescriptors)`
    `` | `public Object` | `getAttribute(Class sender, Object object, String attribute, boolean useSuper, boolean fromInsideClass)` Gets an attribute from the closure or the object it represents.
    `` | `public List<MetaMethod>` | `getMetaMethods()` Returns a list of all metamethods in the Closure metaclass.
    `` | `public MetaProperty` | `getMetaProperty(String name)` Gets the meta property for the given name from the closure metaclass.
    `` | `public MetaMethod` | `getMethodWithoutCaching(int index, Class sender, String methodName, Class[] arguments, boolean isCallToSuper)` Gets a method without using the cache.
    `` | `public List<MetaMethod>` | `getMethods()` Returns a list of all methods available to closures, including both closure-specific doCall methods and inherited Closure class methods.
    `` | `public List<MetaProperty>` | `getProperties()` Returns a list of all properties available to closures.
    `` | `public Object` | `getProperty(Class sender, Object object, String name, boolean useSuper, boolean fromInsideClass)` Gets a property on the closure or the object it represents.
    `` | `public MetaMethod` | `getStaticMetaMethod(String name, Object[] args)` Gets a static meta method by name and argument objects.
    `` | `public MetaMethod` | `getStaticMetaMethod(String name, Class[] argTypes)` Gets a static meta method by name and argument types.
    `` | `public void` | `initialize()` Initializes the closure metaclass by discovering and indexing all closure-related methods.
    `` | `public Object` | `invokeMethod(Class sender, Object object, String methodName, Object[] arguments, boolean isCallToSuper, boolean fromInsideClass)`
    `` | `public Object` | `invokeStaticMethod(Object object, String methodName, Object[] arguments)` Invokes a static method on the closure class itself.
    `` | `public MetaMethod` | `pickMethod(String methodName, Class[] argumentTypes)` Picks the most appropriate method from the available closure methods that matches the given method name and argument types.
    `` | `public static void` | `resetCachedMetaClasses()` Resets the cached metaclasses for Closure and Class.
    `` | `public List` | `respondsTo(Object obj, String name, Object[] argTypes)` Checks if this metaclass responds to the given method name with the specified argument types.
    `` | `public List` | `respondsTo(Object obj, String name)` Checks if this metaclass responds to the given method name.
    `` | `public Constructor` | `retrieveConstructor(Class[] arguments)` Retrieves a constructor matching the given argument types.
    `` | `public MetaMethod` | `retrieveStaticMethod(String methodName, Class[] arguments)` Retrieves a static method with the given name and argument types.
    `` | `public void` | `setAttribute(Class sender, Object object, String attribute, Object newValue, boolean useSuper, boolean fromInsideClass)` Sets an attribute on the closure or the object it represents.
    `` | `public void` | `setProperties(Object bean, Map map)` Sets multiple properties on a bean.
    `` | `public void` | `setProperty(Class sender, Object object, String name, Object newValue, boolean useSuper, boolean fromInsideClass)` Sets a property on the closure or the object it represents.

  - ### Inherited Methods Summary

    Inherited Methods
    Methods inherited from class | Name
    --- | ---
    `class MetaClassImpl` | `addMetaBeanProperty, addMetaMethod, addMetaMethodToIndex, addNewInstanceMethod, addNewStaticMethod, applyPropertyDescriptors, checkIfGroovyObjectMethod, checkInitalised, chooseConstructor, chooseMethod, chooseNamedArgCompatibleConstructor, clearInvocationCaches, createErrorMessageForAmbiguity, createTransformMetaMethod, doChooseMostSpecificParams, dropMethodCache, dropStaticMethodCache, findMethodInClassHierarchy, findMixinMethod, findOwnMethod, findPropertyInClassHierarchy, getAdditionalMetaMethods, getAttribute, getAttribute, getAttribute, getClassInfo, getClassNode, getEffectiveGetMetaProperty, getMetaMethod, getMetaMethods, getMetaProperty, getMethodWithCaching, getMethodWithCaching, getMethodWithoutCaching, getMethods, getNonClosureOuter, getProperties, getProperty, getProperty, getRegistry, getStaticMetaMethod, getSubclassMetaMethods, getSuperClasses, getTheCachedClass, getTheClass, getVersion, handleMatches, hasCustomInvokeMethod, hasCustomStaticInvokeMethod, hasProperty, incVersion, initialize, invokeConstructor, invokeMethod, invokeMethod, invokeMethod, invokeMissingMethod, invokeMissingProperty, invokeStaticMethod, invokeStaticMissingProperty, isGroovyObject, isInitialized, isModified, isPermissivePropertyAccess, onGetPropertyFoundInHierarchy, onInvokeMethodFoundInHierarchy, onMixinMethodFound, onSetPropertyFoundInHierarchy, onSuperMethodFoundInHierarchy, onSuperPropertyFoundInHierarchy, pickMethod, reinitialize, respondsTo, respondsTo, retrieveConstructor, retrieveConstructor, retrieveStaticMethod, selectConstructorAndTransformArguments, setAttribute, setAttribute, setInitialized, setPermissivePropertyAccess, setProperties, setProperty, setProperty, toString`

- - ### Constructor Detail

    - #### public ClosureMetaClass(MetaClassRegistry registry, Class theClass)

      Constructs a new ClosureMetaClass for the given closure class. This metaclass handles method invocation and property access for closure instances, providing special handling for closure-specific behavior.

      Parameters:
        `registry` - the metaclass registry
        `theClass` - the closure class this metaclass represents

  - ### Method Detail

    - #### public void addMetaBeanProperty(MetaBeanProperty mp)

      Adds a meta bean property to this metaclass. This operation is not supported for ClosureMetaClass as it is immutable.

      throws:
        UnsupportedOperationException this method is not supported for closures
      Parameters:
        `mp` - the meta bean property to add

    - #### public void addMetaMethod(MetaMethod method)

      Adds a meta method to this metaclass. This operation is not supported for ClosureMetaClass as it is immutable.

      throws:
        UnsupportedOperationException this method is not supported for closures
      Parameters:
        `method` - the meta method to add

    - #### public void addNewInstanceMethod(Method method)

      Adds a new instance method to this metaclass. This operation is not supported for ClosureMetaClass as it is immutable.

      throws:
        UnsupportedOperationException this method is not supported for closures
      Parameters:
        `method` - the method to add

    - #### public void addNewStaticMethod(Method method)

      Adds a new static method to this metaclass. This operation is not supported for ClosureMetaClass as it is immutable.

      throws:
        UnsupportedOperationException this method is not supported for closures
      Parameters:
        `method` - the static method to add

    - #### protected void applyPropertyDescriptors(PropertyDescriptor[] propertyDescriptors)

    - #### public Object getAttribute(Class sender, Object object, String attribute, boolean useSuper, boolean fromInsideClass)

      Gets an attribute from the closure or the object it represents. For Class objects, delegates to the static metaclass. For closures, checks the cached fields first before delegating to the Closure metaclass.

      Parameters:
        `sender` - the class that is sending this request
        `object` - the object to get the attribute from
        `attribute` - the name of the attribute
        `useSuper` - whether to use super resolution
        `fromInsideClass` - whether the call is from inside the class
      Returns:
        the attribute value

    - #### public List<MetaMethod> getMetaMethods()

      Returns a list of all metamethods in the Closure metaclass.

      Returns:
        a list of all metamethods

    - #### public MetaProperty getMetaProperty(String name)

      Gets the meta property for the given name from the closure metaclass.

      Parameters:
        `name` - the property name
      Returns:
        the meta property or null if not found

    - #### public MetaMethod getMethodWithoutCaching(int index, Class sender, String methodName, Class[] arguments, boolean isCallToSuper)

      Gets a method without using the cache. This operation is not supported for ClosureMetaClass.

      throws:
        UnsupportedOperationException this method is not supported for closures
      Parameters:
        `index` - the method index
        `sender` - the class that is sending this request
        `methodName` - the name of the method
        `arguments` - the argument types
        `isCallToSuper` - whether this is a call to a super method
      Returns:
        never returns; always throws UnsupportedOperationException

    - #### public List<MetaMethod> getMethods()

      Returns a list of all methods available to closures, including both closure-specific doCall methods and inherited Closure class methods.

      Returns:
        a list of all available methods

    - #### public List<MetaProperty> getProperties()

      Returns a list of all properties available to closures.

      Returns:
        a list of all available properties

    - #### public Object getProperty(Class sender, Object object, String name, boolean useSuper, boolean fromInsideClass)

      Gets a property on the closure or the object it represents. For Class objects, delegates to the static metaclass; otherwise delegates to Closure metaclass.

      Parameters:
        `sender` - the class that is sending this request
        `object` - the object to get the property from
        `name` - the name of the property
        `useSuper` - whether to use super resolution
        `fromInsideClass` - whether the call is from inside the class
      Returns:
        the property value

    - #### public MetaMethod getStaticMetaMethod(String name, Object[] args)

      Gets a static meta method by name and argument objects. Delegates to the Closure metaclass.

      Parameters:
        `name` - the name of the static method
        `args` - the arguments
      Returns:
        the matching static meta method, or null if not found

    - #### public MetaMethod getStaticMetaMethod(String name, Class[] argTypes)

      Gets a static meta method by name and argument types. Delegates to the Closure metaclass.

      Parameters:
        `name` - the name of the static method
        `argTypes` - the argument types
      Returns:
        the matching static meta method, or null if not found

    - #### public void initialize()

      Initializes the closure metaclass by discovering and indexing all closure-related methods.

    - #### public Object invokeMethod(Class sender, Object object, String methodName, Object[] arguments, boolean isCallToSuper, boolean fromInsideClass)

    - #### public Object invokeStaticMethod(Object object, String methodName, Object[] arguments)

      Invokes a static method on the closure class itself. Delegates to the static metaclass.

      Parameters:
        `object` - the object on which to invoke the static method
        `methodName` - the name of the static method
        `arguments` - the arguments to pass to the method
      Returns:
        the result of the method invocation

    - #### public MetaMethod pickMethod(String methodName, Class[] argumentTypes)

      Picks the most appropriate method from the available closure methods that matches the given method name and argument types.

      Parameters:
        `methodName` - the name of the method to pick (typically "call" or "doCall" for closures)
        `argumentTypes` - the types of arguments to match against method signatures
      Returns:
        the matching method, or null if no match is found

    - #### public static void resetCachedMetaClasses()

      Resets the cached metaclasses for Closure and Class. This method is used to reinitialize the metaclass caches when the system configuration changes, ensuring that any cached metaclass information is refreshed.

    - #### public List respondsTo(Object obj, String name, Object[] argTypes)

      Checks if this metaclass responds to the given method name with the specified argument types. Returns a list of methods that match the given name and signature.

      Parameters:
        `obj` - the object to check
        `name` - the method name
        `argTypes` - the argument types
      Returns:
        a list of matching methods, or an empty list if none match

    - #### public List respondsTo(Object obj, String name)

      Checks if this metaclass responds to the given method name. Returns a list of methods that match the given name.

      Parameters:
        `obj` - the object to check
        `name` - the method name
      Returns:
        a list of matching methods, or an empty list if none match

    - #### public Constructor retrieveConstructor(Class[] arguments)

      Retrieves a constructor matching the given argument types. This operation is not supported for ClosureMetaClass.

      throws:
        UnsupportedOperationException this method is not supported for closures
      Parameters:
        `arguments` - the argument types
      Returns:
        never returns; always throws UnsupportedOperationException

    - #### public MetaMethod retrieveStaticMethod(String methodName, Class[] arguments)

      Retrieves a static method with the given name and argument types. Closures do not support static methods, so this always returns null.

      Parameters:
        `methodName` - the name of the static method to retrieve
        `arguments` - the argument types
      Returns:
        null, as closures do not have static methods

    - #### public void setAttribute(Class sender, Object object, String attribute, Object newValue, boolean useSuper, boolean fromInsideClass)

      Sets an attribute on the closure or the object it represents. For Class objects, delegates to the static metaclass. For closures, checks the cached fields first before delegating to the Closure metaclass.

      Parameters:
        `sender` - the class that is sending this request
        `object` - the object on which to set the attribute
        `attribute` - the name of the attribute
        `newValue` - the new value for the attribute
        `useSuper` - whether to use super resolution
        `fromInsideClass` - whether the call is from inside the class

    - #### public void setProperties(Object bean, Map map)

      Sets multiple properties on a bean. This operation is not supported for ClosureMetaClass.

      throws:
        UnsupportedOperationException this method is not supported for closures
      Parameters:
        `bean` - the bean object
        `map` - the properties to set

    - #### public void setProperty(Class sender, Object object, String name, Object newValue, boolean useSuper, boolean fromInsideClass)

      Sets a property on the closure or the object it represents. For Class objects, delegates to the static metaclass; otherwise delegates to Closure metaclass.

      Parameters:
        `sender` - the class that is sending this request
        `object` - the object on which to set the property
        `name` - the name of the property
        `newValue` - the new value for the property
        `useSuper` - whether to use super resolution
        `fromInsideClass` - whether the call is from inside the class