Missing enclosing class files causes `Class.getEnclosingClass` throw `NoClassDefFoundError`

Recently I noticed a weird kotlin compiler behavior: some enclosing class files are missing.

Let me try to elaborate on it. A sample can be found at GitHub - blindpirate/gradle-test-kotlin-extensions: Kotlin extensions for Gradle testing

  • Run ./gradlew test
  • javap -verbose 'build/classes/kotlin/test/com/mkobit/gradle/test/kotlin/io/FileContextTest$DirectoryContextTest$FileActionMaybeCreate$test$1$1$1$1.class'
EnclosingMethod: #54.#56                // com.mkobit.gradle.test.kotlin.io.FileContextTest$DirectoryContextTest$FileActionMaybeCreate$test$1$1$1.call

However, FileContextTest$DirectoryContextTest$FileActionMaybeCreate$test$1$1$1.class doesn’t exist at all.

The consequence is, when we try to analyze FileContextTest$DirectoryContextTest$FileActionMaybeCreate$test$1$1$1$1.class and invoke its getEnclosingClass, an exception will be thrown:

Caused by: java.lang.NoClassDefFoundError: com/mkobit/gradle/test/kotlin/io/FileContextTest$DirectoryContextTest$FileActionMaybeCreate$test$1$1$1
	at java.lang.Class.getEnclosingMethod0(Native Method)
	at java.lang.Class.getEnclosingMethodInfo(Class.java:1072)
	at java.lang.Class.getEnclosingClass(Class.java:1272)

Problem relates to https://youtrack.jetbrains.com/issue/KT-22663