Improve reflection error message when function/property is not found

This commit is contained in:
Alexander Udalov
2018-07-02 18:39:48 +02:00
parent 4f2fdd1c01
commit e4af8dc7d8
@@ -20,6 +20,7 @@ import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.descriptors.impl.DeclarationDescriptorVisitorEmptyBodies import org.jetbrains.kotlin.descriptors.impl.DeclarationDescriptorVisitorEmptyBodies
import org.jetbrains.kotlin.load.java.JvmAbi import org.jetbrains.kotlin.load.java.JvmAbi
import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.renderer.DescriptorRenderer
import org.jetbrains.kotlin.resolve.scopes.MemberScope import org.jetbrains.kotlin.resolve.scopes.MemberScope
import java.lang.reflect.Constructor import java.lang.reflect.Constructor
import java.lang.reflect.Method import java.lang.reflect.Method
@@ -133,8 +134,12 @@ internal abstract class KDeclarationContainerImpl : ClassBasedDeclarationContain
return mostVisibleProperties.first() return mostVisibleProperties.first()
} }
val allMembers = getProperties(Name.identifier(name)).joinToString("\n") { descriptor ->
DescriptorRenderer.DEBUG_TEXT.render(descriptor) + " | " + RuntimeTypeMapper.mapPropertySignature(descriptor)
}
throw KotlinReflectionInternalError( throw KotlinReflectionInternalError(
"${properties.size} properties '$name' (JVM signature: $signature) resolved in $this: $properties" "Property '$name' (JVM signature: $signature) not resolved in $this:" +
if (allMembers.isEmpty()) " no members found" else "\n$allMembers"
) )
} }
@@ -142,16 +147,18 @@ internal abstract class KDeclarationContainerImpl : ClassBasedDeclarationContain
} }
fun findFunctionDescriptor(name: String, signature: String): FunctionDescriptor { fun findFunctionDescriptor(name: String, signature: String): FunctionDescriptor {
val functions = (if (name == "<init>") constructorDescriptors.toList() else getFunctions(Name.identifier(name))) val members = if (name == "<init>") constructorDescriptors.toList() else getFunctions(Name.identifier(name))
.filter { descriptor -> val functions = members.filter { descriptor ->
RuntimeTypeMapper.mapSignature(descriptor).asString() == signature RuntimeTypeMapper.mapSignature(descriptor).asString() == signature
} }
if (functions.size != 1) { if (functions.size != 1) {
val debugText = "'$name' (JVM signature: $signature)" val allMembers = members.joinToString("\n") { descriptor ->
DescriptorRenderer.DEBUG_TEXT.render(descriptor) + " | " + RuntimeTypeMapper.mapSignature(descriptor)
}
throw KotlinReflectionInternalError( throw KotlinReflectionInternalError(
if (functions.isEmpty()) "Function $debugText not resolved in $this" "Function '$name' (JVM signature: $signature) not resolved in $this:" +
else "${functions.size} functions $debugText resolved in $this: $functions" if (allMembers.isEmpty()) " no members found" else "\n$allMembers"
) )
} }