groovy.lang
Class Closure

java.lang.Object
  extended by groovy.lang.GroovyObjectSupport
      extended by groovy.lang.Closure
All Implemented Interfaces:
GroovyObject, Serializable, Cloneable, Runnable
Direct Known Subclasses:
CurriedClosure, IteratorClosureAdapter, MethodClosure

public abstract class Closure
extends GroovyObjectSupport
implements Cloneable, Runnable, Serializable

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
 

Version:
$Revision: 13718 $
Author:
James Strachan, John Wilson, Jochen Theodorou, Graeme Rocher
See Also:
Serialized Form

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
           
protected  int maximumNumberOfParameters
           
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
protected  Class[] parameterTypes
           
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.
 
Constructor Summary
Closure(Object owner)
           
Closure(Object owner, Object thisObject)
           
 
Method Summary
 Closure asWritable()
           
 Object call()
          Invokes the closure without any parameters, returning any value if applicable.
 Object call(Object arguments)
          Invokes the closure, returning any value if applicable.
 Object call(Object[] args)
           
 Object clone()
           
 Closure curry(Object[] arguments)
          Support for closure currying
 Object getDelegate()
           
 int getDirective()
           
 int getMaximumNumberOfParameters()
           
 Object getOwner()
           
 Class[] getParameterTypes()
           
 Object getProperty(String property)
          Retrieves a property value.
 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)
          Sets the given property to the new value.
 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 groovy.lang.GroovyObjectSupport
getMetaClass, invokeMethod, setMetaClass
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

OWNER_FIRST

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

See Also:
Constant Field Values

DELEGATE_FIRST

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

See Also:
Constant Field Values

OWNER_ONLY

public 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

See Also:
Constant Field Values

DELEGATE_ONLY

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

See Also:
Constant Field Values

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 allows the developer to override getProperty using ExpandoMetaClass of the closure itself

See Also:
Constant Field Values

DONE

public static final int DONE
See Also:
Constant Field Values

SKIP

public static final int SKIP
See Also:
Constant Field Values

parameterTypes

protected Class[] parameterTypes

maximumNumberOfParameters

protected int maximumNumberOfParameters
Constructor Detail

Closure

public Closure(Object owner,
               Object thisObject)

Closure

public Closure(Object owner)
Method Detail

setResolveStrategy

public void setResolveStrategy(int resolveStrategy)
Sets the strategy which the closure uses to resolve property references. The default is Closure.OWNER_FIRST

Parameters:
resolveStrategy - The resolve strategy to set
See Also:
DELEGATE_FIRST, DELEGATE_ONLY, OWNER_FIRST, OWNER_ONLY, TO_SELF

getResolveStrategy

public int getResolveStrategy()
Gets the strategy which the closure users to resolve methods and properties

Returns:
The resolve strategy
See Also:
DELEGATE_FIRST, DELEGATE_ONLY, OWNER_FIRST, OWNER_ONLY, TO_SELF

getThisObject

public Object getThisObject()

getProperty

public Object getProperty(String property)
Description copied from interface: GroovyObject
Retrieves a property value.

Specified by:
getProperty in interface GroovyObject
Overrides:
getProperty in class GroovyObjectSupport
Parameters:
property - the name of the property of interest
Returns:
the given property

setProperty

public void setProperty(String property,
                        Object newValue)
Description copied from interface: GroovyObject
Sets the given property to the new value.

Specified by:
setProperty in interface GroovyObject
Overrides:
setProperty in class GroovyObjectSupport
Parameters:
property - the name of the property of interest
newValue - the new value for the property

isCase

public boolean isCase(Object candidate)

call

public Object call()
Invokes the closure without any parameters, returning any value if applicable.

Returns:
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.

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

throwRuntimeException

protected static Object throwRuntimeException(Throwable throwable)

getOwner

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

getDelegate

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

setDelegate

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

Parameters:
delegate - the new delegate

getParameterTypes

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

getMaximumNumberOfParameters

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

asWritable

public Closure asWritable()
Returns:
a version of this closure which implements Writable

run

public void run()
Specified by:
run in interface Runnable

curry

public Closure curry(Object[] arguments)
Support for closure currying

Parameters:
arguments - the arguments to bind
Returns:
the new closure with its arguments bound

clone

public Object clone()
Overrides:
clone in class Object

getDirective

public int getDirective()
Returns:
Returns the directive.

setDirective

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

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