Package: org.codehaus.groovy.runtime.metaclass

## [Java] Class ClosureMetaMethod

- org.codehaus.groovy.runtime.metaclass.ClosureMetaMethod

- All Implemented Interfaces and Traits:
    ClosureInvokingMethod

      public class ClosureMetaMethod
      extends MetaMethod
      implements ClosureInvokingMethod

  A MetaMethod that accepts a closure in the constructor which is invoked when the MetaMethod is called. The delegate of the closure is set to the instance that the MetaMethod is invoked on when called.

  Since:
    1.5

- - Inherited fields
    Fields inherited from class | Fields
    --- | ---
    `class MetaMethod` | `EMPTY_ARRAY`

  - ### Constructor Summary

    Constructors
    Constructor and description
    `ClosureMetaMethod(String name, Closure c, CachedMethod doCall)` Constructs a new ClosureMetaMethod with the specified name and closure.
    `ClosureMetaMethod(String name, Class declaringClass, Closure c, CachedMethod doCall)` Constructs a new ClosureMetaMethod with the specified name, declaring class, and closure.

  - ### Methods Summary

    Methods
    Type Params | Return Type | Name and description
    --- | --- | ---
    `` | `public static ClosureMetaMethod` | `copy(ClosureMetaMethod closureMethod)` Creates a copy of the given ClosureMetaMethod.
    `` | `public static List<MetaMethod>` | `createMethodList(String name, Class declaringClass, Closure closure)` Creates a list of MetaMethod instances from the given closure.
    `` | `public Closure` | `getClosure()` Retrieves the closure that is invoked by this MetaMethod
    `` | `public CachedClass` | `getDeclaringClass()` Returns the cached class that declares this meta method.
    `` | `public CachedMethod` | `getDoCall()` Returns the cached method that represents the closure's doCall method.
    `` | `public int` | `getModifiers()` Returns the modifiers for this method.
    `` | `public String` | `getName()` Returns the name of this closure meta method.
    `` | `public Class` | `getReturnType()` Returns the return type of this method.
    `` | `public Object` | `invoke(Object object, Object[] arguments)` Invokes the closure with the given arguments.

  - ### Inherited Methods Summary

    Inherited Methods
    Methods inherited from class | Name
    --- | ---
    `class MetaMethod` | `checkParameters, clone, doMethodInvoke, equal, equal, getDeclaringClass, getDescriptor, getModifiers, getMopName, getName, getReturnType, getSignature, invoke, isAbstract, isCacheable, isDefault, isMethod, isSame, processDoMethodInvokeException, toString`
    `class ParameterTypes` | `coerceArgumentsToClasses, correctArguments, getNativeParameterTypes, getPT, getParameterTypes, isValidExactMethod, isValidExactMethod, isValidMethod, isValidMethod, isVargsMethod, isVargsMethod, setParametersTypes`

- - ### Constructor Detail

    - #### public ClosureMetaMethod(String name, Closure c, CachedMethod doCall)

      Constructs a new ClosureMetaMethod with the specified name and closure. The declaring class is obtained from the closure's owner.

      Parameters:
        `name` - the name of the method
        `c` - the closure to invoke when this method is called
        `doCall` - the cached method representing the closure's doCall method

    - #### public ClosureMetaMethod(String name, Class declaringClass, Closure c, CachedMethod doCall)

      Constructs a new ClosureMetaMethod with the specified name, declaring class, and closure.

      Parameters:
        `name` - the name of the method
        `declaringClass` - the class that declares this meta method
        `c` - the closure to invoke when this method is called
        `doCall` - the cached method representing the closure's doCall method

  - ### Method Detail

    - #### public static ClosureMetaMethod copy(ClosureMetaMethod closureMethod)

      Creates a copy of the given ClosureMetaMethod. If the method is a MethodClosureMetaMethod, returns a new MethodClosureMetaMethod; otherwise returns a new ClosureMetaMethod.

      Parameters:
        `closureMethod` - the closure meta method to copy
      Returns:
        a copy of the closure meta method

    - #### public static List<MetaMethod> createMethodList(String name, Class declaringClass, Closure closure)

      Creates a list of MetaMethod instances from the given closure. Handles MethodClosure, GeneratedClosure, and anonymous closures appropriately.

      Parameters:
        `name` - the name of the method
        `declaringClass` - the class that declares this meta method
        `closure` - the closure to create meta methods from
      Returns:
        a list of MetaMethod instances

    - #### public Closure getClosure()

      Retrieves the closure that is invoked by this MetaMethod

      Returns:
        The closure

    - #### public CachedClass getDeclaringClass()

      Returns the cached class that declares this meta method.

      Returns:
        the declaring class

    - #### public CachedMethod getDoCall()

      Returns the cached method that represents the closure's doCall method.

      Returns:
        the cached doCall method

    - #### public int getModifiers()

      Returns the modifiers for this method.

      Returns:
        Modifier.PUBLIC

    - #### public String getName()

      Returns the name of this closure meta method.

      Returns:
        the name of the method

    - #### public Class getReturnType()

      Returns the return type of this method.

      Returns:
        Object.class, as closures return Object

    - #### public Object invoke(Object object, Object[] arguments)

      Invokes the closure with the given arguments. The closure's delegate is set to the object on which this method is being invoked.

      Parameters:
        `object` - the object on which the method is invoked (becomes the closure's delegate)
        `arguments` - the arguments to pass to the closure
      Returns:
        the result of the closure invocation