From c6e27e8ecb6269886cf185410b05deb1a2cd2b1e Mon Sep 17 00:00:00 2001 From: sschr15 Date: Sat, 31 Aug 2024 14:58:42 -0500 Subject: [PATCH] Don't try/catch, instead don't attempt to read the language on non-classes --- .../vineflower/ijplugin/VineflowerInvoker.kt | 38 +++++++++---------- 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/src/main/kotlin/org/vineflower/ijplugin/VineflowerInvoker.kt b/src/main/kotlin/org/vineflower/ijplugin/VineflowerInvoker.kt index 61a74b6..533025c 100644 --- a/src/main/kotlin/org/vineflower/ijplugin/VineflowerInvoker.kt +++ b/src/main/kotlin/org/vineflower/ijplugin/VineflowerInvoker.kt @@ -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() - 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() + 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 } } }