Minor: reformat and cleanup warnings in CollectionStubMethodGenerator.kt

This commit is contained in:
Dmitry Petrov
2020-09-28 14:02:50 +03:00
parent af1ed77d15
commit 565c156ddb
@@ -70,9 +70,9 @@ class CollectionStubMethodGenerator(
val superCollectionClasses = findRelevantSuperCollectionClasses() val superCollectionClasses = findRelevantSuperCollectionClasses()
if (superCollectionClasses.isEmpty()) return NO_TASKS if (superCollectionClasses.isEmpty()) return NO_TASKS
val existingMethodsInSuperclasses = descriptor.getAllSuperclassesWithoutAny().flatMap { val existingMethodsInSuperclasses = descriptor.getAllSuperclassesWithoutAny().flatMap { superClass ->
val tasksFromSuperClass = CollectionStubMethodGenerator(typeMapper, it).computeTasksToGenerate() val tasksFromSuperClass = CollectionStubMethodGenerator(typeMapper, superClass).computeTasksToGenerate()
(tasksFromSuperClass.methodStubsToGenerate + tasksFromSuperClass.syntheticStubsToGenerate).map { it.asmMethod } (tasksFromSuperClass.methodStubsToGenerate + tasksFromSuperClass.syntheticStubsToGenerate).map { stub -> stub.asmMethod }
} }
val methodStubsToGenerate = LinkedHashSet<JvmMethodGenericSignature>() val methodStubsToGenerate = LinkedHashSet<JvmMethodGenericSignature>()
@@ -132,7 +132,7 @@ class CollectionStubMethodGenerator(
val (asmMethod, valueParameters) = val (asmMethod, valueParameters) =
// if current method has special generic signature, // if current method has special generic signature,
// like `Collection.remove(E): Boolean` in Kotlin, use original signature to obtain `remove(Object)` // like `Collection.remove(E): Boolean` in Kotlin, use original signature to obtain `remove(Object)`
if (genericSignatureInfo?.isObjectReplacedWithTypeParameter ?: false) if (genericSignatureInfo?.isObjectReplacedWithTypeParameter == true)
Pair(originalSignature.asmMethod, originalSignature.valueParameters) Pair(originalSignature.asmMethod, originalSignature.valueParameters)
else else
Pair(overriddenMethodSignature.asmMethod, overriddenMethodSignature.valueParameters) Pair(overriddenMethodSignature.asmMethod, overriddenMethodSignature.valueParameters)
@@ -142,8 +142,7 @@ class CollectionStubMethodGenerator(
valueParameters, valueParameters,
specialGenericSignature specialGenericSignature
) )
} } else {
else {
method.signature() method.signature()
} }
@@ -154,15 +153,15 @@ class CollectionStubMethodGenerator(
// fun add(x: T) = true // fun add(x: T) = true
// } // }
// class B : A<String>() // No 'B.add(String)Z' // class B : A<String>() // No 'B.add(String)Z'
originalSignature.asmMethod !in existingMethodsInSuperclasses) { originalSignature.asmMethod !in existingMethodsInSuperclasses
) {
methodStubsToGenerate.add(commonSignature) methodStubsToGenerate.add(commonSignature)
if (originalSignature.asmMethod != commonSignature.asmMethod) { if (originalSignature.asmMethod != commonSignature.asmMethod) {
syntheticStubsToGenerate.add(originalSignature) syntheticStubsToGenerate.add(originalSignature)
} }
} }
} } else {
else {
// If the fake override is non-abstract, its implementation is already present in the class or inherited from one of its // If the fake override is non-abstract, its implementation is already present in the class or inherited from one of its
// super classes, but is not related to the MutableCollection hierarchy. So maybe it uses more specific return types // super classes, but is not related to the MutableCollection hierarchy. So maybe it uses more specific return types
// and we may need to generate some bridges // and we may need to generate some bridges
@@ -242,7 +241,8 @@ class CollectionStubMethodGenerator(
generateOverridesInAClass(klass, object : NonReportingOverrideStrategy() { generateOverridesInAClass(klass, object : NonReportingOverrideStrategy() {
override fun addFakeOverride(fakeOverride: CallableMemberDescriptor) { override fun addFakeOverride(fakeOverride: CallableMemberDescriptor) {
if (fakeOverride !is FunctionDescriptor) return if (fakeOverride !is FunctionDescriptor) return
val foundOverriddenFromDirectSuperClass = fakeOverride.findOverriddenFromDirectSuperClass(mutableCollectionTypeConstructor) ?: return val foundOverriddenFromDirectSuperClass =
fakeOverride.findOverriddenFromDirectSuperClass(mutableCollectionTypeConstructor) ?: return
if (foundOverriddenFromDirectSuperClass.kind == DECLARATION) { if (foundOverriddenFromDirectSuperClass.kind == DECLARATION) {
// For regular classes there should no be fake overrides having return types incompatible with return types of their // For regular classes there should no be fake overrides having return types incompatible with return types of their
// overridden, while here it's possible to create declaration like `fun remove(e: E): ImmutableCollection<E>` // overridden, while here it's possible to create declaration like `fun remove(e: E): ImmutableCollection<E>`
@@ -256,20 +256,26 @@ class CollectionStubMethodGenerator(
// so we must hack our subtyping a little bit // so we must hack our subtyping a little bit
val newDescriptor = val newDescriptor =
if (READ_ONLY_ARE_EQUAL_TO_MUTABLE_TYPE_CHECKER.isSubtypeOf( if (READ_ONLY_ARE_EQUAL_TO_MUTABLE_TYPE_CHECKER.isSubtypeOf(
fakeOverride.returnType!!, foundOverriddenFromDirectSuperClass.returnType!!)) fakeOverride.returnType!!,
foundOverriddenFromDirectSuperClass.returnType!!
)
)
fakeOverride fakeOverride
else else
foundOverriddenFromDirectSuperClass.copy( foundOverriddenFromDirectSuperClass.copy(
fakeOverride.containingDeclaration, fakeOverride.containingDeclaration,
foundOverriddenFromDirectSuperClass.modality, foundOverriddenFromDirectSuperClass.modality,
foundOverriddenFromDirectSuperClass.visibility, foundOverriddenFromDirectSuperClass.visibility,
fakeOverride.kind, false) fakeOverride.kind, false
)
newDescriptor.overriddenDescriptors = newDescriptor.overriddenDescriptors =
fakeOverride.overriddenDescriptors.filter { fakeOverride.overriddenDescriptors.filter { superDescriptor ->
superDescriptor ->
// filter out incompatible descriptors, e.g. `fun remove(e: E): ImmutableCollection<E>` for `fun remove(e: E): Boolean` // filter out incompatible descriptors, e.g. `fun remove(e: E): ImmutableCollection<E>` for `fun remove(e: E): Boolean`
READ_ONLY_ARE_EQUAL_TO_MUTABLE_TYPE_CHECKER.isSubtypeOf(newDescriptor.returnType!!, superDescriptor.returnType!!) READ_ONLY_ARE_EQUAL_TO_MUTABLE_TYPE_CHECKER.isSubtypeOf(
newDescriptor.returnType!!,
superDescriptor.returnType!!
)
} }
result.add(newDescriptor) result.add(newDescriptor)
@@ -338,7 +344,8 @@ class CollectionStubMethodGenerator(
return KotlinTypeFactory.simpleNotNullType(Annotations.EMPTY, classDescriptor, typeArguments) return KotlinTypeFactory.simpleNotNullType(Annotations.EMPTY, classDescriptor, typeArguments)
} }
private fun FunctionDescriptor.signature(): JvmMethodGenericSignature = typeMapper.mapSignatureWithGeneric(this, OwnerKind.IMPLEMENTATION) private fun FunctionDescriptor.signature(): JvmMethodGenericSignature =
typeMapper.mapSignatureWithGeneric(this, OwnerKind.IMPLEMENTATION)
private fun generateMethodStub(v: ClassBuilder, signature: JvmMethodGenericSignature, synthetic: Boolean) { private fun generateMethodStub(v: ClassBuilder, signature: JvmMethodGenericSignature, synthetic: Boolean) {
assert(descriptor.kind != ClassKind.INTERFACE) { "No stubs should be generated for interface ${descriptor.fqNameUnsafe}" } assert(descriptor.kind != ClassKind.INTERFACE) { "No stubs should be generated for interface ${descriptor.fqNameUnsafe}" }
@@ -351,7 +358,8 @@ class CollectionStubMethodGenerator(
AsmUtil.genThrow( AsmUtil.genThrow(
InstructionAdapter(mv), InstructionAdapter(mv),
"java/lang/UnsupportedOperationException", "java/lang/UnsupportedOperationException",
"Operation is not supported for read-only collection") "Operation is not supported for read-only collection"
)
FunctionCodegen.endVisit(mv, "built-in stub for $signature") FunctionCodegen.endVisit(mv, "built-in stub for $signature")
} }
} }