|
| 1 | +package org.vineflower.ijplugin |
| 2 | + |
| 3 | +import com.intellij.application.options.CodeStyle |
| 4 | +import com.intellij.openapi.application.ApplicationManager |
| 5 | +import com.intellij.openapi.util.text.StringUtil |
| 6 | +import com.intellij.ui.DocumentAdapter |
| 7 | +import com.intellij.ui.JBIntSpinner |
| 8 | +import com.intellij.ui.components.JBCheckBox |
| 9 | +import com.intellij.ui.components.JBTextField |
| 10 | +import java.lang.reflect.Field |
| 11 | +import java.lang.reflect.Modifier |
| 12 | +import java.util.* |
| 13 | +import javax.swing.event.DocumentEvent |
| 14 | + |
| 15 | +object ClassicVineflowerPreferences : VineflowerPreferences() { |
| 16 | + val ignoredPreferences = setOf( |
| 17 | + "ban", // banner |
| 18 | + "bsm", // bytecode source mapping |
| 19 | + "nls", // newline separator |
| 20 | + "__unit_test_mode__", |
| 21 | + "log", // log level |
| 22 | + "urc", // use renamer class |
| 23 | + "thr", // threads |
| 24 | + "mpm", // max processing method |
| 25 | + "\r\n", // irrelevant constant |
| 26 | + "\n", // irrelevant constant |
| 27 | + ) |
| 28 | + |
| 29 | + val defaultOverrides = mapOf( |
| 30 | + "hdc" to "0", // hide default constructor |
| 31 | + "dgs" to "1", // decompile generic signatures |
| 32 | + "rsy" to "1", // remove synthetic |
| 33 | + "rbr" to "1", // remove bridge |
| 34 | + "nls" to "1", // newline separator |
| 35 | + "ban" to "//\n// Source code recreated from a .class file by Vineflower\n//\n\n", // banner |
| 36 | + "mpm" to 0, // max processing method |
| 37 | + "iib" to "1", // ignore invalid bytecode |
| 38 | + "vac" to "1", // verify anonymous classes |
| 39 | + "ind" to CodeStyle.getDefaultSettings().indentOptions.INDENT_SIZE, // indent size |
| 40 | + "__unit_test_mode__" to if (ApplicationManager.getApplication().isUnitTestMode) "1" else "0", |
| 41 | + ) |
| 42 | + |
| 43 | + override fun setupSettings(entries: MutableList<SettingsEntry>, settingsMap: MutableMap<String, String>) { |
| 44 | + val classLoader = VineflowerState.getInstance().getVineflowerClassLoader().getNow(null) ?: return |
| 45 | + val preferencesClass = classLoader.loadClass("org.jetbrains.java.decompiler.main.extern.IFernflowerPreferences") |
| 46 | + |
| 47 | + @Suppress("UNCHECKED_CAST") |
| 48 | + val defaults = (preferencesClass.getField("DEFAULTS").get(null) as Map<String, *>).toMutableMap() |
| 49 | + defaults.putAll(defaultOverrides) |
| 50 | + |
| 51 | + |
| 52 | + val fieldAnnotations = FieldAnnotations(classLoader) |
| 53 | + |
| 54 | + for (field in preferencesClass.fields) { |
| 55 | + if (!Modifier.isStatic(field.modifiers) || field.type != String::class.java) { |
| 56 | + continue |
| 57 | + } |
| 58 | + val key = inferShortKey(field, fieldAnnotations) ?: continue |
| 59 | + if (key in ignoredPreferences) { |
| 60 | + continue |
| 61 | + } |
| 62 | + val longKey = inferLongKey(field) ?: continue |
| 63 | + |
| 64 | + val type = inferType(key, defaults, field, fieldAnnotations) ?: continue |
| 65 | + val name = inferName(key, field, fieldAnnotations) |
| 66 | + val description = inferDescription(field, fieldAnnotations) |
| 67 | + val currentValue = settingsMap[key] ?: (defaults[key] ?: defaults[longKey]!!).toString() |
| 68 | + val component = when (type) { |
| 69 | + Type.BOOLEAN -> JBCheckBox().also { checkBox -> |
| 70 | + checkBox.isSelected = currentValue == "1" |
| 71 | + checkBox.addActionListener { |
| 72 | + val newValue = if (checkBox.isSelected) "1" else "0" |
| 73 | + if (newValue != defaults[key]) { |
| 74 | + settingsMap[key] = newValue |
| 75 | + } else { |
| 76 | + settingsMap.remove(key) |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + Type.STRING -> JBTextField(currentValue).also { textField -> |
| 81 | + textField.columns = 20 |
| 82 | + textField.document.addDocumentListener(object : DocumentAdapter() { |
| 83 | + override fun textChanged(e: DocumentEvent) { |
| 84 | + val newValue = textField.text |
| 85 | + if (newValue != defaults[key]) { |
| 86 | + settingsMap[key] = newValue |
| 87 | + } else { |
| 88 | + settingsMap.remove(key) |
| 89 | + } |
| 90 | + } |
| 91 | + }) |
| 92 | + } |
| 93 | + Type.INTEGER -> JBIntSpinner(currentValue.toInt(), 0, Int.MAX_VALUE).also { spinner -> |
| 94 | + spinner.addChangeListener { |
| 95 | + val newValue = spinner.value.toString() |
| 96 | + if (newValue != defaults[key]) { |
| 97 | + settingsMap[key] = newValue |
| 98 | + } else { |
| 99 | + settingsMap.remove(key) |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + entries += SettingsEntry(name, component, description) |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + private val nameOverrides = mapOf( |
| 109 | + "dc4" to "Decompile Class 1.4", |
| 110 | + "ind" to "Indent Size", |
| 111 | + "lit" to "Literals As-Is", |
| 112 | + ) |
| 113 | + |
| 114 | + private fun inferLongKey(field: Field): String? { |
| 115 | + return field.get(null) as String? |
| 116 | + } |
| 117 | + |
| 118 | + private fun inferShortKey(field: Field, fieldAnnotations: FieldAnnotations): String? { |
| 119 | + if (fieldAnnotations.shortName != null) { |
| 120 | + val shortName = field.getAnnotation(fieldAnnotations.shortName)?.value |
| 121 | + if (shortName != null) { |
| 122 | + return shortName |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + return field.get(null) as String? |
| 127 | + } |
| 128 | + |
| 129 | + private fun inferType(key: String, defaults: Map<String, *>, field: Field, fieldAnnotations: FieldAnnotations): Type? { |
| 130 | + if (fieldAnnotations.type != null) { |
| 131 | + when (field.getAnnotation(fieldAnnotations.type)?.value) { |
| 132 | + "bool" -> return Type.BOOLEAN |
| 133 | + "int" -> return Type.INTEGER |
| 134 | + "string" -> return Type.STRING |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + val dflt = defaults[key]?.toString() ?: return null |
| 139 | + if (dflt == "0" || dflt == "1") { |
| 140 | + return Type.BOOLEAN |
| 141 | + } |
| 142 | + if (dflt.toIntOrNull() != null) { |
| 143 | + return Type.INTEGER |
| 144 | + } |
| 145 | + return Type.STRING |
| 146 | + } |
| 147 | + |
| 148 | + private fun inferName(key: String, field: Field, fieldAnnotations: FieldAnnotations): String { |
| 149 | + if (fieldAnnotations.name != null) { |
| 150 | + val name = field.getAnnotation(fieldAnnotations.name)?.value |
| 151 | + if (name != null) { |
| 152 | + return name |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + val nameOverride = nameOverrides[key] |
| 157 | + if (nameOverride != null) { |
| 158 | + return nameOverride |
| 159 | + } |
| 160 | + |
| 161 | + return StringUtil.toTitleCase(field.name.replace("_", " ").toLowerCase(Locale.ROOT)) |
| 162 | + } |
| 163 | + |
| 164 | + private fun inferDescription(field: Field, fieldAnnotations: FieldAnnotations): String? { |
| 165 | + if (fieldAnnotations.description != null) { |
| 166 | + val description = field.getAnnotation(fieldAnnotations.description)?.value |
| 167 | + if (description != null) { |
| 168 | + return description |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + return null |
| 173 | + } |
| 174 | + |
| 175 | + private val Annotation.value get() = javaClass.getMethod("value").invoke(this) as? String |
| 176 | + |
| 177 | + class FieldAnnotations(classLoader: ClassLoader) { |
| 178 | + val name = classLoader.tryLoadAnnotation("org.jetbrains.java.decompiler.main.extern.IFernflowerPreferences\$Name") |
| 179 | + val description = classLoader.tryLoadAnnotation("org.jetbrains.java.decompiler.main.extern.IFernflowerPreferences\$Description") |
| 180 | + val shortName = classLoader.tryLoadAnnotation("org.jetbrains.java.decompiler.main.extern.IFernflowerPreferences\$ShortName") |
| 181 | + val type = classLoader.tryLoadAnnotation("org.jetbrains.java.decompiler.main.extern.IFernflowerPreferences\$Type") |
| 182 | + |
| 183 | + companion object { |
| 184 | + private fun ClassLoader.tryLoadAnnotation(name: String): Class<out Annotation>? { |
| 185 | + return try { |
| 186 | + loadClass(name).asSubclass(Annotation::class.java) |
| 187 | + } catch (e: ClassNotFoundException) { |
| 188 | + null |
| 189 | + } catch (e: ClassCastException) { |
| 190 | + null |
| 191 | + } |
| 192 | + } |
| 193 | + } |
| 194 | + } |
| 195 | +} |
0 commit comments