Refine generic signature for fields

- For vals use the same semantics as for return types
- For vars use the same semantics as for value parameters
This commit is contained in:
Denis Zharkov
2015-11-27 18:57:00 +03:00
parent 64e0af48ed
commit 303c756302
7 changed files with 72 additions and 22 deletions
@@ -886,7 +886,7 @@ public class ImplementationBodyCodegen extends ClassBodyCodegen {
int modifiers = ACC_STATIC | ACC_FINAL | ACC_PUBLIC | (property.isConst() ? 0 : ACC_DEPRECATED);
FieldVisitor fv = v.newField(JvmDeclarationOriginKt.Synthetic(DescriptorToSourceUtils.descriptorToDeclaration(property), property),
modifiers, context.getFieldName(property, false),
type.getDescriptor(), typeMapper.mapFieldSignature(property.getType()),
type.getDescriptor(), typeMapper.mapFieldSignature(property.getType(), property),
info.defaultValue);
AnnotationCodegen.forField(fv, typeMapper).genAnnotations(property, type);
@@ -360,7 +360,7 @@ public class PropertyCodegen {
v.getSerializationBindings().put(FIELD_FOR_PROPERTY, propertyDescriptor, Pair.create(type, name));
FieldVisitor fv = builder.newField(JvmDeclarationOriginKt.OtherOrigin(element, propertyDescriptor), modifiers, name, type.getDescriptor(),
typeMapper.mapFieldSignature(jetType), defaultValue);
typeMapper.mapFieldSignature(jetType, propertyDescriptor), defaultValue);
Annotated fieldAnnotated = new AnnotatedWithFakeAnnotations(propertyDescriptor, annotations);
AnnotationCodegen.forField(fv, typeMapper).genAnnotations(fieldAnnotated, type, AnnotationUseSiteTarget.FIELD);
@@ -325,20 +325,24 @@ public class JetTypeMapper {
//noinspection ConstantConditions
return mapType(descriptor.getReturnType(), sw, TypeMappingMode.GENERIC_TYPE);
}
else {
boolean isAnnotationMethod = DescriptorUtils.isAnnotationClass(descriptor.getContainingDeclaration());
TypeMappingMode typeMappingModeFromAnnotation =
TypeMappingUtil.extractTypeMappingModeFromAnnotation(descriptor, returnType, isAnnotationMethod);
if (typeMappingModeFromAnnotation != null) {
return mapType(returnType, sw, typeMappingModeFromAnnotation);
}
TypeMappingMode mappingMode = TypeMappingMode.getOptimalModeForReturnType(
returnType,
/* isAnnotationMethod = */ isAnnotationMethod);
return mapReturnType(descriptor, sw, returnType);
}
return mapType(returnType, sw, mappingMode);
@NotNull
private Type mapReturnType(@NotNull CallableDescriptor descriptor, @Nullable BothSignatureWriter sw, @NotNull KotlinType returnType) {
boolean isAnnotationMethod = DescriptorUtils.isAnnotationClass(descriptor.getContainingDeclaration());
TypeMappingMode typeMappingModeFromAnnotation =
TypeMappingUtil.extractTypeMappingModeFromAnnotation(descriptor, returnType, isAnnotationMethod);
if (typeMappingModeFromAnnotation != null) {
return mapType(returnType, sw, typeMappingModeFromAnnotation);
}
TypeMappingMode mappingMode = TypeMappingMode.getOptimalModeForReturnType(
returnType,
/* isAnnotationMethod = */ isAnnotationMethod);
return mapType(returnType, sw, mappingMode);
}
@NotNull
@@ -1108,9 +1112,16 @@ public class JetTypeMapper {
}
@Nullable
public String mapFieldSignature(@NotNull KotlinType backingFieldType) {
public String mapFieldSignature(@NotNull KotlinType backingFieldType, @NotNull PropertyDescriptor propertyDescriptor) {
BothSignatureWriter sw = new BothSignatureWriter(BothSignatureWriter.Mode.TYPE);
mapType(backingFieldType, sw, TypeMappingMode.DEFAULT);
if (!propertyDescriptor.isVar()) {
mapReturnType(propertyDescriptor, sw, backingFieldType);
}
else {
writeParameterType(sw, backingFieldType, propertyDescriptor);
}
return sw.makeJavaGenericSignature();
}
@@ -1211,6 +1222,16 @@ public class JetTypeMapper {
) {
sw.writeParameterType(kind);
writeParameterType(sw, type, callableDescriptor);
sw.writeParameterTypeEnd();
}
private void writeParameterType(
@NotNull BothSignatureWriter sw,
@NotNull KotlinType type,
@NotNull CallableDescriptor callableDescriptor
) {
TypeMappingMode typeMappingMode;
TypeMappingMode typeMappingModeFromAnnotation =
@@ -1227,8 +1248,6 @@ public class JetTypeMapper {
}
mapType(type, sw, typeMappingMode);
sw.writeParameterTypeEnd();
}
private static void writeParameter(@NotNull BothSignatureWriter sw, @NotNull JvmMethodParameterKind kind, @NotNull Type type) {
@@ -58,23 +58,23 @@ fun box(): String {
val classField3 = clz.getDeclaredField("classField3");
if (classField3.getGenericType().toString() != "Zout<? extends java.lang.String>")
if (classField3.getGenericType().toString() != "Zout<java.lang.String>")
return "fail3:" + classField3.getGenericType();
val classField4 = clz.getDeclaredField("classField4");
if (classField4.getGenericType().toString() != "Zin<? super TParam>")
if (classField4.getGenericType().toString() != "Zin<TParam>")
return "fail4:" + classField4.getGenericType();
val classField5 = clz.getDeclaredField("delegateLazy\$delegate");
if (classField5.getGenericType().toString() != "kotlin.Lazy<? extends Z<TParam>>")
if (classField5.getGenericType().toString() != "kotlin.Lazy<Z<TParam>>")
return "fail5:" + classField5.getGenericType();
val classField6 = clz.getDeclaredField("delegateNotNull\$delegate");
if (classField6.getGenericType().toString() != "kotlin.properties.ReadWriteProperty<? super java.lang.Object, Z<TParam>>")
if (classField6.getGenericType().toString() != "kotlin.properties.ReadWriteProperty<java.lang.Object, Z<TParam>>")
return "fail6:" + classField6.getGenericType();
@@ -3,7 +3,7 @@ public val mutList: MutableList<String> = throw Exception()
// field: OutInFieldKt::list
// jvm signature: Ljava/util/List;
// generic signature: Ljava/util/List<+Ljava/lang/String;>;
// generic signature: Ljava/util/List<Ljava/lang/String;>;
// field: OutInFieldKt::mutList
// jvm signature: Ljava/util/List;
@@ -0,0 +1,25 @@
class Out<out T>
class Final
open class Open
@JvmField
val NO_WILDCARDS: Out<Open> = Out()
// field: FieldsKt::NO_WILDCARDS
// generic signature: LOut<LOpen;>;
@JvmField
var HAS_WILDCARDS: Out<Open> = Out()
// field: FieldsKt::HAS_WILDCARDS
// generic signature: LOut<+LOpen;>;
@JvmField
var NO_WILDCARDS_VAR: Out<Final> = Out()
// field: FieldsKt::NO_WILDCARDS_VAR
// generic signature: LOut<LFinal;>;
@JvmSuppressWildcards
@JvmField
var SUPPRESSED: Out<Open> = Out()
// field: FieldsKt::SUPPRESSED
// generic signature: LOut<LOpen;>;
@@ -432,6 +432,12 @@ public class WriteSignatureTestGenerated extends AbstractWriteSignatureTest {
doTest(fileName);
}
@TestMetadata("fields.kt")
public void testFields() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/writeSignature/declarationSiteVariance/wildcardOptimization/fields.kt");
doTest(fileName);
}
@TestMetadata("finalReturnType.kt")
public void testFinalReturnType() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/writeSignature/declarationSiteVariance/wildcardOptimization/finalReturnType.kt");