2

We're converting a lot of java static methods to kotlin extension functions. However, there are some methods that we need to keep around JUST FOR JAVA (We want to force kotlin code to use the extension functions).

Is there a way to hide java static methods from kotlin?

Zoe
  • 27,060
  • 21
  • 118
  • 148
Psest328
  • 6,575
  • 11
  • 55
  • 90

2 Answers2

6

You can annotate those methods with a @Deprecated annotation with deprecation level HIDDEN:

@kotlin.Deprecated(message = "JUST FOR JAVA", level = DeprecationLevel.HIDDEN)
public void foo() {
}
yole
  • 92,896
  • 20
  • 260
  • 197
  • This doesn't seem to work for me. This make it invisible to Java too. Any other suggestions? – Chris Watts Oct 30 '18 at 12:06
  • You can try `DeprecationLevel.ERROR` – yole Oct 30 '18 at 12:36
  • I don't think there's another solution. – yole Oct 30 '18 at 17:04
  • One thing I tried was to mark the functions as `internal` in Kotlin, and then set the name of the function manually with @JvmName("myJavaOnlyFunction"), which works, but IDEs like JetBrains will complain that you're using an internally-marked function. – Chris Watts Oct 30 '18 at 18:38
5

There is a solution. It's a bit of a hack, but works perfectly:

@kotlin.SinceKotlin(version = "99999.0")
public void foo() {
}
Anton Tananaev
  • 2,458
  • 1
  • 25
  • 48