Class ClosureMetaMethod

java.lang.Object
org.codehaus.groovy.reflection.ParameterTypes
groovy.lang.MetaMethod
org.codehaus.groovy.runtime.metaclass.ClosureMetaMethod
All Implemented Interfaces:
ClosureInvokingMethod, MetaMember, Cloneable

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

    • ClosureMetaMethod

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

      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 Details

    • getModifiers

      public int getModifiers()
      Returns the modifiers for this method.
      Specified by:
      getModifiers in interface MetaMember
      Specified by:
      getModifiers in class MetaMethod
      Returns:
      Modifier.PUBLIC
    • getName

      public String getName()
      Returns the name of this closure meta method.
      Specified by:
      getName in interface MetaMember
      Specified by:
      getName in class MetaMethod
      Returns:
      the name of the method
    • getReturnType

      public Class getReturnType()
      Returns the return type of this method.
      Specified by:
      getReturnType in class MetaMethod
      Returns:
      Object.class, as closures return Object
    • getDeclaringClass

      public CachedClass getDeclaringClass()
      Returns the cached class that declares this meta method.
      Specified by:
      getDeclaringClass in class MetaMethod
      Returns:
      the declaring class
    • invoke

      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.
      Specified by:
      invoke in class MetaMethod
      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
    • getClosure

      public Closure getClosure()
      Retrieves the closure that is invoked by this MetaMethod
      Specified by:
      getClosure in interface ClosureInvokingMethod
      Returns:
      The closure
    • createMethodList

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

      public CachedMethod getDoCall()
      Returns the cached method that represents the closure's doCall method.
      Returns:
      the cached doCall method
    • copy

      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