|
Groovy Documentation | |||||||
FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object groovy.lang.GroovyObjectSupport groovy.lang.Closure
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() == 1To 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
Field Summary | |
---|---|
static int |
DELEGATE_FIRST
With this resolveStrategy set the closure will attempt to resolve property references to the delegate first then the owner. |
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 Closure |
IDENTITY
|
static int |
OWNER_FIRST
With this resolveStrategy set the closure will attempt to resolve property references 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 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 java.lang.Class[] |
parameterTypes
|
Constructor Summary | |
Closure(java.lang.Object owner, java.lang.Object thisObject)
|
|
Closure(java.lang.Object owner)
Constructor used when the "this" object for the Closure is null. |
Method Summary | |
---|---|
Closure
|
asWritable()
|
java.lang.Object
|
call()
Invokes the closure without any parameters, returning any value if applicable. |
java.lang.Object
|
call(java.lang.Object... args)
|
java.lang.Object
|
call(java.lang.Object arguments)
Invokes the closure, returning any value if applicable. |
java.lang.Object
|
clone()
|
Closure
|
curry(java.lang.Object... arguments)
|
Closure
|
curry(java.lang.Object argument)
|
Closure
|
dehydrate()
|
java.lang.Object
|
getDelegate()
|
int
|
getDirective()
|
int
|
getMaximumNumberOfParameters()
|
java.lang.Object
|
getOwner()
@return the owner Object to which method calls will go which is typically the outer class when the closure is constructed |
java.lang.Class[]
|
getParameterTypes()
|
java.lang.Object
|
getProperty(java.lang.String property)
|
int
|
getResolveStrategy()
Gets the strategy which the closure users to resolve methods and properties |
java.lang.Object
|
getThisObject()
|
boolean
|
isCase(java.lang.Object candidate)
|
Closure
|
leftShift(Closure other)
|
java.lang.Object
|
leftShift(java.lang.Object arg)
|
Closure
|
memoize()
|
Closure
|
memoizeAtLeast(int protectedCacheSize)
Creates a caching variant of the closure with automatic cache size adjustment and lower limit on the cache size. |
Closure
|
memoizeAtMost(int maxCacheSize)
|
Closure
|
memoizeBetween(int protectedCacheSize, int maxCacheSize)
Creates a caching variant of the closure with automatic cache size adjustment and lower and upper limits on the cache size. |
Closure
|
ncurry(int n, java.lang.Object... arguments)
|
Closure
|
ncurry(int n, java.lang.Object argument)
|
Closure
|
rcurry(java.lang.Object... arguments)
|
Closure
|
rcurry(java.lang.Object argument)
|
Closure
|
rehydrate(java.lang.Object delegate, java.lang.Object owner, java.lang.Object thisObject)
Returns a copy of this closure for which the delegate, owner and thisObject are replaced with the supplied parameters. |
Closure
|
rightShift(Closure other)
|
void
|
run()
|
void
|
setDelegate(java.lang.Object delegate)
|
void
|
setDirective(int directive)
|
void
|
setProperty(java.lang.String property, java.lang.Object newValue)
|
void
|
setResolveStrategy(int resolveStrategy)
Sets the strategy which the closure uses to resolve property references. |
protected static java.lang.Object
|
throwRuntimeException(java.lang.Throwable throwable)
|
Closure
|
trampoline(java.lang.Object... args)
Builds a trampolined variant of the current closure. |
Closure
|
trampoline()
|
Methods inherited from class GroovyObjectSupport | |
---|---|
getMetaClass, getProperty, invokeMethod, setMetaClass, setProperty |
Methods inherited from class java.lang.Object | |
---|---|
java.lang.Object#wait(long, int), java.lang.Object#wait(long), java.lang.Object#wait(), java.lang.Object#equals(java.lang.Object), java.lang.Object#toString(), java.lang.Object#hashCode(), java.lang.Object#getClass(), java.lang.Object#notify(), java.lang.Object#notifyAll() |
Field Detail |
---|
public static final int DELEGATE_FIRST
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.
public static final int DELEGATE_ONLY
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.
public static final int DONE
public static final Closure IDENTITY
public static final int OWNER_FIRST
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.
public static final int OWNER_ONLY
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.
public static final int SKIP
public static final int TO_SELF
Note that local variables are always looked up first, independently of the resolution strategy.
protected int maximumNumberOfParameters
protected java.lang.Class[] parameterTypes
Constructor Detail |
---|
public Closure(java.lang.Object owner, java.lang.Object thisObject)
public Closure(java.lang.Object owner)
owner
- the Closure owner
Method Detail |
---|
public Closure asWritable()
public java.lang.Object call()
} public java.lang.Object call(java.lang.Object... args)
public java.lang.Object call(java.lang.Object arguments)
arguments
- could be a single value or a List of values
public java.lang.Object clone()
public Closure curry(java.lang.Object... arguments)
public Closure curry(java.lang.Object argument)
return result; public Closure dehydrate()
public java.lang.Object getDelegate()
public int getDirective()
public int getMaximumNumberOfParameters()
public java.lang.Object getOwner()
public java.lang.Class[] getParameterTypes()
public java.lang.Object getProperty(java.lang.String property)
public int getResolveStrategy()
public java.lang.Object getThisObject()
public boolean isCase(java.lang.Object candidate)
public Closure leftShift(Closure other)
public java.lang.Object leftShift(java.lang.Object arg)
public Closure memoize()
public Closure memoizeAtLeast(int protectedCacheSize)
protectedCacheSize
- Number of cached return values to protect from garbage collection
public Closure memoizeAtMost(int maxCacheSize)
public Closure memoizeBetween(int protectedCacheSize, int maxCacheSize)
protectedCacheSize
- Number of cached return values to protect from garbage collectionmaxCacheSize
- The maximum size the cache can grow to
public Closure ncurry(int n, java.lang.Object... arguments)
public Closure ncurry(int n, java.lang.Object argument)
public Closure rcurry(java.lang.Object... arguments)
public Closure rcurry(java.lang.Object argument)
return result; public Closure rehydrate(java.lang.Object delegate, java.lang.Object owner, java.lang.Object thisObject)
delegate
- the closure delegateowner
- the closure ownerthisObject
- the closure "this" object
public Closure rightShift(Closure other)
public void run()
public void setDelegate(java.lang.Object delegate)
public void setDirective(int directive)
public void setProperty(java.lang.String property, java.lang.Object newValue)
public void setResolveStrategy(int resolveStrategy)
resolveStrategy
- The resolve strategy to set
protected static java.lang.Object throwRuntimeException(java.lang.Throwable throwable)
public Closure trampoline(java.lang.Object... args)
def fact fact = { n, total -> n == 0 ? total : fact.trampoline(n - 1, n * total) }.trampoline() def factorial = { n -> fact(n, 1G)} println factorial(20) // => 2432902008176640000
args
- Parameters to the closure, so as the trampoline mechanism can call it
public Closure trampoline()
Groovy Documentation