I have come across the @JvmSynthetic
annotation in kotlin-stdlib, and I'm wondering what it is for, but, unfortunately, it is undocumented. (UPD: it was at that moment)
As far as I understand, applying it to a program element will add the synthetic
modifier to the corresponding bytecode elements. As a consequence, the element becomes invisible from Java:
class MyClass {
@JvmSynthetic
fun f() { }
}
Somewhere in Java code:
MyClass c = new MyClass();
c.f() // Error: cannot resolve method f()
But the same elements are still visible in Kotlin code:
val c = MyClass()
c.f() // OK
Is hiding declarations from non-Kotlin sources a valid use of @JvmSynthetic
? Is it the intended use? What are the other appropriate use cases?
Since @JvmSynthetic
hides functions from Java, they cannot be overridden in Java either (and when it comes to an abstract
member, the calls then result into AbstractMethodError
). Given that, can I use @JvmSynthetic
to prohibit overriding members of a Kotlin class in Java sources?
@PublishedApi internal
, i. e. accessible in inline functions. – Ambriz