I’ve been writing more and more things in Kotlin that are intended to be accessible and natural for use from plain Java. In classes with many constants and the occasional static methods, it feels very repetitive to tag lots of companion object members with @JvmField and @JvmStatic.
Is there room for a more macro-level annotation to expose singleton objects as static for Java interop? Perhaps a new annotation that, placed on a object, will effectively mark all public val
s as @JvmField and all public fun
s as @JvmStatic.
Tricky parts might be ensuring some sane behavior if its placed on a non-object class (doesn’t look like there is an annotation target for objects only), and what to do if some scoundrel tries to makes a static var
(consistency points to letting a @JvmInterop modify var
s, sanity points to staying away from mutable static state).
In any case – the goal here is a just a boilerplate reduction. A companion object with a dozen constants and a helper method gets awfully messy with all the annotations, and more importantly, its easy to miss one member, requiring Java consumers to explicitly specify Type.INSTANCE.VALUE instead of Type.VALUE.