# Better Java interop for annotations

**URL:** <https://discuss.kotlinlang.org/t/better-java-interop-for-annotations/15663>\
**Category:** Language Design\
**Created:** [December 30, 2019, 6:47pm UTC](https://discuss.kotlinlang.org/t/better-java-interop-for-annotations/15663 "2019-12-30T18:47:17Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mikehearn](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/mikehearn/32/1424_2.png) [@mikehearn](https://discuss.kotlinlang.org/u/mikehearn)\
**Post date:** [December 30, 2019, 6:47pm UTC](https://discuss.kotlinlang.org/t/better-java-interop-for-annotations/15663/1 "2019-12-30T18:47:17Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![lamba92](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/lamba92/32/5089_2.png) [@lamba92](https://discuss.kotlinlang.org/u/lamba92)\
**Post date:** [December 30, 2019, 7:03pm UTC](https://discuss.kotlinlang.org/t/better-java-interop-for-annotations/15663/2 "2019-12-30T19:03:43Z")

</div>

You can:

```kotlin
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](https://kotlinlang.org/docs/reference/properties.html).

---

<div class="post-metadata">

**Author:** ![Varia](https://avatars.discourse-cdn.com/v4/letter/v/b9e5f3/32.png) [@Varia](https://discuss.kotlinlang.org/u/Varia)\
**Post date:** [December 30, 2019, 9:58pm UTC](https://discuss.kotlinlang.org/t/better-java-interop-for-annotations/15663/3 "2019-12-30T21:58:31Z")

</div>

See [this](https://kotlinlang.org/docs/reference/annotations.html#annotation-use-site-targets)

---

<div class="post-metadata">

**Author:** ![mikehearn](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/mikehearn/32/1424_2.png) [@mikehearn](https://discuss.kotlinlang.org/u/mikehearn)\
**Post date:** [January 2, 2020, 8:08pm UTC](https://discuss.kotlinlang.org/t/better-java-interop-for-annotations/15663/4 "2020-01-02T20:08:16Z")

</div>

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](https://youtrack.jetbrains.com/issue/KT-35793) to track this.
