Fix compilation of KotlinBytecodeToolWindow

This commit is contained in:
Dmitry Jemerov
2017-09-01 16:41:26 +02:00
parent 59976baac2
commit ad37626af3
3 changed files with 27 additions and 21 deletions
@@ -36,28 +36,33 @@ import java.util.jar.Manifest
class KotlinDecompilerServiceImpl : KotlinDecompilerService { class KotlinDecompilerServiceImpl : KotlinDecompilerService {
override fun decompile(file: KtFile): String? { override fun decompile(file: KtFile): String? {
val bytecodeMap = when { try {
file.canBeDecompiledToJava() -> bytecodeMapForExistingClassfile(file.virtualFile) val bytecodeMap = when {
!file.isCompiled -> bytecodeMapForSourceFile(file) file.canBeDecompiledToJava() -> bytecodeMapForExistingClassfile(file.virtualFile)
else -> return null !file.isCompiled -> bytecodeMapForSourceFile(file)
} else -> return null
val resultSaver = KotlinResultSaver() }
val options = hashMapOf<String, Any>( val resultSaver = KotlinResultSaver()
IFernflowerPreferences.REMOVE_BRIDGE to "0" val options = hashMapOf<String, Any>(
) IFernflowerPreferences.REMOVE_BRIDGE to "0"
)
val bytecodeProvider = IBytecodeProvider { val bytecodeProvider = IBytecodeProvider {
externalPath, _ -> externalPath, _ ->
val path = File(FileUtil.toSystemIndependentName(externalPath)) val path = File(FileUtil.toSystemIndependentName(externalPath))
bytecodeMap[path]?.invoke() bytecodeMap[path]?.invoke()
} }
val decompiler = BaseDecompiler(bytecodeProvider, resultSaver, options, IdeaLogger()) val decompiler = BaseDecompiler(bytecodeProvider, resultSaver, options, IdeaLogger())
for (path in bytecodeMap.keys) { for (path in bytecodeMap.keys) {
decompiler.addSpace(path, true) decompiler.addSpace(path, true)
}
decompiler.decompileContext()
return resultSaver.resultText
}
catch (ex: IdeaLogger.InternalException) {
throw DecompileFailedException(ex.message ?: "Unknown error", ex)
} }
decompiler.decompileContext()
return resultSaver.resultText
} }
private fun bytecodeMapForExistingClassfile(file: VirtualFile): Map<File, () -> ByteArray> { private fun bytecodeMapForExistingClassfile(file: VirtualFile): Map<File, () -> ByteArray> {
@@ -34,7 +34,6 @@ import com.intellij.openapi.util.text.StringUtil;
import com.intellij.openapi.wm.ToolWindow; import com.intellij.openapi.wm.ToolWindow;
import com.intellij.util.Alarm; import com.intellij.util.Alarm;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.java.decompiler.IdeaLogger;
import org.jetbrains.kotlin.backend.common.output.OutputFile; import org.jetbrains.kotlin.backend.common.output.OutputFile;
import org.jetbrains.kotlin.backend.common.output.OutputFileCollection; import org.jetbrains.kotlin.backend.common.output.OutputFileCollection;
import org.jetbrains.kotlin.backend.jvm.JvmIrCodegenFactory; import org.jetbrains.kotlin.backend.jvm.JvmIrCodegenFactory;
@@ -213,7 +212,7 @@ public class KotlinBytecodeToolWindow extends JPanel implements Disposable {
try { try {
KotlinDecompilerAdapterKt.showDecompiledCode(file); KotlinDecompilerAdapterKt.showDecompiledCode(file);
} }
catch (IdeaLogger.InternalException ex) { catch (DecompileFailedException ex) {
LOG.info(ex); LOG.info(ex);
Messages.showErrorDialog(myProject, "Failed to decompile " + file.getName() + ": " + ex, "Kotlin Bytecode Decompiler"); Messages.showErrorDialog(myProject, "Failed to decompile " + file.getName() + ": " + ex, "Kotlin Bytecode Decompiler");
} }
@@ -19,6 +19,8 @@ package org.jetbrains.kotlin.idea.internal
import com.intellij.openapi.components.ServiceManager import com.intellij.openapi.components.ServiceManager
import org.jetbrains.kotlin.psi.KtFile import org.jetbrains.kotlin.psi.KtFile
class DecompileFailedException(message: String, cause: Throwable) : RuntimeException(message, cause)
interface KotlinDecompilerService { interface KotlinDecompilerService {
fun decompile(file: KtFile): String? fun decompile(file: KtFile): String?