Do not resolve services in static initializers in IDEKotlinBinaryClassCache, KT-33973
This commit is contained in:
+8
-1
@@ -17,7 +17,9 @@
|
||||
package org.jetbrains.kotlin.idea.caches
|
||||
|
||||
import com.intellij.ide.highlighter.JavaClassFileType
|
||||
import com.intellij.openapi.components.Service
|
||||
import com.intellij.openapi.components.ServiceManager
|
||||
import com.intellij.openapi.components.service
|
||||
import com.intellij.openapi.util.Key
|
||||
import com.intellij.openapi.vfs.VirtualFile
|
||||
import com.intellij.openapi.vfs.VirtualFileWithId
|
||||
@@ -28,7 +30,8 @@ import org.jetbrains.kotlin.load.kotlin.header.KotlinClassHeader
|
||||
import org.jetbrains.kotlin.metadata.jvm.deserialization.JvmMetadataVersion
|
||||
import org.jetbrains.kotlin.name.ClassId
|
||||
|
||||
object IDEKotlinBinaryClassCache {
|
||||
@Service
|
||||
class IDEKotlinBinaryClassCache {
|
||||
class KotlinBinaryClassHeaderData(
|
||||
val classId: ClassId,
|
||||
val kind: KotlinClassHeader.Kind,
|
||||
@@ -132,4 +135,8 @@ object IDEKotlinBinaryClassCache {
|
||||
|
||||
return null
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun getInstance(): IDEKotlinBinaryClassCache = service()
|
||||
}
|
||||
}
|
||||
|
||||
+7
-4
@@ -41,9 +41,10 @@ val KEY = Key.create<IsKotlinBinary>(KOTLIN_COMPILED_FILE_ATTRIBUTE)
|
||||
* Checks if this file is a compiled Kotlin class file ABI-compatible with the current plugin
|
||||
*/
|
||||
fun isKotlinWithCompatibleAbiVersion(file: VirtualFile): Boolean {
|
||||
if (!IDEKotlinBinaryClassCache.isKotlinJvmCompiledFile(file)) return false
|
||||
val ideKotlinBinaryClassCache = IDEKotlinBinaryClassCache.getInstance()
|
||||
if (!ideKotlinBinaryClassCache.isKotlinJvmCompiledFile(file)) return false
|
||||
|
||||
val kotlinClass = IDEKotlinBinaryClassCache.getKotlinBinaryClassHeaderData(file)
|
||||
val kotlinClass = ideKotlinBinaryClassCache.getKotlinBinaryClassHeaderData(file)
|
||||
return kotlinClass != null && kotlinClass.metadataVersion.isCompatible()
|
||||
}
|
||||
|
||||
@@ -57,7 +58,9 @@ fun isKotlinInternalCompiledFile(file: VirtualFile, fileContent: ByteArray? = nu
|
||||
return false
|
||||
}
|
||||
|
||||
if (!IDEKotlinBinaryClassCache.isKotlinJvmCompiledFile(file, fileContent)) {
|
||||
val ideKotlinBinaryClassCache = IDEKotlinBinaryClassCache.getInstance()
|
||||
|
||||
if (!ideKotlinBinaryClassCache.isKotlinJvmCompiledFile(file, fileContent)) {
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -80,7 +83,7 @@ fun isKotlinInternalCompiledFile(file: VirtualFile, fileContent: ByteArray? = nu
|
||||
return true
|
||||
}
|
||||
|
||||
val header = IDEKotlinBinaryClassCache.getKotlinBinaryClassHeaderData(file, fileContent) ?: return false
|
||||
val header = ideKotlinBinaryClassCache.getKotlinBinaryClassHeaderData(file, fileContent) ?: return false
|
||||
if (header.classId.isLocal) return true
|
||||
|
||||
return header.kind == KotlinClassHeader.Kind.SYNTHETIC_CLASS ||
|
||||
|
||||
+4
-3
@@ -43,8 +43,9 @@ import org.jetbrains.kotlin.serialization.deserialization.descriptors.Deserializ
|
||||
import java.io.InputStream
|
||||
|
||||
fun DeserializerForClassfileDecompiler(classFile: VirtualFile): DeserializerForClassfileDecompiler {
|
||||
val kotlinClassHeaderInfo = IDEKotlinBinaryClassCache.getKotlinBinaryClassHeaderData(classFile)
|
||||
?: error("Decompiled data factory shouldn't be called on an unsupported file: " + classFile)
|
||||
val kotlinClassHeaderInfo =
|
||||
IDEKotlinBinaryClassCache.getInstance().getKotlinBinaryClassHeaderData(classFile)
|
||||
?: error("Decompiled data factory shouldn't be called on an unsupported file: $classFile")
|
||||
val packageFqName = kotlinClassHeaderInfo.classId.packageFqName
|
||||
return DeserializerForClassfileDecompiler(classFile.parent!!, packageFqName)
|
||||
}
|
||||
@@ -119,7 +120,7 @@ class DirectoryBasedClassFinder(
|
||||
val targetName = classId.relativeClassName.pathSegments().joinToString("$", postfix = ".class")
|
||||
val virtualFile = packageDirectory.findChild(targetName)
|
||||
if (virtualFile != null && isKotlinWithCompatibleAbiVersion(virtualFile)) {
|
||||
return IDEKotlinBinaryClassCache.getKotlinBinaryClass(virtualFile)?.let(::KotlinClass)
|
||||
return IDEKotlinBinaryClassCache.getInstance().getKotlinBinaryClass(virtualFile)?.let(::KotlinClass)
|
||||
}
|
||||
return null
|
||||
}
|
||||
|
||||
+4
-3
@@ -40,7 +40,7 @@ import org.jetbrains.kotlin.types.isFlexible
|
||||
class KotlinClassFileDecompiler : ClassFileDecompilers.Full() {
|
||||
private val stubBuilder = KotlinClsStubBuilder()
|
||||
|
||||
override fun accepts(file: VirtualFile) = IDEKotlinBinaryClassCache.isKotlinJvmCompiledFile(file)
|
||||
override fun accepts(file: VirtualFile) = IDEKotlinBinaryClassCache.getInstance().isKotlinJvmCompiledFile(file)
|
||||
|
||||
override fun getStubBuilder() = stubBuilder
|
||||
|
||||
@@ -67,8 +67,9 @@ fun buildDecompiledTextForClassFile(
|
||||
classFile: VirtualFile,
|
||||
resolver: ResolverForDecompiler = DeserializerForClassfileDecompiler(classFile)
|
||||
): DecompiledText {
|
||||
val classHeader = IDEKotlinBinaryClassCache.getKotlinBinaryClassHeaderData(classFile)
|
||||
?: error("Decompiled data factory shouldn't be called on an unsupported file: " + classFile)
|
||||
val classHeader =
|
||||
IDEKotlinBinaryClassCache.getInstance().getKotlinBinaryClassHeaderData(classFile)
|
||||
?: error("Decompiled data factory shouldn't be called on an unsupported file: $classFile")
|
||||
|
||||
val classId = classHeader.classId
|
||||
|
||||
|
||||
+2
-1
@@ -59,7 +59,8 @@ open class KotlinClsStubBuilder : ClsStubBuilder() {
|
||||
}
|
||||
|
||||
private fun doBuildFileStub(file: VirtualFile, fileContent: ByteArray): PsiFileStub<KtFile>? {
|
||||
val kotlinClass = IDEKotlinBinaryClassCache.getKotlinBinaryClass(file, fileContent) ?: error("Can't find binary class for Kotlin file: $file")
|
||||
val kotlinClass = IDEKotlinBinaryClassCache.getInstance().getKotlinBinaryClass(file, fileContent)
|
||||
?: error("Can't find binary class for Kotlin file: $file")
|
||||
val header = kotlinClass.classHeader
|
||||
val classId = kotlinClass.classId
|
||||
val packageFqName = header.packageName?.let { FqName(it) } ?: classId.packageFqName
|
||||
|
||||
@@ -92,7 +92,7 @@ object KotlinClassFileIndex : KotlinFileIndexBase<KotlinClassFileIndex>(KotlinCl
|
||||
private val VERSION = 3
|
||||
|
||||
private val INDEXER = indexer { fileContent ->
|
||||
val headerInfo = IDEKotlinBinaryClassCache.getKotlinBinaryClassHeaderData(fileContent.file, fileContent.content)
|
||||
val headerInfo = IDEKotlinBinaryClassCache.getInstance().getKotlinBinaryClassHeaderData(fileContent.file, fileContent.content)
|
||||
if (headerInfo != null && headerInfo.metadataVersion.isCompatible()) headerInfo.classId.asSingleFqName() else null
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user