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 c6e27e8
Showing 1 changed file with 17 additions and 21 deletions.
38 changes: 17 additions & 21 deletions src/main/kotlin/org/vineflower/ijplugin/VineflowerInvoker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -251,30 +251,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 c6e27e8

Please sign in to comment.