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
@@ -69,10 +69,10 @@ message JvmMethodSignature {
}
message JvmFieldSignature {
required int32 name = 1 [(string_id_in_table) = true];
optional int32 name = 1 [(string_id_in_table) = true];
// JVM descriptor of the field type, e.g. 'Ljava/lang/String;'
required int32 desc = 2 [(string_id_in_table) = true];
optional int32 desc = 2 [(string_id_in_table) = true];
// True iff this field is a backing field for a companion object and is really present as a static
// field in the outer class, not as an instance field here
@@ -286,11 +286,9 @@ public abstract class AbstractBinaryClassAnnotationAndConstantLoader<A : Any, C
if (proto.hasExtension(propertySignature)) proto.getExtension(propertySignature)
else return null
if (field && signature.hasField()) {
return MemberSignature.fromFieldNameAndDesc(
nameResolver.getString(signature.field.name),
nameResolver.getString(signature.field.desc)
)
if (field) {
val (name, desc) = JvmProtoBufUtil.getJvmFieldSignature(proto, nameResolver) ?: return null
return MemberSignature.fromFieldNameAndDesc(name, desc)
}
else if (synthetic && signature.hasSyntheticMethod()) {
return MemberSignature.fromMethod(nameResolver, signature.syntheticMethod)
@@ -2306,19 +2306,19 @@ public final class JvmProtoBuf {
public interface JvmFieldSignatureOrBuilder
extends com.google.protobuf.MessageLiteOrBuilder {
// required int32 name = 1;
// optional int32 name = 1;
/**
* <code>required int32 name = 1;</code>
* <code>optional int32 name = 1;</code>
*/
boolean hasName();
/**
* <code>required int32 name = 1;</code>
* <code>optional int32 name = 1;</code>
*/
int getName();
// required int32 desc = 2;
// optional int32 desc = 2;
/**
* <code>required int32 desc = 2;</code>
* <code>optional int32 desc = 2;</code>
*
* <pre>
* JVM descriptor of the field type, e.g. 'Ljava/lang/String;'
@@ -2326,7 +2326,7 @@ public final class JvmProtoBuf {
*/
boolean hasDesc();
/**
* <code>required int32 desc = 2;</code>
* <code>optional int32 desc = 2;</code>
*
* <pre>
* JVM descriptor of the field type, e.g. 'Ljava/lang/String;'
@@ -2439,27 +2439,27 @@ public final class JvmProtoBuf {
}
private int bitField0_;
// required int32 name = 1;
// optional int32 name = 1;
public static final int NAME_FIELD_NUMBER = 1;
private int name_;
/**
* <code>required int32 name = 1;</code>
* <code>optional int32 name = 1;</code>
*/
public boolean hasName() {
return ((bitField0_ & 0x00000001) == 0x00000001);
}
/**
* <code>required int32 name = 1;</code>
* <code>optional int32 name = 1;</code>
*/
public int getName() {
return name_;
}
// required int32 desc = 2;
// optional int32 desc = 2;
public static final int DESC_FIELD_NUMBER = 2;
private int desc_;
/**
* <code>required int32 desc = 2;</code>
* <code>optional int32 desc = 2;</code>
*
* <pre>
* JVM descriptor of the field type, e.g. 'Ljava/lang/String;'
@@ -2469,7 +2469,7 @@ public final class JvmProtoBuf {
return ((bitField0_ & 0x00000002) == 0x00000002);
}
/**
* <code>required int32 desc = 2;</code>
* <code>optional int32 desc = 2;</code>
*
* <pre>
* JVM descriptor of the field type, e.g. 'Ljava/lang/String;'
@@ -2515,14 +2515,6 @@ public final class JvmProtoBuf {
byte isInitialized = memoizedIsInitialized;
if (isInitialized != -1) return isInitialized == 1;
if (!hasName()) {
memoizedIsInitialized = 0;
return false;
}
if (!hasDesc()) {
memoizedIsInitialized = 0;
return false;
}
memoizedIsInitialized = 1;
return true;
}
@@ -2710,14 +2702,6 @@ public final class JvmProtoBuf {
}
public final boolean isInitialized() {
if (!hasName()) {
return false;
}
if (!hasDesc()) {
return false;
}
return true;
}
@@ -2740,22 +2724,22 @@ public final class JvmProtoBuf {
}
private int bitField0_;
// required int32 name = 1;
// optional int32 name = 1;
private int name_ ;
/**
* <code>required int32 name = 1;</code>
* <code>optional int32 name = 1;</code>
*/
public boolean hasName() {
return ((bitField0_ & 0x00000001) == 0x00000001);
}
/**
* <code>required int32 name = 1;</code>
* <code>optional int32 name = 1;</code>
*/
public int getName() {
return name_;
}
/**
* <code>required int32 name = 1;</code>
* <code>optional int32 name = 1;</code>
*/
public Builder setName(int value) {
bitField0_ |= 0x00000001;
@@ -2764,7 +2748,7 @@ public final class JvmProtoBuf {
return this;
}
/**
* <code>required int32 name = 1;</code>
* <code>optional int32 name = 1;</code>
*/
public Builder clearName() {
bitField0_ = (bitField0_ & ~0x00000001);
@@ -2773,10 +2757,10 @@ public final class JvmProtoBuf {
return this;
}
// required int32 desc = 2;
// optional int32 desc = 2;
private int desc_ ;
/**
* <code>required int32 desc = 2;</code>
* <code>optional int32 desc = 2;</code>
*
* <pre>
* JVM descriptor of the field type, e.g. 'Ljava/lang/String;'
@@ -2786,7 +2770,7 @@ public final class JvmProtoBuf {
return ((bitField0_ & 0x00000002) == 0x00000002);
}
/**
* <code>required int32 desc = 2;</code>
* <code>optional int32 desc = 2;</code>
*
* <pre>
* JVM descriptor of the field type, e.g. 'Ljava/lang/String;'
@@ -2796,7 +2780,7 @@ public final class JvmProtoBuf {
return desc_;
}
/**
* <code>required int32 desc = 2;</code>
* <code>optional int32 desc = 2;</code>
*
* <pre>
* JVM descriptor of the field type, e.g. 'Ljava/lang/String;'
@@ -2809,7 +2793,7 @@ public final class JvmProtoBuf {
return this;
}
/**
* <code>required int32 desc = 2;</code>
* <code>optional int32 desc = 2;</code>
*
* <pre>
* JVM descriptor of the field type, e.g. 'Ljava/lang/String;'
@@ -3142,12 +3126,6 @@ public final class JvmProtoBuf {
byte isInitialized = memoizedIsInitialized;
if (isInitialized != -1) return isInitialized == 1;
if (hasField()) {
if (!getField().isInitialized()) {
memoizedIsInitialized = 0;
return false;
}
}
memoizedIsInitialized = 1;
return true;
}
@@ -3351,12 +3329,6 @@ public final class JvmProtoBuf {
}
public final boolean isInitialized() {
if (hasField()) {
if (!getField().isInitialized()) {
return false;
}
}
return true;
}
@@ -93,6 +93,22 @@ public object JvmProtoBufUtil {
return "<init>" + desc
}
fun getJvmFieldSignature(proto: ProtoBuf.Property, nameResolver: NameResolver): PropertySignature? {
val signature =
if (proto.hasExtension(JvmProtoBuf.propertySignature)) proto.getExtension(JvmProtoBuf.propertySignature) else return null
val field =
if (signature.hasField()) signature.field else null
val name = if (field != null && field.hasName()) field.name else proto.name
val desc =
if (field != null && field.hasDesc()) nameResolver.getString(field.desc)
else mapTypeDefault(proto.returnType, nameResolver) ?: return null
return PropertySignature(nameResolver.getString(name), desc)
}
data class PropertySignature(val name: String, val desc: String)
private fun mapTypeDefault(type: ProtoBuf.Type, nameResolver: NameResolver): String? {
return if (type.hasClassName()) mapClassIdDefault(nameResolver.getClassId(type.className)) else null
}
@@ -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 ?: