[SLC] provide light classes only for platforms with jvm target
Light classes are not supposed to work on non-jvm platforms due to its specific ^KT-60318 Fixed
This commit is contained in:
committed by
Space Team
parent
13a7bb95b3
commit
7fa7d8ea8d
+21
-12
@@ -21,13 +21,15 @@ import org.jetbrains.kotlin.fileClasses.isJvmMultifileClassFile
|
||||
import org.jetbrains.kotlin.fileClasses.javaFileFacadeFqName
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.psi.*
|
||||
import org.jetbrains.kotlin.utils.exceptions.errorWithAttachment
|
||||
import org.jetbrains.kotlin.utils.exceptions.withPsiEntry
|
||||
|
||||
abstract class KotlinAsJavaSupportBase<TModule>(protected val project: Project) : KotlinAsJavaSupport() {
|
||||
abstract class KotlinAsJavaSupportBase<TModule : Any>(protected val project: Project) : KotlinAsJavaSupport() {
|
||||
@Suppress("MemberVisibilityCanBePrivate")
|
||||
fun createLightFacade(file: KtFile): LightClassCachedValue<KtLightClassForFacade>? {
|
||||
if (!file.facadeIsPossible()) return null
|
||||
|
||||
val module = file.findModule().takeIf { facadeIsApplicable(it, file) } ?: return null
|
||||
val module = file.findModule()?.takeIf { facadeIsApplicable(it, file) } ?: return null
|
||||
val facadeFqName = file.javaFileFacadeFqName
|
||||
val facadeFiles = if (file.canHaveAdditionalFilesInFacade()) {
|
||||
findFilesForFacade(facadeFqName, module.contentSearchScope).filter(KtFile::isJvmMultifileClassFile)
|
||||
@@ -61,11 +63,11 @@ abstract class KotlinAsJavaSupportBase<TModule>(protected val project: Project)
|
||||
|
||||
private fun KtFile.canHaveAdditionalFilesInFacade(): Boolean = !isCompiled && isJvmMultifileClassFile
|
||||
|
||||
protected abstract fun KtFile.findModule(): TModule
|
||||
protected abstract fun KtFile.findModule(): TModule?
|
||||
protected abstract fun facadeIsApplicable(module: TModule, file: KtFile): Boolean
|
||||
protected abstract val TModule.contentSearchScope: GlobalSearchScope
|
||||
|
||||
protected abstract fun createInstanceOfLightFacade(facadeFqName: FqName, files: List<KtFile>): KtLightClassForFacade
|
||||
protected abstract fun createInstanceOfLightFacade(facadeFqName: FqName, files: List<KtFile>): KtLightClassForFacade?
|
||||
protected abstract fun createInstanceOfDecompiledLightFacade(facadeFqName: FqName, files: List<KtFile>): KtLightClassForFacade?
|
||||
|
||||
protected open fun projectWideOutOfBlockModificationTracker(): ModificationTracker {
|
||||
@@ -85,7 +87,12 @@ abstract class KotlinAsJavaSupportBase<TModule>(protected val project: Project)
|
||||
}
|
||||
|
||||
override fun createFacadeForSyntheticFile(file: KtFile): KtLightClassForFacade {
|
||||
return createInstanceOfLightFacade(file.javaFileFacadeFqName, listOf(file))
|
||||
return createInstanceOfLightFacade(file.javaFileFacadeFqName, listOf(file)) ?: errorWithAttachment(
|
||||
"Unsupported ${file::class.simpleName}"
|
||||
) {
|
||||
withEntry("module", file.findModule().toString())
|
||||
withPsiEntry("file", file)
|
||||
}
|
||||
}
|
||||
|
||||
override fun getFacadeClassesInPackage(packageFqName: FqName, scope: GlobalSearchScope): Collection<KtLightClassForFacade> {
|
||||
@@ -99,19 +106,21 @@ abstract class KotlinAsJavaSupportBase<TModule>(protected val project: Project)
|
||||
override fun getFacadeNames(packageFqName: FqName, scope: GlobalSearchScope): Collection<String> {
|
||||
return findFilesForFacadeByPackage(packageFqName, scope).mapNotNullTo(mutableSetOf()) { file ->
|
||||
file.takeIf { it.facadeIsPossible() }
|
||||
?.takeIf { facadeIsApplicable(it.findModule(), file) }
|
||||
?.takeIf { it.findModule()?.let { module -> facadeIsApplicable(module, file) } == true }
|
||||
?.javaFileFacadeFqName
|
||||
?.shortName()
|
||||
?.asString()
|
||||
}.toSet()
|
||||
}
|
||||
|
||||
private fun Collection<KtFile>.toFacadeClasses(): List<KtLightClassForFacade> = filter {
|
||||
it.facadeIsPossible()
|
||||
}.groupBy {
|
||||
FacadeKey(it.javaFileFacadeFqName, it.isJvmMultifileClassFile, it.findModule())
|
||||
}.mapNotNull { (key, files) ->
|
||||
files.firstOrNull { facadeIsApplicable(key.module, it) }?.let(::getLightFacade)
|
||||
private fun Collection<KtFile>.toFacadeClasses(): List<KtLightClassForFacade> = mapNotNull { file ->
|
||||
file.takeIf { it.facadeIsPossible() }?.findModule()?.let { file to it }
|
||||
}.groupBy { (file, module) ->
|
||||
FacadeKey(file.javaFileFacadeFqName, file.isJvmMultifileClassFile, module)
|
||||
}.mapNotNull { (_, pairs) ->
|
||||
pairs.firstNotNullOfOrNull { (file, module) ->
|
||||
file.takeIf { facadeIsApplicable(module, file) }
|
||||
}?.let(::getLightFacade)
|
||||
}
|
||||
|
||||
private data class FacadeKey<TModule>(val fqName: FqName, val isMultifile: Boolean, val module: TModule)
|
||||
|
||||
+23
-20
@@ -29,6 +29,8 @@ import org.jetbrains.kotlin.light.classes.symbol.classes.createSymbolLightClassN
|
||||
import org.jetbrains.kotlin.name.ClassId
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.name.parentOrNull
|
||||
import org.jetbrains.kotlin.platform.has
|
||||
import org.jetbrains.kotlin.platform.jvm.JvmPlatform
|
||||
import org.jetbrains.kotlin.psi.KtClassOrObject
|
||||
import org.jetbrains.kotlin.psi.KtElement
|
||||
import org.jetbrains.kotlin.psi.KtFile
|
||||
@@ -37,6 +39,11 @@ import org.jetbrains.kotlin.psi.KtScript
|
||||
class SymbolKotlinAsJavaSupport(project: Project) : KotlinAsJavaSupportBase<KtModule>(project) {
|
||||
private val projectStructureProvider by lazy { ProjectStructureProvider.getInstance(project) }
|
||||
|
||||
private fun PsiElement.getModuleIfSupportEnabled(): KtModule? = projectStructureProvider.getModule(
|
||||
element = this,
|
||||
contextualModule = null,
|
||||
).takeIf(KtModule::isLightClassesEnabled)
|
||||
|
||||
override fun findClassOrObjectDeclarationsInPackage(
|
||||
packageFqName: FqName,
|
||||
searchScope: GlobalSearchScope
|
||||
@@ -93,22 +100,18 @@ class SymbolKotlinAsJavaSupport(project: Project) : KotlinAsJavaSupportBase<KtMo
|
||||
.getKotlinOnlySubPackagesFqNames(fqn, nameFilter = { true })
|
||||
.map { fqn.child(it) }
|
||||
|
||||
override fun createInstanceOfLightScript(script: KtScript): KtLightClass {
|
||||
val module = ProjectStructureProvider.getModule(project, script, contextualModule = null)
|
||||
override fun createInstanceOfLightScript(script: KtScript): KtLightClass? {
|
||||
val module = script.getModuleIfSupportEnabled() ?: return null
|
||||
return SymbolLightClassForScript(script, module)
|
||||
}
|
||||
|
||||
override fun KtFile.findModule(): KtModule {
|
||||
return projectStructureProvider.getModule(this, contextualModule = null)
|
||||
}
|
||||
override fun KtFile.findModule(): KtModule? = getModuleIfSupportEnabled()
|
||||
|
||||
override fun declarationLocation(file: KtFile): DeclarationLocation? {
|
||||
return when (projectStructureProvider.getModule(file, contextualModule = null)) {
|
||||
is KtSourceModule -> DeclarationLocation.ProjectSources
|
||||
is KtLibraryModule -> DeclarationLocation.LibraryClasses
|
||||
is KtLibrarySourceModule -> DeclarationLocation.LibrarySources
|
||||
else -> null
|
||||
}
|
||||
override fun declarationLocation(file: KtFile): DeclarationLocation? = when (file.getModuleIfSupportEnabled()) {
|
||||
is KtSourceModule -> DeclarationLocation.ProjectSources
|
||||
is KtLibraryModule -> DeclarationLocation.LibraryClasses
|
||||
is KtLibrarySourceModule -> DeclarationLocation.LibrarySources
|
||||
else -> null
|
||||
}
|
||||
|
||||
override fun createInstanceOfDecompiledLightClass(classOrObject: KtClassOrObject): KtLightClass? {
|
||||
@@ -116,7 +119,7 @@ class SymbolKotlinAsJavaSupport(project: Project) : KotlinAsJavaSupportBase<KtMo
|
||||
}
|
||||
|
||||
override fun createInstanceOfLightClass(classOrObject: KtClassOrObject): KtLightClass? {
|
||||
val module = projectStructureProvider.getModule(classOrObject, contextualModule = null)
|
||||
val module = classOrObject.getModuleIfSupportEnabled() ?: return null
|
||||
return createSymbolLightClassNoCache(classOrObject, module)
|
||||
}
|
||||
|
||||
@@ -136,14 +139,15 @@ class SymbolKotlinAsJavaSupport(project: Project) : KotlinAsJavaSupportBase<KtMo
|
||||
return project.createAllLibrariesModificationTracker()
|
||||
}
|
||||
|
||||
override fun createInstanceOfLightFacade(facadeFqName: FqName, files: List<KtFile>): KtLightClassForFacade {
|
||||
val module = projectStructureProvider.getModule(files.first(), contextualModule = null)
|
||||
override fun createInstanceOfLightFacade(facadeFqName: FqName, files: List<KtFile>): KtLightClassForFacade? {
|
||||
val module = files.first().getModuleIfSupportEnabled() ?: return null
|
||||
return SymbolLightClassForFacade(facadeFqName, files, module)
|
||||
}
|
||||
|
||||
override val KtModule.contentSearchScope: GlobalSearchScope get() = this.contentScope
|
||||
|
||||
override fun facadeIsApplicable(module: KtModule, file: KtFile): Boolean = module.isFromSourceOrLibraryBinary()
|
||||
override fun facadeIsApplicable(module: KtModule, file: KtFile): Boolean =
|
||||
module.isFromSourceOrLibraryBinary() && module.isLightClassesEnabled()
|
||||
|
||||
override fun getKotlinInternalClasses(fqName: FqName, scope: GlobalSearchScope): Collection<PsiClass> {
|
||||
val facadeKtFiles = project.createDeclarationProvider(scope, null).findInternalFilesForFacade(fqName)
|
||||
@@ -176,10 +180,7 @@ class SymbolKotlinAsJavaSupport(project: Project) : KotlinAsJavaSupportBase<KtMo
|
||||
|
||||
override fun getFakeLightClass(classOrObject: KtClassOrObject): KtFakeLightClass = SymbolBasedFakeLightClass(classOrObject)
|
||||
|
||||
private fun KtElement.isFromSourceOrLibraryBinary(): Boolean {
|
||||
val module = projectStructureProvider.getModule(this, contextualModule = null)
|
||||
return module.isFromSourceOrLibraryBinary()
|
||||
}
|
||||
private fun KtElement.isFromSourceOrLibraryBinary(): Boolean = getModuleIfSupportEnabled()?.isFromSourceOrLibraryBinary() == true
|
||||
|
||||
private fun KtModule.isFromSourceOrLibraryBinary() = when (this) {
|
||||
is KtSourceModule -> true
|
||||
@@ -187,3 +188,5 @@ class SymbolKotlinAsJavaSupport(project: Project) : KotlinAsJavaSupportBase<KtMo
|
||||
else -> false
|
||||
}
|
||||
}
|
||||
|
||||
private fun KtModule.isLightClassesEnabled(): Boolean = platform.has<JvmPlatform>()
|
||||
|
||||
Reference in New Issue
Block a user