[FE] Support sealed classes and interfaces from java

KT-43551
KT-41215
This commit is contained in:
Dmitriy Novozhilov
2020-11-19 17:48:25 +03:00
committed by TeamCityServer
parent bdfb71b149
commit 7897bb6adb
9 changed files with 260 additions and 10 deletions
@@ -15,12 +15,13 @@ enum class Modality {
ABSTRACT;
companion object {
// NB: never returns SEALED
fun convertFromFlags(abstract: Boolean, open: Boolean): Modality {
if (abstract) return ABSTRACT
if (open) return OPEN
return FINAL
fun convertFromFlags(sealed: Boolean, abstract: Boolean, open: Boolean): Modality {
return when {
sealed -> SEALED
abstract -> ABSTRACT
open -> OPEN
else -> FINAL
}
}
}
}
@@ -76,7 +76,7 @@ class LazyJavaClassDescriptor(
private val modality =
if (jClass.isAnnotationType || jClass.isEnum) Modality.FINAL
else Modality.convertFromFlags(jClass.isAbstract || jClass.isInterface, !jClass.isFinal)
else Modality.convertFromFlags(jClass.isSealed, jClass.isAbstract || jClass.isInterface, !jClass.isFinal)
private val visibility = jClass.visibility
private val isInner = jClass.outerClass != null && !jClass.isStatic
@@ -180,7 +180,14 @@ class LazyJavaClassDescriptor(
fun wasScopeContentRequested() =
getUnsubstitutedMemberScope().wasContentRequested() || staticScope.wasContentRequested()
override fun getSealedSubclasses(): Collection<ClassDescriptor> = emptyList()
override fun getSealedSubclasses(): Collection<ClassDescriptor> = if (modality == Modality.SEALED) {
val attributes = TypeUsage.COMMON.toAttributes()
jClass.permittedTypes.mapNotNull {
c.typeResolver.transformJavaType(it, attributes).constructor.declarationDescriptor as? ClassDescriptor
}
} else {
emptyList()
}
override fun toString() = "Lazy Java class ${this.fqNameUnsafe}"
@@ -515,7 +515,7 @@ class LazyJavaClassMemberScope(
returnType,
// Those functions are generated as open in bytecode
// Actually, it should not be important because the class is final anyway, but leaving them open is convenient for consistency
Modality.convertFromFlags(abstract = false, open = true),
Modality.convertFromFlags(sealed = false, abstract = false, open = true),
DescriptorVisibilities.PUBLIC,
null,
)
@@ -181,7 +181,7 @@ abstract class LazyJavaScope(
effectiveSignature.typeParameters,
effectiveSignature.valueParameters,
effectiveSignature.returnType,
Modality.convertFromFlags(method.isAbstract, !method.isFinal),
Modality.convertFromFlags(sealed = false, method.isAbstract, !method.isFinal),
method.visibility.toDescriptorVisibility(),
if (effectiveSignature.receiverType != null)
mapOf(JavaMethodDescriptor.ORIGINAL_VALUE_PARAMETER_FOR_EXTENSION_RECEIVER to valueParameters.descriptors.first())