Skip to content

Commit 5bd7918

Browse files
committed
fix(FileInsCommand): improve handling of compiled files for decompilation
1 parent 7640952 commit 5bd7918

File tree

1 file changed

+9
-10
lines changed
  • mpp-idea/mpp-idea-exts/devins-lang/src/main/kotlin/cc/unitmesh/devti/language/compiler/exec/file

1 file changed

+9
-10
lines changed

mpp-idea/mpp-idea-exts/devins-lang/src/main/kotlin/cc/unitmesh/devti/language/compiler/exec/file/FileInsCommand.kt

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ import cc.unitmesh.devti.util.relativePath
1313
import com.intellij.openapi.application.runReadAction
1414
import com.intellij.openapi.project.Project
1515
import com.intellij.openapi.vfs.VirtualFile
16-
import com.intellij.psi.PsiCompiledFile
1716
import com.intellij.psi.PsiManager
1817
import com.intellij.psi.search.FilenameIndex
1918
import com.intellij.psi.search.GlobalSearchScope
@@ -64,15 +63,15 @@ class FileInsCommand(private val myProject: Project, private val prop: String) :
6463
val psiFile = PsiManager.getInstance(myProject).findFile(virtualFile)
6564
val language = psiFile?.language?.displayName ?: ""
6665

67-
val fileContent = when (psiFile) {
68-
is PsiCompiledFile -> {
69-
// For compiled files (like .class files), get the decompiled text
70-
psiFile.decompiledPsiFile?.text ?: virtualFile.readText()
71-
}
72-
73-
else -> {
74-
virtualFile.readText()
75-
}
66+
// Try to get decompiled text for compiled files, otherwise read the file directly
67+
val fileContent = try {
68+
// For compiled files (like .class files), try to get the decompiled text
69+
// Use reflection to avoid ClassNotFoundException in newer IntelliJ versions
70+
val decompiledPsiFile = psiFile?.javaClass?.getMethod("getDecompiledPsiFile")?.invoke(psiFile)
71+
(decompiledPsiFile as? com.intellij.psi.PsiFile)?.text ?: virtualFile.readText()
72+
} catch (e: Exception) {
73+
// If decompilation fails or method doesn't exist, just read the file
74+
virtualFile.readText()
7675
}
7776

7877
Pair(fileContent, language)

0 commit comments

Comments
 (0)