Annotation Interface Delegate


@Beta @GwtCompatible @Target({FIELD,METHOD}) @Documented public @interface Delegate
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.
Since:
2.7
  • Optional Element Summary

    Optional Elements
    Modifier and Type
    Optional Element
    Description
    Class<?>[]
    Optional list of interfaces that this delegate is restricted to.
  • Element Details

    • value

      Class<?>[] value
      Optional list of interfaces that this delegate is restricted to. Defaults to the common interfaces of the context type and the annotated element.
      Default:
      {}