Don't write field signatures when not necessary

This commit is contained in:
Alexander Udalov
2015-10-07 19:42:35 +03:00
parent 864926ee2e
commit 934ffed944
10 changed files with 120 additions and 135 deletions
@@ -17,6 +17,7 @@
package kotlin.reflect.jvm.internal
import org.jetbrains.kotlin.descriptors.PropertyDescriptor
import org.jetbrains.kotlin.serialization.jvm.JvmProtoBufUtil
import java.lang.reflect.Field
import kotlin.reflect.jvm.internal.JvmPropertySignature.JavaField
import kotlin.reflect.jvm.internal.JvmPropertySignature.KotlinProperty
@@ -46,8 +47,9 @@ internal abstract class DescriptorBasedProperty<out R> protected constructor(
val jvmSignature = RuntimeTypeMapper.mapPropertySignature(descriptor)
when (jvmSignature) {
is KotlinProperty -> {
if (!jvmSignature.signature.hasField()) null
else container.findFieldBySignature(jvmSignature.proto, jvmSignature.signature.field, jvmSignature.nameResolver)
JvmProtoBufUtil.getJvmFieldSignature(jvmSignature.proto, jvmSignature.nameResolver)?.let {
container.findFieldBySignature(jvmSignature.proto, jvmSignature.nameResolver, it.name)
}
}
is JavaField -> jvmSignature.field
}
@@ -240,17 +240,14 @@ internal abstract class KDeclarationContainerImpl : ClassBasedDeclarationContain
// TODO: check resulting field's type
fun findFieldBySignature(
proto: ProtoBuf.Property,
signature: JvmProtoBuf.JvmFieldSignature,
nameResolver: NameResolver
nameResolver: NameResolver,
name: String
): Field? {
val name = nameResolver.getString(signature.getName())
val owner =
implClassForCallable(nameResolver, proto) ?:
if (signature.getIsStaticInOuter()) {
jClass.getEnclosingClass() ?: throw KotlinReflectionInternalError("Inconsistent metadata for field $name in $jClass")
}
else jClass
val owner = implClassForCallable(nameResolver, proto) ?:
if (proto.hasExtension(JvmProtoBuf.propertySignature) &&
proto.getExtension(JvmProtoBuf.propertySignature).let { it.hasField() && it.field.getIsStaticInOuter() }) {
jClass.enclosingClass ?: throw KotlinReflectionInternalError("Inconsistent metadata for field $name in $jClass")
} else jClass
return try {
owner.getDeclaredField(name)
@@ -92,6 +92,7 @@ internal sealed class JvmPropertySignature {
abstract fun asString(): String
class KotlinProperty(
val descriptor: PropertyDescriptor,
val proto: ProtoBuf.Property,
val signature: JvmProtoBuf.JvmPropertySignature,
val nameResolver: NameResolver
@@ -103,9 +104,10 @@ internal sealed class JvmPropertySignature {
string = nameResolver.getString(signature.getter.name) + nameResolver.getString(signature.getter.desc)
}
else {
string = JvmAbi.getterName(nameResolver.getString(signature.field.name)) +
"()" +
nameResolver.getString(signature.field.desc)
val (name, desc) =
JvmProtoBufUtil.getJvmFieldSignature(proto, nameResolver) ?:
throw KotlinReflectionInternalError("No field signature for property: $descriptor")
string = JvmAbi.getterName(name) + "()" + desc
}
}
@@ -170,7 +172,9 @@ internal object RuntimeTypeMapper {
if (!proto.hasExtension(JvmProtoBuf.propertySignature)) {
throw KotlinReflectionInternalError("No metadata found for $property")
}
return JvmPropertySignature.KotlinProperty(proto, proto.getExtension(JvmProtoBuf.propertySignature), property.nameResolver)
return JvmPropertySignature.KotlinProperty(
property, proto, proto.getExtension(JvmProtoBuf.propertySignature), property.nameResolver
)
}
else if (property is JavaPropertyDescriptor) {
val field = ((property.source as? JavaSourceElement)?.javaElement as? ReflectJavaField)?.member ?: