Groovy 1.7.0

groovy.lang
Class Closure

java.lang.Object
  groovy.lang.GroovyObjectSupport
      groovy.lang.Closure
All Implemented Interfaces:
Cloneable

class Closure
extends GroovyObjectSupport

Represents any closure object in Groovy.

Groovy allows instances of Closures to be called in a short form. For example:

   def a = 1
   def c = {a}
   assert c() == 1
 
To be able to use a Closure in this way with your own subclass, you need to provide a doCall method with any signature you want to. This ensures that #getMaximumNumberOfParameters() and #getParameterTypes() will work too without any additional code. If no doCall method is provided a closure must be used in its long form like
   def a = 1
   def c = {a}
   assert c.call() == 1
 
author:
James Strachan
author:
John Wilson
author:
Jochen Theodorou
author:
Graeme Rocher
version:
$Revision: 18300 $


Nested Class Summary
class Closure.WritableClosure

 
Field Summary
static int DELEGATE_FIRST

With this resolveStrategy set the closure will attempt to resolve property references to the delegate first

static int DELEGATE_ONLY

With this resolveStrategy set the closure will resolve property references to the delegate only and entirely bypass the owner

static int DONE

static int OWNER_FIRST

With this resolveStrategy set the closure will attempt to resolve property references to the owner first

static int OWNER_ONLY

With this resolveStrategy set the closure will resolve property references to the owner only and not call the delegate at all

static int SKIP

static int TO_SELF

With this resolveStrategy set the closure will resolve property references to itself and go through the usual MetaClass look-up process.

protected int maximumNumberOfParameters

protected Class[] parameterTypes

 
Constructor Summary
Closure(Object owner, Object thisObject)

Closure(Object owner)

 
Method Summary
Closure asWritable()

Object call()

Invokes the closure without any parameters, returning any value if applicable.

Object call(Object[] args)

Object call(Object arguments)

Invokes the closure, returning any value if applicable.

Object clone()

Closure curry(Object[] arguments)

Support for closure currying

Object getDelegate()

typically the outer class when the closure is constructed

int getDirective()

int getMaximumNumberOfParameters()

of this closure can take

Object getOwner()

typically the outer class when the closure is constructed

Class[] getParameterTypes()

of this closure

Object getProperty(String property)

int getResolveStrategy()

Gets the strategy which the closure users to resolve methods and properties

Object getThisObject()

boolean isCase(Object candidate)

void run()

void setDelegate(Object delegate)

Allows the delegate to be changed such as when performing markup building

void setDirective(int directive)

void setProperty(String property, Object newValue)

void setResolveStrategy(int resolveStrategy)

Sets the strategy which the closure uses to resolve property references.

protected static Object throwRuntimeException(Throwable throwable)

 
Methods inherited from class GroovyObjectSupport
getMetaClass, getProperty, invokeMethod, setMetaClass, setProperty
 
Methods inherited from class Object
wait, wait, wait, hashCode, getClass, equals, toString, notify, notifyAll
 

Field Detail

DELEGATE_FIRST

static final int DELEGATE_FIRST
With this resolveStrategy set the closure will attempt to resolve property references to the delegate first


DELEGATE_ONLY

static final int DELEGATE_ONLY
With this resolveStrategy set the closure will resolve property references to the delegate only and entirely bypass the owner


DONE

static final int DONE


OWNER_FIRST

static final int OWNER_FIRST
With this resolveStrategy set the closure will attempt to resolve property references to the owner first


OWNER_ONLY

static final int OWNER_ONLY
With this resolveStrategy set the closure will resolve property references to the owner only and not call the delegate at all


SKIP

static final int SKIP


TO_SELF

static final int TO_SELF
With this resolveStrategy set the closure will resolve property references to itself and go through the usual MetaClass look-up process. This allows the developer to override getProperty using ExpandoMetaClass of the closure itself


maximumNumberOfParameters

protected int maximumNumberOfParameters


parameterTypes

protected Class[] parameterTypes


 
Constructor Detail

Closure

public Closure(Object owner, Object thisObject)


Closure

public Closure(Object owner)


 
Method Detail

asWritable

public Closure asWritable()
return:
a version of this closure which implements Writable. Note that the returned Writable also overrides #toString() in order to allow rendering the result directly to a String.


call

public Object call()
Invokes the closure without any parameters, returning any value if applicable.
return:
the value if applicable or null if there is no return statement in the closure


call

public Object call(Object[] args)


call

public Object call(Object arguments)
Invokes the closure, returning any value if applicable.
param:
arguments could be a single value or a List of values
return:
the value if applicable or null if there is no return statement in the closure


clone

public Object clone()


curry

public Closure curry(Object[] arguments)
Support for closure currying
param:
arguments the arguments to bind
return:
the new closure with its arguments bound


getDelegate

public Object getDelegate()
return:
the delegate Object to which method calls will go which is typically the outer class when the closure is constructed


getDirective

public int getDirective()
return:
Returns the directive.


getMaximumNumberOfParameters

public int getMaximumNumberOfParameters()
return:
the maximum number of parameters a doCall method of this closure can take


getOwner

public Object getOwner()
return:
the owner Object to which method calls will go which is typically the outer class when the closure is constructed


getParameterTypes

public Class[] getParameterTypes()
return:
the parameter types of the longest doCall method of this closure


getProperty

public Object getProperty(String property)


getResolveStrategy

public int getResolveStrategy()
Gets the strategy which the closure users to resolve methods and properties
return:
The resolve strategy
see:
Closure#DELEGATE_FIRST
see:
Closure#DELEGATE_ONLY
see:
Closure#OWNER_FIRST
see:
Closure#OWNER_ONLY
see:
Closure#TO_SELF


getThisObject

public Object getThisObject()


isCase

public boolean isCase(Object candidate)


run

public void run()


setDelegate

public void setDelegate(Object delegate)
Allows the delegate to be changed such as when performing markup building
param:
delegate the new delegate


setDirective

public void setDirective(int directive)
param:
directive The directive to set.


setProperty

public void setProperty(String property, Object newValue)


setResolveStrategy

public void setResolveStrategy(int resolveStrategy)
Sets the strategy which the closure uses to resolve property references. The default is Closure.OWNER_FIRST
param:
resolveStrategy The resolve strategy to set
see:
Closure#DELEGATE_FIRST
see:
Closure#DELEGATE_ONLY
see:
Closure#OWNER_FIRST
see:
Closure#OWNER_ONLY
see:
Closure#TO_SELF


throwRuntimeException

protected static Object throwRuntimeException(Throwable throwable)


 

Copyright © 2003-2009 The Codehaus. All rights reserved.