[lc] introduce KotlinAsJavaSupportBase with new common facade logic
^KT-53543
This commit is contained in:
+1
-5
@@ -33,11 +33,7 @@ abstract class LightClassGenerationSupport {
|
||||
ConstantExpressionEvaluator(moduleDescriptor, languageVersionSettings, expression.project)
|
||||
}
|
||||
|
||||
internal fun canCreateUltraLightClassForFacade(files: Collection<KtFile>): Boolean = files.none { it.isScript() }
|
||||
|
||||
fun createUltraLightClassForFacade(facadeClassFqName: FqName, files: Collection<KtFile>): KtUltraLightClassForFacade? {
|
||||
if (!canCreateUltraLightClassForFacade(files)) return null
|
||||
|
||||
fun createUltraLightClassForFacade(facadeClassFqName: FqName, files: Collection<KtFile>): KtUltraLightClassForFacade {
|
||||
val filesToSupports: List<Pair<KtFile, KtUltraLightSupport>> = files.map {
|
||||
it to getUltraLightClassSupport(it)
|
||||
}
|
||||
|
||||
@@ -1,56 +0,0 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.asJava.classes
|
||||
|
||||
import com.intellij.openapi.components.ServiceManager
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.psi.search.GlobalSearchScope
|
||||
import com.intellij.psi.util.CachedValue
|
||||
import com.intellij.psi.util.CachedValueProvider
|
||||
import com.intellij.psi.util.CachedValuesManager
|
||||
import com.intellij.util.containers.SLRUCache
|
||||
import org.jetbrains.kotlin.analyzer.KotlinModificationTrackerService
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
|
||||
private data class FacadeCacheKey(val fqName: FqName, val searchScope: GlobalSearchScope)
|
||||
|
||||
private data class ValueWrapper(val value: KtLightClassForFacade?) {
|
||||
companion object {
|
||||
val Null = ValueWrapper(null)
|
||||
}
|
||||
}
|
||||
|
||||
class FacadeCache(private val project: Project) {
|
||||
private inner class FacadeCacheData {
|
||||
val cache = object : SLRUCache<FacadeCacheKey, ValueWrapper>(20, 30) {
|
||||
override fun createValue(key: FacadeCacheKey): ValueWrapper =
|
||||
KotlinK1LightClassFactory.createFacadeNoCache(key.fqName, key.searchScope, project)
|
||||
?.let { ValueWrapper(it) }
|
||||
?: ValueWrapper.Null
|
||||
}
|
||||
}
|
||||
|
||||
private val cachedValue: CachedValue<FacadeCacheData> = CachedValuesManager.getManager(project).createCachedValue(
|
||||
{
|
||||
CachedValueProvider.Result.create(
|
||||
FacadeCacheData(),
|
||||
KotlinModificationTrackerService.getInstance(project).outOfBlockModificationTracker
|
||||
)
|
||||
}, false
|
||||
)
|
||||
|
||||
operator fun get(qualifiedName: FqName, searchScope: GlobalSearchScope): KtLightClassForFacade? {
|
||||
synchronized(cachedValue) {
|
||||
return cachedValue.value.cache.get(FacadeCacheKey(qualifiedName, searchScope)).value
|
||||
}
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun getInstance(project: Project): FacadeCache {
|
||||
return ServiceManager.getService(project, FacadeCache::class.java)
|
||||
}
|
||||
}
|
||||
}
|
||||
+2
-36
@@ -5,17 +5,12 @@
|
||||
|
||||
package org.jetbrains.kotlin.asJava.classes
|
||||
|
||||
import com.intellij.openapi.project.Project
|
||||
import com.intellij.psi.search.GlobalSearchScope
|
||||
import com.intellij.psi.util.CachedValueProvider
|
||||
import com.intellij.psi.util.CachedValuesManager
|
||||
import org.jetbrains.kotlin.analyzer.KotlinModificationTrackerService
|
||||
import org.jetbrains.kotlin.asJava.KotlinAsJavaSupport
|
||||
import org.jetbrains.kotlin.asJava.LightClassGenerationSupport
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.psi.KtClassOrObject
|
||||
import org.jetbrains.kotlin.psi.KtCodeFragment
|
||||
import org.jetbrains.kotlin.psi.KtFile
|
||||
import org.jetbrains.kotlin.psi.KtScript
|
||||
|
||||
class KotlinK1LightClassFactory : KotlinLightClassFactory {
|
||||
@@ -27,18 +22,6 @@ class KotlinK1LightClassFactory : KotlinLightClassFactory {
|
||||
)
|
||||
}
|
||||
|
||||
override fun createFacade(
|
||||
project: Project,
|
||||
facadeClassFqName: FqName,
|
||||
searchScope: GlobalSearchScope,
|
||||
): KtLightClassForFacade? = FacadeCache.getInstance(project)[facadeClassFqName, searchScope]
|
||||
|
||||
override fun createFacadeForSyntheticFile(facadeClassFqName: FqName, file: KtFile): KtLightClassForFacade {
|
||||
return LightClassGenerationSupport.getInstance(file.project).run {
|
||||
createUltraLightClassForFacade(facadeClassFqName, listOf(file)) ?: error { "Unable to create UL class for facade" }
|
||||
}
|
||||
}
|
||||
|
||||
override fun createScript(script: KtScript): KtLightClassForScript? = CachedValuesManager.getCachedValue(script) {
|
||||
CachedValueProvider.Result.create(
|
||||
createScriptNoCache(script),
|
||||
@@ -54,9 +37,7 @@ class KotlinK1LightClassFactory : KotlinLightClassFactory {
|
||||
return null
|
||||
}
|
||||
|
||||
return LightClassGenerationSupport.getInstance(script.project).run {
|
||||
createUltraLightClassForScript(script)
|
||||
}
|
||||
return LightClassGenerationSupport.getInstance(script.project).createUltraLightClassForScript(script)
|
||||
}
|
||||
|
||||
fun createClassNoCache(classOrObject: KtClassOrObject): KtLightClassForSourceDeclaration? {
|
||||
@@ -70,22 +51,7 @@ class KotlinK1LightClassFactory : KotlinLightClassFactory {
|
||||
return null
|
||||
}
|
||||
|
||||
return LightClassGenerationSupport.getInstance(classOrObject.project).run {
|
||||
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
|
||||
return LightClassGenerationSupport.getInstance(project).run {
|
||||
if (!canCreateUltraLightClassForFacade(sources)) return null
|
||||
createUltraLightClassForFacade(fqName, sources)
|
||||
?: error("Unable to create UL class for facade: $fqName for ${sources.joinToString { it.virtualFilePath }}")
|
||||
}
|
||||
return LightClassGenerationSupport.getInstance(classOrObject.project).createUltraLightClass(classOrObject)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user