Package org.codehaus.groovy.runtime.metaclass

# Class ClosureMetaClass

java.lang.Object
groovy.lang.MetaClassImpl
org.codehaus.groovy.runtime.metaclass.ClosureMetaClass

All Implemented Interfaces:
  `MetaClass`, `MetaObjectProtocol`, `MutableMetaClass`

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

- ## Nested Class Summary

  ## Nested classes/interfaces inherited from class groovy.lang.MetaClassImpl

  `MetaClassImpl.MetaConstructor`

- ## Field Summary

  ### Fields inherited from class groovy.lang.MetaClassImpl

  `getPropertyMethod, INVOKE_METHOD_METHOD, invokeMethodMethod, isGroovyObject, isMap, metaMethodIndex, METHOD_MISSING, PROPERTY_MISSING, registry, setPropertyMethod, STATIC_METHOD_MISSING, STATIC_PROPERTY_MISSING, theCachedClass, theClass`

- ## Constructor Summary

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

- ## Method Summary

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

  ### Methods inherited from class groovy.lang.MetaClassImpl

  `addMetaMethodToIndex, checkIfGroovyObjectMethod, checkInitalised, chooseConstructor, chooseMethod, chooseNamedArgCompatibleConstructor, clearInvocationCaches, createErrorMessageForAmbiguity, createTransformMetaMethod, doChooseMostSpecificParams, dropMethodCache, dropStaticMethodCache, findMethodInClassHierarchy, findMixinMethod, findOwnMethod, findPropertyInClassHierarchy, getAdditionalMetaMethods, getAttribute, getAttribute, getClassInfo, getClassNode, getEffectiveGetMetaProperty, getMetaMethod, getMethodWithCaching, getMethodWithCaching, getMethodWithoutCaching, getNonClosureOuter, getProperty, getRegistry, getSubclassMetaMethods, getSuperClasses, getTheCachedClass, getTheClass, getVersion, handleMatches, hasCustomInvokeMethod, hasCustomStaticInvokeMethod, hasProperty, incVersion, invokeConstructor, invokeMethod, invokeMethod, invokeMissingMethod, invokeMissingProperty, invokeStaticMissingProperty, isGroovyObject, isInitialized, isModified, isPermissivePropertyAccess, onGetPropertyFoundInHierarchy, onInvokeMethodFoundInHierarchy, onMixinMethodFound, onSetPropertyFoundInHierarchy, onSuperMethodFoundInHierarchy, onSuperPropertyFoundInHierarchy, reinitialize, retrieveConstructor, retrieveStaticMethod, selectConstructorAndTransformArguments, setAttribute, setInitialized, setPermissivePropertyAccess, setProperty, toString`

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

- ## Constructor Details

  - ### ClosureMetaClass

    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 Details

  - ### resetCachedMetaClasses

    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.

  - ### getMetaProperty

    public MetaProperty getMetaProperty(String name)
    Gets the meta property for the given name from the closure metaclass.
    Specified by:
      `getMetaProperty` in interface `MetaObjectProtocol`
    Overrides:
      `getMetaProperty` in class `MetaClassImpl`
    Parameters:
      `name` - the property name
    Returns:
      the meta property or null if not found
    See Also:
      - `MetaObjectProtocol.getMetaProperty(String)`

  - ### invokeMethod

    public Object invokeMethod(Class sender, Object object, String methodName, Object[] arguments, boolean isCallToSuper, boolean fromInsideClass)
    Description copied from class: `MetaClassImpl`
    Invokes a method on the given receiver for the specified arguments. The sender is the class that invoked the method on the object. The MetaClass will attempt to establish the method to invoke based on the name and arguments provided.

    The isCallToSuper and fromInsideClass help the Groovy runtime perform optimisations on the call to go directly to the super class if necessary

    Specified by:
      `invokeMethod` in interface `MetaClass`
    Overrides:
      `invokeMethod` in class `MetaClassImpl`
    Parameters:
      `sender` - The java.lang.Class instance that invoked the method
      `object` - The object which the method was invoked on
      `methodName` - The name of the method
      `arguments` - The arguments to the method
      `isCallToSuper` - Whether the method is a call to a super class method
      `fromInsideClass` - Whether the call was invoked from the inside or the outside of the class
    Returns:
      The return value of the method.
    See Also:
      - `MetaClass.invokeMethod(Class, Object, String, Object[], boolean, boolean)`

  - ### initialize

    public void initialize()
    Initializes the closure metaclass by discovering and indexing all closure-related methods.
    Specified by:
      `initialize` in interface `MetaClass`
    Overrides:
      `initialize` in class `MetaClassImpl`

  - ### getMethods

    public List<MetaMethod> getMethods()
    Returns a list of all methods available to closures, including both closure-specific doCall methods and inherited Closure class methods.
    Specified by:
      `getMethods` in interface `MetaClass`
    Specified by:
      `getMethods` in interface `MetaObjectProtocol`
    Overrides:
      `getMethods` in class `MetaClassImpl`
    Returns:
      a list of all available methods
    See Also:
      - `MetaMethod`

  - ### getMetaMethods

    public List<MetaMethod> getMetaMethods()
    Returns a list of all metamethods in the Closure metaclass.
    Specified by:
      `getMetaMethods` in interface `MetaClass`
    Overrides:
      `getMetaMethods` in class `MetaClassImpl`
    Returns:
      a list of all metamethods

  - ### getProperties

    public List<MetaProperty> getProperties()
    Returns a list of all properties available to closures.
    Specified by:
      `getProperties` in interface `MetaClass`
    Specified by:
      `getProperties` in interface `MetaObjectProtocol`
    Overrides:
      `getProperties` in class `MetaClassImpl`
    Returns:
      a list of all available properties
    See Also:
      - `MetaProperty`

  - ### pickMethod

    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.
    Specified by:
      `pickMethod` in interface `MetaClass`
    Overrides:
      `pickMethod` in class `MetaClassImpl`
    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

  - ### retrieveStaticMethod

    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

  - ### getStaticMetaMethod

    public MetaMethod getStaticMetaMethod(String name, Object[] args)
    Gets a static meta method by name and argument objects. Delegates to the Closure metaclass.
    Specified by:
      `getStaticMetaMethod` in interface `MetaObjectProtocol`
    Overrides:
      `getStaticMetaMethod` in class `MetaClassImpl`
    Parameters:
      `name` - the name of the static method
      `args` - the arguments
    Returns:
      the matching static meta method, or null if not found
    See Also:
      - `MetaObjectProtocol.getStaticMetaMethod(String, Object[])`

  - ### getStaticMetaMethod

    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

  - ### getProperty

    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.
    Specified by:
      `getProperty` in interface `MetaClass`
    Overrides:
      `getProperty` in class `MetaClassImpl`
    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

  - ### getAttribute

    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.
    Overrides:
      `getAttribute` in class `MetaClassImpl`
    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

  - ### setAttribute

    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.
    Specified by:
      `setAttribute` in interface `MetaClass`
    Overrides:
      `setAttribute` in class `MetaClassImpl`
    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

  - ### invokeStaticMethod

    public Object invokeStaticMethod(Object object, String methodName, Object[] arguments)
    Invokes a static method on the closure class itself. Delegates to the static metaclass.
    Specified by:
      `invokeStaticMethod` in interface `MetaObjectProtocol`
    Overrides:
      `invokeStaticMethod` in class `MetaClassImpl`
    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
    See Also:
      - `MissingMethodException`

  - ### setProperty

    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.
    Specified by:
      `setProperty` in interface `MetaClass`
    Overrides:
      `setProperty` in class `MetaClassImpl`
    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

  - ### getMethodWithoutCaching

    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.
    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
    Throws:
      `UnsupportedOperationException` - this method is not supported for closures

  - ### setProperties

    public void setProperties(Object bean, Map map)
    Sets multiple properties on a bean. This operation is not supported for ClosureMetaClass.
    Overrides:
      `setProperties` in class `MetaClassImpl`
    Parameters:
      `bean` - the bean object
      `map` - the properties to set
    Throws:
      `UnsupportedOperationException` - this method is not supported for closures

  - ### addMetaBeanProperty

    public void addMetaBeanProperty(MetaBeanProperty mp)
    Adds a meta bean property to this metaclass. This operation is not supported for ClosureMetaClass as it is immutable.
    Specified by:
      `addMetaBeanProperty` in interface `MutableMetaClass`
    Overrides:
      `addMetaBeanProperty` in class `MetaClassImpl`
    Parameters:
      `mp` - the meta bean property to add
    Throws:
      `UnsupportedOperationException` - this method is not supported for closures

  - ### addMetaMethod

    public void addMetaMethod(MetaMethod method)
    Adds a meta method to this metaclass. This operation is not supported for ClosureMetaClass as it is immutable.
    Specified by:
      `addMetaMethod` in interface `MutableMetaClass`
    Overrides:
      `addMetaMethod` in class `MetaClassImpl`
    Parameters:
      `method` - the meta method to add
    Throws:
      `UnsupportedOperationException` - this method is not supported for closures
    See Also:
      - `MetaClassImpl.initialize()`

  - ### addNewInstanceMethod

    public void addNewInstanceMethod(Method method)
    Adds a new instance method to this metaclass. This operation is not supported for ClosureMetaClass as it is immutable.
    Specified by:
      `addNewInstanceMethod` in interface `MutableMetaClass`
    Overrides:
      `addNewInstanceMethod` in class `MetaClassImpl`
    Parameters:
      `method` - the method to add
    Throws:
      `UnsupportedOperationException` - this method is not supported for closures

  - ### addNewStaticMethod

    public void addNewStaticMethod(Method method)
    Adds a new static method to this metaclass. This operation is not supported for ClosureMetaClass as it is immutable.
    Specified by:
      `addNewStaticMethod` in interface `MutableMetaClass`
    Overrides:
      `addNewStaticMethod` in class `MetaClassImpl`
    Parameters:
      `method` - the static method to add
    Throws:
      `UnsupportedOperationException` - this method is not supported for closures

  - ### retrieveConstructor

    public Constructor retrieveConstructor(Class[] arguments)
    Retrieves a constructor matching the given argument types. This operation is not supported for ClosureMetaClass.
    Overrides:
      `retrieveConstructor` in class `MetaClassImpl`
    Parameters:
      `arguments` - the argument types
    Returns:
      never returns; always throws UnsupportedOperationException
    Throws:
      `UnsupportedOperationException` - this method is not supported for closures

  - ### respondsTo

    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.
    Specified by:
      `respondsTo` in interface `MetaObjectProtocol`
    Overrides:
      `respondsTo` in class `MetaClassImpl`
    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
    See Also:
      - `MetaObjectProtocol.respondsTo(Object, String, Object[])`

  - ### respondsTo

    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.
    Specified by:
      `respondsTo` in interface `MetaObjectProtocol`
    Overrides:
      `respondsTo` in class `MetaClassImpl`
    Parameters:
      `obj` - the object to check
      `name` - the method name
    Returns:
      a list of matching methods, or an empty list if none match
    See Also:
      - `MetaObjectProtocol.respondsTo(Object, String)`

  - ### applyPropertyDescriptors

    protected void applyPropertyDescriptors(PropertyDescriptor[] propertyDescriptors)
    Description copied from class: `MetaClassImpl`
    Turns JavaBeans property descriptors into Groovy meta properties.
    Overrides:
      `applyPropertyDescriptors` in class `MetaClassImpl`
    Parameters:
      `propertyDescriptors` - the descriptors to apply