Top Description Inners Fields Constructors Methods
groovy.lang

public abstract Class Closure<V>

extends GroovyObjectSupport
implements Cloneable, Runnable, GroovyCallable<V>, Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable, groovy.lang.GroovyCallable, java.util.concurrent.Callable, java.lang.Runnable, java.lang.Cloneable
Known Direct Subclasses
groovy.lang.Closure.WritableClosure, groovy.lang.TrampolineClosure, org.codehaus.groovy.runtime.ComposedClosure, org.codehaus.groovy.runtime.CurriedClosure, org.codehaus.groovy.runtime.IteratorClosureAdapter, org.codehaus.groovy.runtime.MethodClosure, org.codehaus.groovy.runtime.ProxyGeneratorAdapter.ReturnValueWrappingClosure, org.codehaus.groovy.runtime.memoize.Memoize.MemoizeFunction
Imports
org.apache.groovy.internal.util.UncheckedThrow, org.apache.groovy.io.StringBuilderWriter, org.codehaus.groovy.reflection.ReflectionCache, org.codehaus.groovy.reflection.stdclasses.CachedClosureClass, org.codehaus.groovy.runtime.ComposedClosure, .CurriedClosure, .InvokerHelper, .InvokerInvocationException, org.codehaus.groovy.runtime.callsite.BooleanClosureWrapper, org.codehaus.groovy.runtime.memoize.ConcurrentCommonCache, .ConcurrentSoftCache, .LRUCache, .Memoize, java.io.IOException, .Serializable, .Writer

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

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class

Field Summary

Modifier and TypeField and Description
private BooleanClosureWrapper
private Object
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.

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.

private int
public static final int
private static final Object[]
public static final Closure<V>
protected int
private Object
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).

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.

protected Class<T>[]
private int
private static final long
public static final int
private Object
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.

Constructor Summary

AccessConstructor and Description
public
Closure(Object owner, Object thisObject)

public
Closure(Object
the Closure owner
owner
)

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

Method Summary

Modifier and TypeMethod and Description
public <W> Closure<W>

Returns:

the newly composed closure
andThen
(final Closure<W> other)

Alias for rightShift(Closure)

public Closure<V>

Returns:

the newly composed closure
andThenSelf
()

Call andThen(Closure) on this.

public Closure<V>

Returns:

the newly composed closure
andThenSelf
(int
the number of times to reverse compose the closure with itself
times
)

Call andThen(Closure) on this exactly times times.

public Closure<V>

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.
asWritable
()

public V

Returns:

The value if applicable or null if there is no return statement in the closure.
call
()

Implements java.util.concurrent.Callable.call.

Invokes the closure with no arguments, returning any value if applicable.
public V

Returns:

The value if applicable or null if there is no return statement in the closure.
call
(final Object
could be a single value or a List of values
arguments
)

Invokes the closure with given argument(s), returning any value if applicable.

public V

Returns:

The value if applicable or null if there is no return statement in the closure.
call
(final Object... arguments)

Invokes the closure with given argument(s), returning any value if applicable.

public Object
clone()

Overrides java.lang.Object.clone.

Creates and returns a copy of this object.
public Closure<V>

Returns:

the newly composed closure
compose
(final Closure<V> other)

Alias for leftShift(Closure)

public Closure<V>

Returns:

the newly composed closure
composeSelf
()

Call compose(Closure) on this.

public Closure<V>

Returns:

the newly composed closure
composeSelf
(int
the number of times to compose the closure with itself
times
)

Call compose(Closure) on this exactly times times.

public Closure<V>

Returns:

the new closure with its arguments bound
curry
(final Object...
the arguments to bind
arguments
)

Support for Closure currying.

public Closure<V>

Returns:

the new closure with the argument bound
curry
(final Object
the argument to bind
argument
)

Support for Closure currying.

public Closure<V>

Returns:

a serializable closure.
dehydrate
()

Returns a copy of this closure where the "owner", "delegate" and "thisObject" fields are null, allowing proper serialization when one of them is not serializable.

public Object

Returns:

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

public int

Returns:

Returns the directive.
getDirective
()

public int

Returns:

the maximum number of parameters a doCall method of this closure can take
getMaximumNumberOfParameters
()

public Object

Returns:

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

public Class<T>[]

Returns:

the parameter types of the longest doCall method of this closure
getParameterTypes
()

public Object
getProperty(final String
the name of the property of interest
property
)

Overrides default groovy.lang.GroovyObject.getProperty.

Retrieves a property value.
private Object
private Object
private Object
getPropertyTryThese(String property, Object firstTry, Object secondTry)

public int

Returns:

The resolve strategy
getResolveStrategy
()

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

public Object
public boolean
isCase(Object candidate)

public Closure<V>

Returns:

the new composed Closure
leftShift
(final Closure<V>
the Closure to compose with the current Closure
other
)

Support for Closure reverse composition.

public V

Returns:

the result of calling the Closure
leftShift
(final Object
the argument to call the closure with
arg
)

Alias for calling a Closure for non-closure arguments.

public Closure<V>

Returns:

A new closure forwarding to the original one while caching the results
memoize
()

Creates a caching variant of the closure.

public Closure<V>

Returns:

A new function forwarding to the original one while caching the results
memoizeAtLeast
(final int
Number of cached return values to protect from garbage collection
protectedCacheSize
)

Creates a caching variant of the closure with automatic cache size adjustment and lower limit on the cache size.

public Closure<V>

Returns:

A new function forwarding to the original one while caching the results
memoizeAtMost
(final int
The maximum size the cache can grow to
maxCacheSize
)

Creates a caching variant of the closure with upper limit on the cache size.

public Closure<V>

Returns:

A new function forwarding to the original one while caching the results
memoizeBetween
(final int
Number of cached return values to protect from garbage collection
protectedCacheSize
,
final int
The maximum size the cache can grow to
maxCacheSize
)

Creates a caching variant of the closure with automatic cache size adjustment and lower and upper limits on the cache size.

public Closure<V>

Returns:

the new closure with its arguments bound
ncurry
(int
the index from which to bind parameters (may be -ve in which case it will be normalized)
n
,
final Object...
the arguments to bind
arguments
)

Support for Closure currying at a given index.

public Closure<V>

Returns:

the new closure with the argument bound
ncurry
(int n, final Object
the argument to bind
argument
)

Support for Closure currying at a given index.

public Closure<V>

Returns:

the new closure with its arguments bound
rcurry
(final Object...
the arguments to bind
arguments
)

Support for Closure "right" currying.

public Closure<V>

Returns:

the new closure with the argument bound
rcurry
(final Object
the argument to bind
argument
)

Support for Closure "right" currying.

public Closure<V>

Returns:

a copy of this closure where owner, delegate and thisObject are replaced
rehydrate
(Object
the closure delegate
delegate
,
Object
the closure owner
owner
,
Object
the closure "this" object
thisObject
)

Returns a copy of this closure for which the delegate, owner and thisObject are replaced with the supplied parameters.

public <W> Closure<W>

Returns:

the new composed Closure
rightShift
(final Closure<W>
the Closure to compose with the current Closure
other
)

Support for Closure forward composition.

public void
run()

Implements java.lang.Runnable.run.

Runs this operation.
public void
setDelegate(Object
the new delegate
delegate
)

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

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

public void
setProperty(String
the name of the property of interest
property
,
Object
the new value for the property
newValue
)

Overrides default groovy.lang.GroovyObject.setProperty.

Sets the given property to the new value.
private void
private void
setPropertyOwnerFirst(String property, Object newValue)

private void
setPropertyTryThese(String property, Object newValue, Object firstTry, Object secondTry)

public void
setResolveStrategy(int
The resolve strategy to set
resolveStrategy
)

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

protected static Object
public Closure<V>

Returns:

A closure, which will execute the original closure on a trampoline.
trampoline
(final Object...
Parameters to the closure, so as the trampoline mechanism can call it
args
)

Builds a trampolined variant of the current closure.

public Closure<V>

Returns:

A closure, which will execute the original closure on a trampoline.
trampoline
()

Builds a trampolined variant of the current closure.

Inherited from groovy.lang.GroovyObjectSupport:
getMetaClasssetMetaClass