[light classes] move facade creation logic to KotlinLightClassFactory
^KT-53097
This commit is contained in:
@@ -36,7 +36,7 @@ class CliKotlinAsJavaSupport(
|
|||||||
return findFacadeFilesInPackage(packageFqName, scope)
|
return findFacadeFilesInPackage(packageFqName, scope)
|
||||||
.groupBy { it.javaFileFacadeFqName }
|
.groupBy { it.javaFileFacadeFqName }
|
||||||
.mapNotNull { (facadeClassFqName, _) ->
|
.mapNotNull { (facadeClassFqName, _) ->
|
||||||
KtLightClassForFacadeImpl.createForFacade(psiManager, facadeClassFqName, scope)
|
KotlinLightClassFactory.createFacade(psiManager, facadeClassFqName, scope)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -53,7 +53,7 @@ class CliKotlinAsJavaSupport(
|
|||||||
.orEmpty()
|
.orEmpty()
|
||||||
|
|
||||||
override fun getFacadeClasses(facadeFqName: FqName, scope: GlobalSearchScope): Collection<PsiClass> {
|
override fun getFacadeClasses(facadeFqName: FqName, scope: GlobalSearchScope): Collection<PsiClass> {
|
||||||
return listOfNotNull(KtLightClassForFacadeImpl.createForFacade(psiManager, facadeFqName, scope))
|
return listOfNotNull(KotlinLightClassFactory.createFacade(psiManager, facadeFqName, scope))
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun getScriptClasses(scriptFqName: FqName, scope: GlobalSearchScope): Collection<PsiClass> {
|
override fun getScriptClasses(scriptFqName: FqName, scope: GlobalSearchScope): Collection<PsiClass> {
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2010-2019 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
* Copyright 2010-2022 JetBrains s.r.o. and Kotlin Programming Language contributors.
|
||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@@ -27,7 +27,7 @@ class FacadeCache(private val project: Project) {
|
|||||||
private inner class FacadeCacheData {
|
private inner class FacadeCacheData {
|
||||||
val cache = object : SLRUCache<FacadeCacheKey, ValueWrapper>(20, 30) {
|
val cache = object : SLRUCache<FacadeCacheKey, ValueWrapper>(20, 30) {
|
||||||
override fun createValue(key: FacadeCacheKey): ValueWrapper =
|
override fun createValue(key: FacadeCacheKey): ValueWrapper =
|
||||||
KtLightClassForFacadeImpl.createForFacadeNoCache(key.fqName, key.searchScope, project)
|
KotlinLightClassFactory.createFacadeNoCache(key.fqName, key.searchScope, project)
|
||||||
?.let { ValueWrapper(it) }
|
?.let { ValueWrapper(it) }
|
||||||
?: ValueWrapper.Null
|
?: ValueWrapper.Null
|
||||||
}
|
}
|
||||||
|
|||||||
+40
-1
@@ -3,16 +3,22 @@
|
|||||||
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
@file:Suppress("unused")
|
@file:Suppress("unused", "MemberVisibilityCanBePrivate")
|
||||||
|
|
||||||
package org.jetbrains.kotlin.asJava.classes
|
package org.jetbrains.kotlin.asJava.classes
|
||||||
|
|
||||||
|
import com.intellij.openapi.project.Project
|
||||||
|
import com.intellij.psi.PsiManager
|
||||||
|
import com.intellij.psi.search.GlobalSearchScope
|
||||||
import com.intellij.psi.util.CachedValueProvider
|
import com.intellij.psi.util.CachedValueProvider
|
||||||
import com.intellij.psi.util.CachedValuesManager
|
import com.intellij.psi.util.CachedValuesManager
|
||||||
import org.jetbrains.kotlin.analyzer.KotlinModificationTrackerService
|
import org.jetbrains.kotlin.analyzer.KotlinModificationTrackerService
|
||||||
|
import org.jetbrains.kotlin.asJava.KotlinAsJavaSupport
|
||||||
import org.jetbrains.kotlin.asJava.LightClassGenerationSupport
|
import org.jetbrains.kotlin.asJava.LightClassGenerationSupport
|
||||||
|
import org.jetbrains.kotlin.name.FqName
|
||||||
import org.jetbrains.kotlin.psi.KtClassOrObject
|
import org.jetbrains.kotlin.psi.KtClassOrObject
|
||||||
import org.jetbrains.kotlin.psi.KtCodeFragment
|
import org.jetbrains.kotlin.psi.KtCodeFragment
|
||||||
|
import org.jetbrains.kotlin.psi.KtFile
|
||||||
|
|
||||||
object KotlinLightClassFactory {
|
object KotlinLightClassFactory {
|
||||||
fun createClass(classOrObject: KtClassOrObject): KtLightClassForSourceDeclaration? =
|
fun createClass(classOrObject: KtClassOrObject): KtLightClassForSourceDeclaration? =
|
||||||
@@ -38,4 +44,37 @@ object KotlinLightClassFactory {
|
|||||||
createUltraLightClass(classOrObject)
|
createUltraLightClass(classOrObject)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fun createFacadeNoCache(fqName: FqName, searchScope: GlobalSearchScope, project: Project): KtLightClassForFacade? {
|
||||||
|
val sources = KotlinAsJavaSupport.getInstance(project)
|
||||||
|
.findFilesForFacade(fqName, searchScope)
|
||||||
|
.filterNot { it.isCompiled || it.isScript() }
|
||||||
|
|
||||||
|
if (sources.isEmpty()) return null
|
||||||
|
val manager = PsiManager.getInstance(project)
|
||||||
|
return LightClassGenerationSupport.getInstance(project).run {
|
||||||
|
if (!canCreateUltraLightClassForFacade(sources)) return null
|
||||||
|
createUltraLightClassForFacade(manager, fqName, sources)
|
||||||
|
?: error("Unable to create UL class for facade: $fqName for ${sources.joinToString { it.virtualFilePath }}")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fun createFacade(
|
||||||
|
manager: PsiManager,
|
||||||
|
facadeClassFqName: FqName,
|
||||||
|
searchScope: GlobalSearchScope
|
||||||
|
): KtLightClassForFacade? {
|
||||||
|
return FacadeCache.getInstance(manager.project)[facadeClassFqName, searchScope]
|
||||||
|
}
|
||||||
|
|
||||||
|
fun createFacadeForSyntheticFile(
|
||||||
|
facadeClassFqName: FqName,
|
||||||
|
file: KtFile
|
||||||
|
): KtLightClassForFacade {
|
||||||
|
val project = file.project
|
||||||
|
val manager = PsiManager.getInstance(project)
|
||||||
|
return LightClassGenerationSupport.getInstance(project).run {
|
||||||
|
createUltraLightClassForFacade(manager, facadeClassFqName, listOf(file)) ?: error { "Unable to create UL class for facade" }
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
-39
@@ -5,16 +5,12 @@
|
|||||||
|
|
||||||
package org.jetbrains.kotlin.asJava.classes
|
package org.jetbrains.kotlin.asJava.classes
|
||||||
|
|
||||||
import com.intellij.openapi.project.Project
|
|
||||||
import com.intellij.openapi.util.TextRange
|
import com.intellij.openapi.util.TextRange
|
||||||
import com.intellij.psi.*
|
import com.intellij.psi.*
|
||||||
import com.intellij.psi.impl.PsiSuperMethodImplUtil
|
import com.intellij.psi.impl.PsiSuperMethodImplUtil
|
||||||
import com.intellij.psi.impl.light.LightEmptyImplementsList
|
import com.intellij.psi.impl.light.LightEmptyImplementsList
|
||||||
import com.intellij.psi.impl.light.LightModifierList
|
import com.intellij.psi.impl.light.LightModifierList
|
||||||
import com.intellij.psi.search.GlobalSearchScope
|
|
||||||
import org.jetbrains.annotations.NonNls
|
import org.jetbrains.annotations.NonNls
|
||||||
import org.jetbrains.kotlin.asJava.KotlinAsJavaSupport
|
|
||||||
import org.jetbrains.kotlin.asJava.LightClassGenerationSupport
|
|
||||||
import org.jetbrains.kotlin.asJava.elements.FakeFileForLightClass
|
import org.jetbrains.kotlin.asJava.elements.FakeFileForLightClass
|
||||||
import org.jetbrains.kotlin.fileClasses.JvmFileClassUtil
|
import org.jetbrains.kotlin.fileClasses.JvmFileClassUtil
|
||||||
import org.jetbrains.kotlin.fileClasses.javaFileFacadeFqName
|
import org.jetbrains.kotlin.fileClasses.javaFileFacadeFqName
|
||||||
@@ -210,39 +206,4 @@ abstract class KtLightClassForFacadeImpl constructor(
|
|||||||
override fun isWritable() = files.all { it.isWritable }
|
override fun isWritable() = files.all { it.isWritable }
|
||||||
|
|
||||||
override fun getVisibleSignatures(): MutableCollection<HierarchicalMethodSignature> = PsiSuperMethodImplUtil.getVisibleSignatures(this)
|
override fun getVisibleSignatures(): MutableCollection<HierarchicalMethodSignature> = PsiSuperMethodImplUtil.getVisibleSignatures(this)
|
||||||
|
|
||||||
companion object {
|
|
||||||
|
|
||||||
fun createForFacadeNoCache(fqName: FqName, searchScope: GlobalSearchScope, project: Project): KtLightClassForFacade? {
|
|
||||||
val sources = KotlinAsJavaSupport.getInstance(project).findFilesForFacade(fqName, searchScope)
|
|
||||||
.filterNot { it.isCompiled || it.isScript() }
|
|
||||||
|
|
||||||
if (sources.isEmpty()) return null
|
|
||||||
val manager = PsiManager.getInstance(project)
|
|
||||||
return LightClassGenerationSupport.getInstance(project).run {
|
|
||||||
if (!canCreateUltraLightClassForFacade(sources)) return null
|
|
||||||
createUltraLightClassForFacade(manager, fqName, sources)
|
|
||||||
?: error("Unable to create UL class for facade: $fqName for ${sources.joinToString { it.virtualFilePath }}")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fun createForFacade(
|
|
||||||
manager: PsiManager,
|
|
||||||
facadeClassFqName: FqName,
|
|
||||||
searchScope: GlobalSearchScope
|
|
||||||
): KtLightClassForFacade? {
|
|
||||||
return FacadeCache.getInstance(manager.project)[facadeClassFqName, searchScope]
|
|
||||||
}
|
|
||||||
|
|
||||||
fun createForSyntheticFile(
|
|
||||||
facadeClassFqName: FqName,
|
|
||||||
file: KtFile
|
|
||||||
): KtLightClassForFacade {
|
|
||||||
val project = file.project
|
|
||||||
val manager = PsiManager.getInstance(project)
|
|
||||||
return LightClassGenerationSupport.getInstance(project).run {
|
|
||||||
createUltraLightClassForFacade(manager, facadeClassFqName, listOf(file)) ?: error { "Unable to create UL class for facade" }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user