7

In Kotlin, it's possible to name a method using back-ticks like this:

fun `i am a test method`(){
   Assert.assertEquals("x", "x")
}

The compiler generates a method with underscores instead of blanks: "i_am_a_test_method", which seems reasonable as the JVM does not permit methods with blanks afaik. How can Junit and/or Gradle report these tests with the back-ticked name though?

s1m0nw1
  • 76,759
  • 17
  • 167
  • 196

1 Answers1

9

In a Java method descriptor, several characters have a special meaning, namely [ ( ) / and ;. The space doesn't have any special meaning, and therefore it can be used directly in a method name; that's exactly what the compiler does. The spaces are not converted to underscores.

yole
  • 92,896
  • 20
  • 260
  • 197
  • 4
    A Java decompiler tries to produce valid Java source code from a given class file. In a Java source file, there is no way to represent spaces in a method name, so the decompiler replaces them with underscores. To understand how the Kotlin compiler really works, you need to look at the bytecode, not at the decompiler output. – yole Aug 18 '17 at 07:31
  • 1
    javap shows spaces in method names, it produces `public final void testing LookupCodes();` for `fun \`testing LookupCodes\`()` – Muhammad Hewedy Jun 08 '18 at 12:14
  • 1
    Is there any way to call these methods from java without using reflection? – herman Jul 31 '19 at 13:20
  • @herman No, such methods can only be called through reflection. – yole Aug 05 '19 at 15:46