Groovy 2.2.1

groovy.lang
[Java] Class Closure

java.lang.Object
  groovy.lang.GroovyObjectSupport
      groovy.lang.Closure
All Implemented Interfaces:
Cloneable, GroovyCallable, Runnable, Serializable

public abstract 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
 
Authors:
James Strachan
John Wilson
Jochen Theodorou
Graeme Rocher
Paul King


Field Summary
static int DELEGATE_FIRST

With this resolveStrategy set the closure will attempt to resolve property references and methods to the delegate first then the owner.

static int DELEGATE_ONLY

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

static int DONE

static Closure IDENTITY

static int OWNER_FIRST

With this resolveStrategy set the closure will attempt to resolve property references and methods to the owner first, then the delegate (this is the default strategy).

static int OWNER_ONLY

With this resolveStrategy set the closure will resolve property references and methods 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)

Constructor used when the "this" object for the Closure is null.

 
Method Summary
Closure asWritable()

Object call()

Object call(Object... args)

Object call(Object arguments)

Invokes the closure, returning any value if applicable.

Object clone()

Closure curry(Object... arguments)

Closure curry(Object argument)

Closure dehydrate()

Object getDelegate()

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

int getDirective()

@param directive The directive to set.

int getMaximumNumberOfParameters()

Returns:
a version of this closure which implements Writable.

Object getOwner()

@return the delegate Object to which method calls will go which is typically the outer class when the closure is constructed

Class[] getParameterTypes()

@return the maximum number of parameters a doCall method of this closure can take

Object getProperty(String property)

int getResolveStrategy()

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

Object getThisObject()

boolean isCase(Object candidate)

Closure leftShift(Closure other)

Object leftShift(Object arg)

Closure memoize()

Closure memoizeAtLeast(int protectedCacheSize)

Closure memoizeAtMost(int maxCacheSize)

Closure memoizeBetween(int protectedCacheSize, int maxCacheSize)

Closure ncurry(int n, Object... arguments)

Closure ncurry(int n, Object argument)

Closure rcurry(Object... arguments)

Closure rcurry(Object argument)

Closure rehydrate(Object delegate, Object owner, Object thisObject)

Closure rightShift(Closure other)

void run()

void setDelegate(Object delegate)

@return the parameter types of the longest doCall method of this closure

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 and methods.

protected static Object throwRuntimeException(Throwable throwable)

Closure trampoline(Object... args)

Closure trampoline()

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

Field Detail

DELEGATE_FIRST

public static final int DELEGATE_FIRST
With this resolveStrategy set the closure will attempt to resolve property references and methods to the delegate first then the owner. For example the following code :
 class Test {
     def x = 30
     def y = 40

     def run() {
         def data = [ x: 10, y: 20 ]
         def cl = { y = x + y }
         cl.delegate = data
         cl.resolveStrategy = Closure.DELEGATE_FIRST
         cl()
         println x
         println y
         println data
     }
 }

 new Test().run()
 
will output :
 30
 40
 [x:10, y:30]
 
because the x and y variables declared in the delegate shadow the fields in the owner class.

Note that local variables are always looked up first, independently of the resolution strategy.


DELEGATE_ONLY

public static final int DELEGATE_ONLY
With this resolveStrategy set the closure will resolve property references and methods to the delegate only and entirely bypass the owner. For example the following code :
 class Test {
     def x = 30
     def y = 40
     def z = 50

     def run() {
         def data = [ x: 10, y: 20 ]
         def cl = { y = x + y + z }
         cl.delegate = data
         cl.resolveStrategy = Closure.DELEGATE_ONLY
         cl()
         println x
         println y
         println data
     }
 }

 new Test().run()
 
will throw an error because even if the owner declares a "z" field, the resolution strategy will bypass lookup in the owner.

Note that local variables are always looked up first, independently of the resolution strategy.


DONE

public static final int DONE


IDENTITY

public static final Closure IDENTITY


OWNER_FIRST

public static final int OWNER_FIRST
With this resolveStrategy set the closure will attempt to resolve property references and methods to the owner first, then the delegate (this is the default strategy). For example the following code :
 class Test {
     def x = 30
     def y = 40

     def run() {
         def data = [ x: 10, y: 20 ]
         def cl = { y = x + y }
         cl.delegate = data
         cl()
         println x
         println y
         println data
     }
 }

 new Test().run()
 
will output :
 30
 70
 [x:10, y:20]
 
because the x and y fields declared in the Test class the variables in the delegate.

Note that local variables are always looked up first, independently of the resolution strategy.


OWNER_ONLY

public static final int OWNER_ONLY
With this resolveStrategy set the closure will resolve property references and methods to the owner only and not call the delegate at all. For example the following code :
 class Test {
     def x = 30
     def y = 40

     def run() {
         def data = [ x: 10, y: 20, z: 30 ]
         def cl = { y = x + y + z }
         cl.delegate = data
         cl.resolveStrategy = Closure.OWNER_ONLY
         cl()
         println x
         println y
         println data
     }
 }

 new Test().run()
 
will throw "No such property: z" error because even if the z variable is declared in the delegate, no lookup is made.

Note that local variables are always looked up first, independently of the resolution strategy.


SKIP

public static final int SKIP


TO_SELF

public 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 means that properties and methods are neither resolved from the owner nor the delegate, but only on the closure object itself. This allows the developer to override getProperty using ExpandoMetaClass of the closure itself.

Note that local variables are always looked up first, independently of the resolution strategy.


maximumNumberOfParameters

protected int maximumNumberOfParameters


parameterTypes

protected Class[] parameterTypes


 
Constructor Detail

Closure

public Closure(Object owner, Object thisObject)


Closure

public Closure(Object owner)
Constructor used when the "this" object for the Closure is null. This is rarely the case in normal Groovy usage.
Parameters:
owner - the Closure owner


 
Method Detail

asWritable

public Closure asWritable()


call

public Object call()


call

@SuppressWarnings}
public Object call(Object... args)


call

public Object call(Object arguments)
Invokes the closure, returning any value if applicable.
Parameters:
arguments - could be a single value or a List of values
Returns:
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)


curry

public Closure curry(Object argument)


dehydrate

@SuppressWarnings* Returns a copy of this closure for which the delegate, owner and thisObject are
public Closure dehydrate()


getDelegate

public Object getDelegate()
Allows the delegate to be changed such as when performing markup building
Parameters:
delegate - the new delegate


getDirective

public int getDirective()
Parameters:
directive - The directive to set.


getMaximumNumberOfParameters

public int getMaximumNumberOfParameters()
Returns:
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.


getOwner

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


getParameterTypes

public Class[] getParameterTypes()
Returns:
the maximum number of parameters a doCall method of this closure can take


getProperty

public Object getProperty(String property)


getResolveStrategy

public int getResolveStrategy()
Gets the strategy which the closure users to resolve methods and properties
Returns:
The resolve strategy
See Also:
Closure.DELEGATE_FIRST
Closure.DELEGATE_ONLY
Closure.OWNER_FIRST
Closure.OWNER_ONLY
Closure.TO_SELF


getThisObject

public Object getThisObject()


isCase

public boolean isCase(Object candidate)


leftShift

public Closure leftShift(Closure other)


leftShift

public Object leftShift(Object arg)


memoize

public Closure memoize()


memoizeAtLeast

public Closure memoizeAtLeast(int protectedCacheSize)


memoizeAtMost

public Closure memoizeAtMost(int maxCacheSize)


memoizeBetween

public Closure memoizeBetween(int protectedCacheSize, int maxCacheSize)


ncurry

public Closure ncurry(int n, Object... arguments)


ncurry

public Closure ncurry(int n, Object argument)


rcurry

public Closure rcurry(Object... arguments)


rcurry

public Closure rcurry(Object argument)


rehydrate

@SuppressWarnings
public Closure rehydrate(Object delegate, Object owner, Object thisObject)


rightShift

public Closure rightShift(Closure other)


run

public void run()


setDelegate

public void setDelegate(Object delegate)
Returns:
the parameter types of the longest doCall method of this closure


setDirective

public void setDirective(int directive)


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 and methods. The default is Closure.OWNER_FIRST
Parameters:
resolveStrategy - The resolve strategy to set
See Also:
Closure.DELEGATE_FIRST
Closure.DELEGATE_ONLY
Closure.OWNER_FIRST
Closure.OWNER_ONLY
Closure.TO_SELF


throwRuntimeException

protected static Object throwRuntimeException(Throwable throwable)


trampoline

public Closure trampoline(Object... args)


trampoline

public Closure trampoline()


 

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