3

TL;DR:

Is there a simple syntax in java to access kotlins backticked functions such as fun `if`(){...}

Long Version:

In Kotlin, one may write the following class.

class ShapeShifter {
    fun speak() { println("Hello fellow hooman") }
    fun `speakLikeA`() { println("Meow") }
    fun `speakLikeA`() { println("Bwoof !") }
    fun `speakLikeA`() { println("NOOT NOOT ! (you would'nt have so much problems with linux ...)") }
}

And this would work just fine ... All your fellow kotlin-ers would be able to speak to you in all your forms like so :

ShapeShifter().`speakLikeA`() // would work like a charm

but when interracting with java-ist muggles your secret identity would be safe because I am pretty sure java-ists can only interract with you like so :

new ShapeShifter().speak()

My question is : Is there a way for java commoners to reach to your backticked kotlin functions WITHOUT resorting to using black magics such as introspection/reflection like so :

var tomJedusor = new ShapeShifter();
ShapeShifter.class.getDeclaredMethod("speakLikeA").invoke(tomJedusor); //the forbidden arcane spell banned from Konoha ...
Ar3s
  • 2,237
  • 2
  • 25
  • 47
  • 2
    Strictly speaking, you can't: if the name of a class, method, or property in the bytecode isn't a valid Java identifier (if it's a language keyword, or has illegal characters), then any Java code trying to use it will fail to compile. Java provides no workarounds for that. — However, you can work around it from the Kotlin side, by changing the names in the bytecode; see ocos's answer. – gidds Jul 29 '22 at 12:56

1 Answers1

5

@JvmName annotation lets you define jvm name for that method.

@JvmName("speakLikeACat")
fun `speakLikeA`() { println("Meow") }

@JvmName("speakLikeADog")
fun `speak like a `() { println("Bwoof") }

Now, you can access that method from java code.

// .java 
shapeShifter.speakLikeACat();
shapeShifter.speakLikeADog();

Accesing backticked kotlin methods, fields from java is only possible with some jvm annotations. Kotlin compiler must obey java language specifications to generate the byte code. Actually java language specification does not allow this feature.

ocos
  • 1,901
  • 1
  • 10
  • 12
  • Nifty ! But let's assume that ```fun `speakLikeA`() { println("Meow") }``` is defined in a lib I have no control over ? Is there a way to access it purely from Java while retaining compile-time verification ? – Ar3s Jul 29 '22 at 12:50
  • 2
    As gidds explained, it is not possible. If a `pure kotlin library` is intended to be used also by java projects, the library design has to be `java` compatible. – ocos Jul 29 '22 at 13:19
  • Ok brilliant, this solves it for me then :) I was just wondering if there was no new/obscure thingy in java that this would have been an occasion for me to discover ... I guess there is no groundbreaking discovery planned today but more knowledge is already real nice on it's own. thank you and @gidds :) – Ar3s Jul 29 '22 at 13:43
  • If you could just edit a bit your answer to specifically state said impossibility I'd accept it :) – Ar3s Jul 29 '22 at 13:45
  • 3
    Unicode characters are legal in java, as long as they are in UTF-16, so `double π = Math.PI;` is valid. But is outside of the Unicode baseplane, so that's why it's illegal. – GeertPt Jul 29 '22 at 13:56