Skip to content

Commit

Permalink
Improve help output, display plugin information
Browse files Browse the repository at this point in the history
  • Loading branch information
jaskarth committed Jan 22, 2024
1 parent 850814f commit 13c0063
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ public String id() {
return "IdeaNotNull";
}

@Override
public String description() {
return "Decompiles code inserted by Intellij IDEA's @NotNull annotation.";
}

@Override
public void registerJavaPasses(JavaPassRegistrar registrar) {
registrar.register(JavaPassLocation.MAIN_LOOP, new NamedPass("IdeaNotNull", new IdeaNotNullPass()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ public String id() {
return "Kotlin";
}

@Override
public String description() {
return "Detects and decompiles Kotlin class files.";
}

@Override
public @Nullable PluginOptions getPluginOptions() {
return () -> Pair.of(KotlinOptions.class, KotlinOptions::addDefaults);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@

import org.jetbrains.java.decompiler.api.plugin.Plugin;

public class ScalaPlugin implements Plugin{
public class ScalaPlugin implements Plugin {

public String id(){
return "Scala";
}

@Override
public String description() {
return "Detects and decompiles Scala class files.";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ public String id() {
return "VariableRenaming";
}

@Override
public String description() {
return "Allows automatic renaming of variables with a common naming scheme.";
}

@Override
public @Nullable PluginOptions getPluginOptions() {
return () -> Pair.of(VariableRenamingOptions.class, VariableRenamingOptions::addDefaults);
Expand Down
6 changes: 6 additions & 0 deletions src/org/jetbrains/java/decompiler/api/plugin/Plugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ public interface Plugin {
*/
String id();

/**
* Short (1-2 sentence) long description of the plugin and its function. This will be presented to users when requested via terminal.
* @return a short description of the plugin
*/
String description();

/**
* Allows addition to the list of passes that will be run during Java decompilation.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,16 @@ else if (args.length > x+1) {
return;
}

if (Arrays.stream(args).anyMatch(arg -> arg.equals("--list-plugins"))) {
ConsoleHelp.printPlugins();
return;
}

if (args.length < 1) {
System.out.println(
"=== Vineflower Decompiler " + version() + " ===\n\n" +
"Usage: java -jar vineflower.jar --<option>=<value>... <source>... <destination>\n" +
"Example: java -jar vineflower.jar --decompile-generics ./MyJar.jar ./out_files\n" +
"Example: java -jar vineflower.jar --decompile-generics ./MyJar.jar ./out_files\n\n" +
"Use -h or --help for more information.");
return;
}
Expand Down Expand Up @@ -431,4 +437,9 @@ public Function<File, IResultSaver> getSaver() {
return saver;
}
}

public static String version() {
String ver = ConsoleDecompiler.class.getPackage().getImplementationVersion();
return ver == null ? "<UNK>" : ver;
}
}
54 changes: 39 additions & 15 deletions src/org/jetbrains/java/decompiler/main/decompiler/ConsoleHelp.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,32 +12,37 @@

public class ConsoleHelp {
private static final String[] DEFAULT_HELP = {
"=== Vineflower Decompiler " + ConsoleDecompiler.version() + " ===",
"",
"--- Command-line options ---",
"-h, --help: Print this help screen",
"-s, --silent: Only print to the console if an error is raised",
"--list-plugins: Displays loaded plugin information",
"",
"--- Decompiler usage ---",
"Usage: java -jar vineflower.jar --<option>=<value>... <source>... <destination>",
"At least one source file or directory must be specified.",
"Options:",
"-h, --help: Show this help",
"-s, --silent: Only print to console if an error is raised",
"",
"Saving options",
"--- Saving options ---",
"A maximum of one of the options can be specified:",
"--file - Write the decompiled source to a file",
"--folder - Write the decompiled source to a folder",
"--legacy-saving - Use the legacy console-specific method of saving",
"If unspecified, the decompiled source will be automatically detected based on destination name.",
"",
"General options",
"--- General options ---",
"These options can be specified multiple times.",
"-e=<path>, --add-external=<path> - Add the specified path to the list of external libraries",
"-only=<class>, --only=<class> - Only decompile the specified class",
"",
"Additional options",
"--- Additional options ---",
"These options take the last specified value.",
"They are mostly specified with a name followed by an equals sign, followed by the value.",
"Boolean options can also be specified without a value, in which case they are treated as `true`.",
"They can also be specified with a `no-` prefix, in which case they are treated as `false`.",
"For example, `--decompile-generics` is equivalent to `--decompile-generics=true`.",
"",
"Options from the core decompiler:"
"====== Options from the core decompiler ======"
// Options are added at runtime
};

Expand Down Expand Up @@ -73,7 +78,7 @@ static void printHelp() {

if (!pluginFields.isEmpty()) {
System.out.println();
System.out.println("Options for plugin '" + plugin.id() + "':");
System.out.println("====== Options for plugin '" + plugin.id() + "' ======");
writeOptions(pluginFields, pluginDefaults);
}
}
Expand Down Expand Up @@ -121,30 +126,49 @@ private static void writeOptions(List<Field> fields, Map<String, Object> default
.append(" ".repeat(Math.max(8 - type.value().length(), 0)))
.append(name.value())
.append(" ".repeat(Math.max(50 - name.value().length(), 0)))
.append(": ")
.append(description.value());
.append(":");

StringBuilder sb2 = new StringBuilder();
if (defaults.containsKey(paramName)) {
sb.append(" (default: ");
sb2.append(" (default: ");
Object value = defaults.get(paramName);
switch (type.value()) {
case IFernflowerPreferences.Type.BOOLEAN:
sb.append(value.equals("1"));
sb2.append(value.equals("1"));
break;
case IFernflowerPreferences.Type.STRING:
sb.append('"').append(value).append('"');
sb2.append('"').append(value).append('"');
break;
default:
sb.append(value);
sb2.append(value);
break;
}
sb.append(")");
sb2.append(")");
sb2.append(" ".repeat(Math.max(18 - sb2.length(), 1)));
sb.append(sb2);
}

sb.append(description.value());

System.out.println(sb);
}
}

static void printPlugins() {
System.out.println("=== Vineflower Decompiler " + ConsoleDecompiler.version() + " ===");
System.out.println();

PluginContext ctx = new PluginContext();
ctx.findPlugins();

System.out.println("Loaded " + ctx.getPlugins().size() + " plugins:");
for (Plugin plugin : ctx.getPlugins()) {
System.out.println(plugin.id() + " (loaded from " + ctx.getSource(plugin).getClass().getSimpleName() + ")");
System.out.println(" - " + plugin.description());
System.out.println();
}
}

static {
Init.init();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

public class PluginContext {
private final List<Plugin> plugins = new ArrayList<>();
private final Map<Plugin, PluginSource> bySource = new HashMap<>();
private boolean initialized = false;
private Map<JavaPassLocation, List<NamedPass>> passes = new HashMap<>();
private final Map<Plugin, LanguageSpec> languageSpecs = new HashMap<>();
Expand All @@ -28,15 +29,16 @@ public static PluginContext getCurrentContext() {
return DecompilerContext.getCurrentContext().structContext.getPluginContext();
}

private void registerPlugin(Plugin plugin) {
private void registerPlugin(Plugin plugin, PluginSource source) {
plugins.add(plugin);
bySource.put(plugin, source);
}

public int findPlugins() {
int pluginCount = 0;
for (PluginSource source : PluginSources.PLUGIN_SOURCES) {
for (Plugin plugin : source.findPlugins()) {
registerPlugin(plugin);
registerPlugin(plugin, source);
pluginCount++;
}
}
Expand Down Expand Up @@ -113,6 +115,10 @@ public IVariableNamingFactory getVariableRenamer() {
return null;
}

public PluginSource getSource(Plugin plugin) {
return bySource.get(plugin);
}

public List<Plugin> getPlugins() {
return Collections.unmodifiableList(plugins);
}
Expand Down

0 comments on commit 13c0063

Please sign in to comment.