Better Java interop for annotations

It would be nice to get some small quick wins for Java interop:

  1. If a Java annotation can be applied to a method, it should be applicable to a Kotlin property and go onto the getter method.
  2. If a Java annotation can be applied to a type it should be propagated to the bytecode properly.

Currently these two constructs which are very common with e.g. Bean Validation don’t work right:

@Foo val name: String
val name: @Bar String

The former doesn’t compile and the latter doesn’t affect generated bytecode so fixing these would be backwards compatible.

You can:

var setterVisibility: String = "abc"
    private set // the setter is private and has the default implementation

var setterWithAnnotation: Any? = null
    @Inject set // annotate the setter with Inject

I’d reommend to read the documentation.

See this

I know how to do it already, I’ve been using Kotlin for years. My post is a request to JetBrains for a slight improvement to Java interop - there are a lot of frameworks where they really want to annotate properties, but JavaBeans are the closest they have so they expect you to annotate the getter method instead (note, getter not setter). This can be done in Kotlin, it’s just ugly, and needlessly so.

I filed https://youtrack.jetbrains.com/issue/KT-35793 to track this.