Skip to content

Commit

Permalink
KSP1: Try to invalidate KotlinCliJavaFileManagerImpl.topLevelClassesC…
Browse files Browse the repository at this point in the history
…ache

See JetBrains/kotlin#5369 for more details.

(cherry picked from commit f12968a)
  • Loading branch information
ting-yuan committed Oct 16, 2024
1 parent 57f5d46 commit a989599
Showing 1 changed file with 14 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,10 @@ import com.intellij.openapi.vfs.StandardFileSystems
import com.intellij.openapi.vfs.VirtualFileManager
import com.intellij.psi.PsiJavaFile
import com.intellij.psi.PsiManager
import com.intellij.psi.impl.file.impl.JavaFileManager
import org.jetbrains.kotlin.analyzer.AnalysisResult
import org.jetbrains.kotlin.cli.common.messages.CompilerMessageSeverity
import org.jetbrains.kotlin.cli.jvm.compiler.KotlinCliJavaFileManagerImpl
import org.jetbrains.kotlin.config.JvmAnalysisFlags
import org.jetbrains.kotlin.config.LanguageVersionSettings
import org.jetbrains.kotlin.container.ComponentProvider
Expand Down Expand Up @@ -157,6 +159,8 @@ abstract class AbstractKotlinSymbolProcessingExtension(
val psiManager = PsiManager.getInstance(project)
if (initialized) {
psiManager.dropPsiCaches()
psiManager.dropResolveCaches()
invalidateKotlinCliJavaFileManagerCache(project)
} else {
// In case of broken builds.
if (javaShadowBase.exists()) {
Expand Down Expand Up @@ -503,3 +507,13 @@ fun findTargetInfos(languageVersionSettings: LanguageVersionSettings, module: Mo
else -> UnknownPlatformInfoImpl(platform.toString())
}
} ?: emptyList()

// FIXME: remove as soon as possible.
private fun invalidateKotlinCliJavaFileManagerCache(project: Project): Boolean {
val javaFileManager = (JavaFileManager.getInstance(project) as? KotlinCliJavaFileManagerImpl) ?: return false
val privateCacheField = KotlinCliJavaFileManagerImpl::class.java.getDeclaredField("topLevelClassesCache")
if (!privateCacheField.trySetAccessible())
return false
(privateCacheField.get(javaFileManager) as? MutableMap<*, *>)?.clear() ?: return false
return true
}

0 comments on commit a989599

Please sign in to comment.