Sometimes I need to add an annotation to a field of a super class. To do this, I write this:
open class B {
open var x: Int = 0
}
class A : B() {
@get:SomeAnnotation
override var x: Int
get() = super.x
set(value) {
super.x = value
}
}
That’s a lot of code just to annotate an existing field, especially compared to the decorator version:
interface Api {
var x: Int
}
class B : Api {
override var x: Int = 0
}
class A(private val b: B) : Api by b {
@get:SomeAnnotation
override var x by b::x // short and simple
}
Unfortunately, I can’t use the delegation in example 1:
class A : B() {
@get:SomeAnnotation
override var x by super::x // doesn't work
}
Is there another possibility?