Skip to content

Commit

Permalink
Don't try/catch, instead don't attempt to read the language on non-cl…
Browse files Browse the repository at this point in the history
…asses
  • Loading branch information
sschr15 committed Aug 31, 2024
1 parent b2b2ebe commit e83039b
Showing 1 changed file with 17 additions and 22 deletions.
39 changes: 17 additions & 22 deletions src/main/kotlin/org/vineflower/ijplugin/VineflowerInvoker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package org.vineflower.ijplugin
import com.intellij.execution.filters.LineNumbersMapping
import com.intellij.ide.highlighter.JavaClassFileType
import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.diagnostic.ControlFlowException
import com.intellij.openapi.diagnostic.logger
import com.intellij.openapi.progress.ProcessCanceledException
import com.intellij.openapi.project.DumbService
Expand Down Expand Up @@ -251,30 +250,26 @@ class VineflowerInvoker(classLoader: ClassLoader) {
private val getCurrentDecompContext = classLoader.loadClass("org.jetbrains.java.decompiler.main.DecompilerContext")
.getMethod("getCurrentContext")

fun getLanguage(bytes: ByteArray): String {
try {
// ensure a decompilation context is available (otherwise the structclass constructor will fail)
if (getCurrentDecompContext.invoke(null) == null) {
val empty = emptyMap<Nothing, Nothing>()
val bytecodeProvider = myBytecodeProviderCtor.newInstance(empty)
val resultSaver = myResultSaverCtor.newInstance()
fun getLanguage(bytes: ByteArray): String? {
if (bytes.take(4) != listOf(0xCA, 0xFE, 0xBA, 0xBE).map { it.toByte() }.toByteArray()) {
// Non-class file
return null
}

baseDecompilerCtor.newInstance(bytecodeProvider, resultSaver, empty, myLogger)
}
// ensure a decompilation context is available (otherwise the structclass constructor will fail)
if (getCurrentDecompContext.invoke(null) == null) {
val empty = emptyMap<Nothing, Nothing>()
val bytecodeProvider = myBytecodeProviderCtor.newInstance(empty)
val resultSaver = myResultSaverCtor.newInstance()

val inputStream = dataInputFullStreamCtor.newInstance(bytes)
val structClass = structClassCreate.invoke(null, inputStream, true)
val pluginContext = getCurrentPluginContext.invoke(null)
val languageSpec = pluginContextGetLanguageSpec.invoke(pluginContext, structClass) ?: return "java"
return languageSpecName.get(languageSpec) as String
} catch (e: Throwable) {
if (e is ControlFlowException) {
throw e
}

LOGGER.warn("Failed to determine language of class file", e)
return "java"
baseDecompilerCtor.newInstance(bytecodeProvider, resultSaver, empty, myLogger)
}

val inputStream = dataInputFullStreamCtor.newInstance(bytes)
val structClass = structClassCreate.invoke(null, inputStream, true)
val pluginContext = getCurrentPluginContext.invoke(null)
val languageSpec = pluginContextGetLanguageSpec.invoke(pluginContext, structClass) ?: return "java"
return languageSpecName.get(languageSpec) as String
}
}
}

0 comments on commit e83039b

Please sign in to comment.