Implements interfaces by forwarding method calls to an annotated field or method.
interface I {
def String m()
}
class Foo implements I {
override String m() {
"Foo"
}
}
class Bar implements I {
//This will generate a method m(), which calls foo.m()
@Delegate val foo = new Foo
}
For each interface that the declaring class and the delegate have in common,
an implementation for each method is added if it does not yet exist. This
implementation forwards all calls directly to the delegate. You can restrict
which interfaces to implement using the
Class
[] value of this
annotation. This is especially useful when there are several delegates that
have some interfaces in common.
Delegate methods can either take
- no arguments
- the name of the method to be called (String)
- the name of the method to be called (String), its parameter types
(Class[]) and the actual arguments (Object[]) of the call
This allows you to generate meaningful error messages or to dynamically
dispatch based on the arguments.