[java decompiler] handling class format exceptions on the IDE side

(followup to PR #1560)

GitOrigin-RevId: eaf539eba28d1f37fc83969698d054ebf37f14f6
This commit is contained in:
Roman Shevchenko
2021-11-25 22:55:13 +01:00
committed by intellij-monorepo-bot
parent 2c4db88f5e
commit 01992bb753
2 changed files with 16 additions and 6 deletions

View File

@@ -28,6 +28,7 @@ import com.intellij.psi.impl.compiled.ClsFileImpl
import com.intellij.ui.components.LegalNoticeDialog
import com.intellij.util.FileContentUtilCore
import org.jetbrains.java.decompiler.main.decompiler.BaseDecompiler
import org.jetbrains.java.decompiler.main.extern.ClassFormatException
import org.jetbrains.java.decompiler.main.extern.IBytecodeProvider
import org.jetbrains.java.decompiler.main.extern.IFernflowerPreferences
import org.jetbrains.java.decompiler.main.extern.IResultSaver
@@ -165,7 +166,7 @@ class IdeaDecompiler : ClassFileDecompilers.Light() {
Logger.getInstance(IdeaDecompiler::class.java).warn(file.url, e)
return Strings.EMPTY_CHAR_SEQUENCE
}
ApplicationManager.getApplication().isUnitTestMode -> throw AssertionError(file.url, e)
ApplicationManager.getApplication().isUnitTestMode && e !is ClassFormatException -> throw AssertionError(file.url, e)
else -> throw CannotDecompileException(file.url, e)
}
}

View File

@@ -1,4 +1,4 @@
// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
// Copyright 2000-2021 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
package org.jetbrains.java.decompiler
import com.intellij.JavaTestUtil
@@ -188,20 +188,29 @@ class IdeaDecompilerTest : LightJavaCodeInsightFixtureTestCase() {
private fun getTestFile(name: String): VirtualFile {
val path = if (FileUtil.isAbsolute(name)) name else "${myFixture.testDataPath}/${name}"
val fs = if (path.contains(URLUtil.JAR_SEPARATOR)) StandardFileSystems.jar() else StandardFileSystems.local()
return fs.refreshAndFindFileByPath(path)!!
val file = fs.refreshAndFindFileByPath(path)!!
if (file.isDirectory) file.refresh(false, true)
return file
}
private class MyFileVisitor(private val psiManager: PsiManager) : VirtualFileVisitor<Any>() {
private val negativeTests = setOf("TestUnsupportedConstantPoolEntry")
override fun visitFile(file: VirtualFile): Boolean {
if (file.isDirectory) {
println(file.path)
}
else if (file.fileType === JavaClassFileType.INSTANCE && !file.name.contains('$')) {
val psiFile = psiManager.findFile(file)!!
val psiFile = psiManager.findFile(file)
if (psiFile !is ClsFileImpl) {
assertTrue("Psi file for ${file.name} should be an instance of ${ClsFileImpl::javaClass.name}", psiFile is ClsFileImpl)
throw AssertionError("PSI file for ${file.name} should be an instance of ${ClsFileImpl::javaClass.name}")
}
if (file.nameWithoutExtension in negativeTests) {
assertEquals("corrupted_class_file", psiFile.packageName)
return true
}
val decompiled = psiFile.mirror.text
assertTrue(file.path, decompiled.startsWith(IdeaDecompiler.BANNER) || file.name.endsWith("-info.class"))
@@ -224,4 +233,4 @@ class IdeaDecompilerTest : LightJavaCodeInsightFixtureTestCase() {
return true
}
}
}
}