SLC: add marker interface for collection inheritor

This commit is contained in:
Jinseong Jeon
2022-10-25 23:43:50 -07:00
committed by Ilya Kirillov
parent e15d8fc56f
commit 8d8d0d9922
8 changed files with 48 additions and 35 deletions
@@ -10,6 +10,8 @@ import com.intellij.psi.PsiElement
import com.intellij.psi.PsiJavaCodeReferenceElement
import com.intellij.psi.PsiManager
import com.intellij.psi.PsiReferenceList
import org.jetbrains.kotlin.builtins.jvm.JavaToKotlinClassMap
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.psi.KtSuperTypeList
import org.jetbrains.kotlin.psi.psiUtil.getElementTextWithContext
@@ -54,4 +56,25 @@ class KotlinSuperTypeListBuilder(kotlinOrigin: KtSuperTypeList?, manager: PsiMan
return element
}
fun addMarkerInterfaceIfNeeded(classId: ClassId) {
tryResolveMarkerInterfaceFQName(classId)?.let { addReference(it) }
}
/***
* @see org.jetbrains.kotlin.codegen.ImplementationBodyCodegen
*/
private fun tryResolveMarkerInterfaceFQName(classId: ClassId): String? {
for (mapping in JavaToKotlinClassMap.mutabilityMappings) {
if (mapping.kotlinReadOnly == classId) {
return "kotlin.jvm.internal.markers.KMappedMarker"
} else if (mapping.kotlinMutable == classId) {
return "kotlin.jvm.internal.markers.K" + classId.relativeClassName.asString()
.replace("MutableEntry", "Entry") // kotlin.jvm.internal.markers.KMutableMap.Entry for some reason
.replace(".", "$")
}
}
return null
}
}
@@ -416,14 +416,26 @@ internal fun SymbolLightClassBase.createInheritanceList(forExtendsList: Boolean,
return forExtendsList == !isJvmInterface
}
//TODO Add support for kotlin.collections.
superTypes.asSequence()
.filter { it.needToAddTypeIntoList() }
.mapNotNull { type ->
if (type !is KtNonErrorClassType) return@mapNotNull null
mapType(type, this@createInheritanceList, KtTypeMappingMode.SUPER_TYPE)
.forEach { superType ->
if (superType !is KtNonErrorClassType) return@forEach
val mappedType =
mapType(superType, this@createInheritanceList, KtTypeMappingMode.SUPER_TYPE_KOTLIN_COLLECTIONS_AS_IS)
?: return@forEach
listBuilder.addReference(mappedType)
if (mappedType.canonicalText.startsWith("kotlin.collections.")) {
val mappedToNoCollectionAsIs = mapType(superType, this@createInheritanceList, KtTypeMappingMode.SUPER_TYPE)
if (mappedToNoCollectionAsIs != null &&
mappedType.canonicalText != mappedToNoCollectionAsIs.canonicalText
) {
// Add java supertype
listBuilder.addReference(mappedToNoCollectionAsIs)
// Add marker interface
listBuilder.addMarkerInterfaceIfNeeded(superType.classId)
}
}
}
.forEach { listBuilder.addReference(it) }
return listBuilder
}