Reflection: support Java anonymous/local classes

Local classes and anonymous objects are normal classes and
kotlin-reflect can load all declarations and modifiers from them, and
support calling members, exactly in the same way as it does for normal
non-local classes.

 #KT-41373 In Progress
This commit is contained in:
Alexander Udalov
2023-03-28 01:11:49 +02:00
committed by Space Team
parent 1e031d9fb8
commit d833b732c9
3 changed files with 17 additions and 6 deletions
@@ -105,7 +105,14 @@ class ReflectJavaClass(
get() = null
override val name: Name
get() = Name.identifier(klass.simpleName)
get() = if (klass.isAnonymousClass) {
// For anonymous classes, `Class.simpleName` returns an empty string in Java reflection.
// We don't want that because it breaks all sorts of invariants on names which cannot be empty in Kotlin.
// So we extract the simple name from the full JVM binary name, e.g. "org.test.Foo$1" -> "Foo$1".
Name.identifier(klass.name.substringAfterLast("."))
} else {
Name.identifier(klass.simpleName)
}
override val typeParameters: List<ReflectJavaTypeParameter>
get() = klass.typeParameters.map(::ReflectJavaTypeParameter)