# Small Gem: @Delegate annotation

While looking through [some source code](https://github.com/jwagenleitner/groovy-wslite/blob/master/src/main/groovy/wslite/soap/SOAPFaultException.groovy#L19-L20)  of the open source library [groovy-wslite](https://github.com/jwagenleitner/groovy-wslite), I came across an annotation I had not seen before - `@Delegate`.  This annotation adds all public instance methods of the annotated field to the present class.    In the example from groovy-wslite, the field `soapResponse` is annotated with `@Delegate`:
```java
class SoapFaultException {
  @Delegate
  SoapResponse soapResponse
}
```
This means that all of `SoapResponse`'s public instance methods will be available as methods on all instances of `SoapFaultException`, and you can do the following:
```java
try {
  // do something
} catch (SoapFaultException e) {
  log.info(e.getFault())
}
```
Note that `getFault` isn't defined in `SoapFaultException`.  But it's defined in `SoapResponse` and since `@Delegate` is used, we can use it as if it's defined in `SoapFaultException`.  Behind the scenes, the above code translates to:
```java
try {
  // do something
} catch (SoapFaultException e) {
  log.info(e.soapResponse.getFault())
}
```

See more info about `@Delegate` [here](http://docs.groovy-lang.org/2.4.7/html/gapi/groovy/lang/Delegate.html).
