-
Notifications
You must be signed in to change notification settings - Fork 7
Delegate and Implements annotation
Aki edited this page Jul 27, 2018
·
1 revision
Fields can be annotated with a @LDelegate
annotation. It takes interface classes as parameter.
The class that contains the field will implement all those interfaces by delegating all methods to the annotated field. The field must therefore be of a type that implements all those interfaces.
Interfaces extended by delegated interface will also get delegated.
public interface Button {
Color getColor();
void setColor(Color color);
boolean isPressed();
void setPressed(boolean pressed);
}
Here's the usage of the annotation
public class ButtonBox {
@LDelegate({ Button.class })
private BasicButton button;
public ButtonBox(BasicButton button) {
this.button = button;
}
}
This will be compiled to the following
public class ButtonBox implements Button {
@LDelegate({ Button.class })
private BasicButton button;
public ButtonBox(BasicButton button) {
this.button = button;
}
public Color getColor() {
return button.getColor();
}
public void setColor(Color color) {
button.setColor(color);
}
public boolean isPressed() {
return button.isPressed();
}
public void setPressed(boolean pressed) {
button.setPressed(pressed);
}
}
The @LDelegate
annotation delegates all methods in the interfaces, but what if you want a custom implementation for one one of those?
Just create the method you want to implement and annotate it with a @LImplements
annotation!
Here's an example:
public class ButtonBox {
@LDelegate({ Button.class })
private BasicButton button;
public ButtonBox(BasicButton button) {
this.button = button;
}
@LImplements
public Color getColor() {
return Color.RED;
}
}