Top Description Fields Constructors Methods
groovy.util

public abstract Class DelegatingScript

extends Script
Class Inheritance
Imports
groovy.lang.Binding, .GroovyObject, .MetaClass, .MissingMethodException, .MissingPropertyException, .Script, org.codehaus.groovy.runtime.InvokerHelper

Script that performs method invocations and property access like groovy.lang.Closure does.

DelegatingScript is a convenient basis for loading a custom-defined DSL as a Script, then execute it. The following sample code illustrates how to do it:

class MyDSL {
    public void foo(int x, int y, Closure z) { ... }
    public void setBar(String a) { ... }
}

CompilerConfiguration cc = new CompilerConfiguration();
cc.setScriptBaseClass(DelegatingScript.class.getName());
GroovyShell sh = new GroovyShell(cl,new Binding(),cc);
DelegatingScript script = (DelegatingScript)sh.parse(new File("my.dsl"))
script.setDelegate(new MyDSL());
script.run();

my.dsl can look like this:

foo(1,2) {
    ....
}
bar = ...;

DelegatingScript does this by delegating property access and method invocation to the delegate object.

More formally speaking, given the following script:

a = 1;
b(2);

Using DelegatingScript as the base class, the code will run as:

delegate.a = 1;
delegate.b(2);
... whereas in plain Script, this will be run as:
binding.setProperty("a",1);
((Closure)binding.getProperty("b")).call(2);

Field Summary

Modifier and TypeField and Description
private Object
private MetaClass

Constructor Summary

AccessConstructor and Description
protected
protected

Method Summary

Modifier and TypeMethod and Description
public Object
public Object
getProperty(String
the name of the property of interest
property
)

Overrides groovy.lang.Script.getProperty.

Overrides default groovy.lang.GroovyObject.getProperty.

Retrieves a property value.
public Object
invokeMethod(String
method to call
name
,
Object
arguments to pass to the method
args
)

Overrides groovy.lang.Script.invokeMethod.

Overrides default groovy.lang.GroovyObject.invokeMethod.

Invoke a method (or closure in the binding) defined.
public void
setDelegate(Object delegate)

Sets the delegation target.

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

Overrides groovy.lang.Script.setProperty.

Overrides default groovy.lang.GroovyObject.setProperty.

Sets the given property to the new value.
Inherited from groovy.lang.Script:
evaluateevaluategetBindingprintprintfprintfprintlnprintlnrunrunsetBinding

Field Detail

delegateback to summary
private Object delegate
metaClassback to summary
private MetaClass metaClass

Hides groovy.lang.GroovyObjectSupport.metaClass.

Constructor Detail

DelegatingScriptback to summary
protected DelegatingScript()
DelegatingScriptback to summary
protected DelegatingScript(Binding binding)

Method Detail

getDelegateback to summary
public Object getDelegate()
getPropertyback to summary
public Object getProperty(String property)

Overrides groovy.lang.Script.getProperty.

Overrides default groovy.lang.GroovyObject.getProperty.

Doc from groovy.lang.GroovyObject.getProperty.

Retrieves a property value.

Parameters
property:String

the name of the property of interest

Returns:Object

the given property

Annotations
@Override
invokeMethodback to summary
public Object invokeMethod(String name, Object args)

Overrides groovy.lang.Script.invokeMethod.

Overrides default groovy.lang.GroovyObject.invokeMethod.

Doc from groovy.lang.Script.invokeMethod.

Invoke a method (or closure in the binding) defined.

Parameters
name:String

method to call

args:Object

arguments to pass to the method

Returns:Object

value

Annotations
@Override
setDelegateback to summary
public void setDelegate(Object delegate)

Sets the delegation target.

setPropertyback to summary
public void setProperty(String property, Object newValue)

Overrides groovy.lang.Script.setProperty.

Overrides default groovy.lang.GroovyObject.setProperty.

Doc from groovy.lang.GroovyObject.setProperty.

Sets the given property to the new value.

Parameters
property:String

the name of the property of interest

newValue:Object

the new value for the property

Annotations
@Override