Skip to content

Commit

Permalink
Improve console help formatting, add -s, and add plugin options
Browse files Browse the repository at this point in the history
  • Loading branch information
jaskarth committed Sep 14, 2023
1 parent 37e9cf9 commit 875763c
Show file tree
Hide file tree
Showing 17 changed files with 191 additions and 68 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.vineflower.ideanotnull;

import org.jetbrains.java.decompiler.api.plugin.PluginOptions;
import org.jetbrains.java.decompiler.main.extern.IFernflowerPreferences;

public interface IdeaNotNullOptions {
@IFernflowerPreferences.Name("Resugar Intellij IDEA @NotNull")
@IFernflowerPreferences.Description("Resugar Intellij IDEA's code generated by @NotNull annotations.")
@IFernflowerPreferences.ShortName("inn")
@IFernflowerPreferences.Type(IFernflowerPreferences.Type.BOOLEAN)
String IDEA_NOT_NULL_ANNOTATION = "resugar-idea-notnull";

static void addDefaults(PluginOptions.AddDefaults cons) {
cons.addDefault(IDEA_NOT_NULL_ANNOTATION, "1");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
public class IdeaNotNullPass implements Pass{

public boolean run(PassContext ctx){
if (!DecompilerContext.getOption(IdeaNotNullOptions.IDEA_NOT_NULL_ANNOTATION)) {
return false;
}

return removeHardcodedChecks(ctx.getRoot(), ctx.getMethod());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package org.vineflower.ideanotnull;

import org.jetbrains.annotations.Nullable;
import org.jetbrains.java.decompiler.api.plugin.Plugin;
import org.jetbrains.java.decompiler.api.java.JavaPassLocation;
import org.jetbrains.java.decompiler.api.java.JavaPassRegistrar;
import org.jetbrains.java.decompiler.api.plugin.PluginOptions;
import org.jetbrains.java.decompiler.api.plugin.pass.NamedPass;
import org.jetbrains.java.decompiler.util.Pair;

public class IdeaNotNullPlugin implements Plugin {

Expand All @@ -15,4 +18,9 @@ public String id() {
public void registerJavaPasses(JavaPassRegistrar registrar) {
registrar.register(JavaPassLocation.MAIN_LOOP, new NamedPass("IdeaNotNull", new IdeaNotNullPass()));
}

@Override
public @Nullable PluginOptions getPluginOptions() {
return () -> Pair.of(IdeaNotNullOptions.class, IdeaNotNullOptions::addDefaults);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import org.vineflower.kotlin.metadata.MetadataNameResolver;

import java.io.ByteArrayInputStream;
import java.util.Arrays;
import java.util.Objects;

public class KotlinChooser implements LanguageChooser {
Expand All @@ -33,7 +32,7 @@ public class KotlinChooser implements LanguageChooser {

@Override
public boolean isLanguage(StructClass cl) {
if (Objects.equals(KotlinPreferences.getPreference(KotlinPreferences.FORCE_DISABLE), "1")) {
if (!DecompilerContext.getOption(KotlinOptions.DECOMPILE_KOTLIN)) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.vineflower.kotlin;

import org.jetbrains.java.decompiler.api.plugin.PluginOptions;
import org.jetbrains.java.decompiler.main.DecompilerContext;
import org.jetbrains.java.decompiler.main.extern.IFernflowerPreferences.*;

import java.util.Map;

public interface KotlinOptions {
@Name("Show public visibility")
@Description("If a construct is public, show the public keyword")
@Type(Type.BOOLEAN)
String SHOW_PUBLIC_VISIBILITY = "kt-show-public";

@Name("Decompile Kotlin")
@Description("Decompile Kotlin classes as Kotlin instead of Java")
@Type(Type.BOOLEAN)
String DECOMPILE_KOTLIN = "kt-decompile-kotlin";

static void addDefaults(PluginOptions.AddDefaults cons) {
cons.addDefault(SHOW_PUBLIC_VISIBILITY, "1");
cons.addDefault(DECOMPILE_KOTLIN, "1");
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package org.vineflower.kotlin;

import org.jetbrains.annotations.Nullable;
import org.jetbrains.java.decompiler.api.plugin.Plugin;
import org.jetbrains.java.decompiler.api.plugin.LanguageSpec;
import org.jetbrains.java.decompiler.api.plugin.PluginOptions;
import org.jetbrains.java.decompiler.api.plugin.pass.LoopingPassBuilder;
import org.jetbrains.java.decompiler.api.plugin.pass.MainPassBuilder;
import org.jetbrains.java.decompiler.api.plugin.pass.Pass;
import org.jetbrains.java.decompiler.api.plugin.pass.WrappedPass;
import org.jetbrains.java.decompiler.modules.decompiler.*;
import org.jetbrains.java.decompiler.modules.decompiler.decompose.DomHelper;
import org.jetbrains.java.decompiler.util.Pair;
import org.vineflower.kotlin.pass.*;

public class KotlinPlugin implements Plugin {
Expand All @@ -22,6 +25,11 @@ public String id() {
return "Kotlin";
}

@Override
public @Nullable PluginOptions getPluginOptions() {
return () -> Pair.of(KotlinOptions.class, KotlinOptions::addDefaults);
}

@Override
public LanguageSpec getLanguageSpec() {
return new LanguageSpec("kotlin", new KotlinChooser(), new DomHelper(), new KotlinWriter(), makePass());
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.jetbrains.annotations.Nullable;
import org.jetbrains.java.decompiler.code.CodeConstants;
import org.jetbrains.java.decompiler.main.ClassesProcessor;
import org.jetbrains.java.decompiler.main.DecompilerContext;
import org.jetbrains.java.decompiler.main.rels.ClassWrapper;
import org.jetbrains.java.decompiler.main.rels.MethodWrapper;
import org.jetbrains.java.decompiler.modules.decompiler.exps.ConstExprent;
Expand All @@ -20,7 +21,7 @@
import org.jetbrains.java.decompiler.util.TextBuffer;
import org.jetbrains.java.decompiler.util.collections.VBStyleCollection;
import org.vineflower.kotlin.KotlinDecompilationContext;
import org.vineflower.kotlin.KotlinPreferences;
import org.vineflower.kotlin.KotlinOptions;
import org.vineflower.kotlin.KotlinWriter;
import org.vineflower.kotlin.metadata.MetadataNameResolver;
import org.vineflower.kotlin.util.KTypes;
Expand Down Expand Up @@ -212,8 +213,7 @@ private static void appendVisibility(TextBuffer buf, ProtoBuf.Visibility visibil
buf.append("private ");
break;
case PUBLIC:
String showPublicVisibility = KotlinPreferences.getPreference(KotlinPreferences.SHOW_PUBLIC_VISIBILITY);
if (Objects.equals(showPublicVisibility, "1")) {
if (DecompilerContext.getOption(KotlinOptions.SHOW_PUBLIC_VISIBILITY)) {
buf.append("public ");
}
break;
Expand Down
7 changes: 7 additions & 0 deletions src/org/jetbrains/java/decompiler/api/plugin/Plugin.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.jetbrains.java.decompiler.api.plugin;

import org.jetbrains.annotations.Nullable;
import org.jetbrains.java.decompiler.api.java.JavaPassRegistrar;

/**
Expand All @@ -26,7 +27,13 @@ default void registerJavaPasses(JavaPassRegistrar registrar) {
* Allows the plugin to specify a totally custom decompilation process for a language based on the JVM.
* @return The language spec, if any.
*/
@Nullable
default LanguageSpec getLanguageSpec() {
return null;
}

@Nullable
default PluginOptions getPluginOptions() {
return null;
}
}
16 changes: 16 additions & 0 deletions src/org/jetbrains/java/decompiler/api/plugin/PluginOptions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package org.jetbrains.java.decompiler.api.plugin;

import org.jetbrains.java.decompiler.util.Pair;

import java.util.function.BiConsumer;
import java.util.function.Consumer;

@FunctionalInterface
public interface PluginOptions {
Pair<Class<?>, Consumer<AddDefaults>> provideOptions();

@FunctionalInterface
interface AddDefaults {
void addDefault(String key, Object defaultVal);
}
}
14 changes: 2 additions & 12 deletions src/org/jetbrains/java/decompiler/main/Fernflower.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,13 +115,7 @@ public Fernflower(IBytecodeProvider provider, IResultSaver saver, Map<String, Ob

PluginContext plugins = structContext.getPluginContext();

int pluginCount = 0;
for (PluginSource source : PluginSources.PLUGIN_SOURCES) {
for (Plugin plugin : source.findPlugins()) {
plugins.registerPlugin(plugin);
pluginCount++;
}
}
int pluginCount = plugins.findPlugins();

DecompilerContext.getLogger().writeMessage("Loaded " + pluginCount + " plugins", IFernflowerLogger.Severity.INFO);

Expand Down Expand Up @@ -239,10 +233,6 @@ public String getClassContent(StructClass cl) {
}

static {
// Load all Java code attributes
StructGeneralAttribute.init();

// Class-load all plugins that potentially could be included in the jar
JarPluginLoader.init();
Init.init();
}
}
21 changes: 21 additions & 0 deletions src/org/jetbrains/java/decompiler/main/Init.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package org.jetbrains.java.decompiler.main;

import org.jetbrains.java.decompiler.main.plugins.JarPluginLoader;
import org.jetbrains.java.decompiler.struct.attr.StructGeneralAttribute;

public final class Init {
private static boolean initialized = false;
public static void init() {
if (initialized) {
return;
}

initialized = true;

// Load all Java code attributes
StructGeneralAttribute.init();

// Class-load all plugins that potentially could be included in the jar
JarPluginLoader.init();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,8 @@ else if (args.length > x+1) {

if (args.length < 1) {
System.out.println(
"Usage: java -jar vineflower.jar [--<option>=<value>]* [<source>]+ <destination>\n" +
"Example: java -jar vineflower.jar --decompile-generics c:\\my\\source\\ c:\\my.jar d:\\decompiled\\\n" +
"Usage: java -jar vineflower.jar --<option>=<value>... <source>... <destination>\n" +
"Example: java -jar vineflower.jar --decompile-generics ./MyJar.jar ./out_files\n" +
"Use -h or --help for more information.");
return;
}
Expand Down Expand Up @@ -116,6 +116,7 @@ else if (args.length > x+1) {
if (isOption && arg.length() > 5 && arg.startsWith("-")) {
parsed = OptionParser.parse(arg, mapOptions);
}

if (!parsed) {
nonOption++;
// Don't process this, as it is the output
Expand All @@ -125,13 +126,13 @@ else if (args.length > x+1) {

isOption = false;

if (arg.startsWith("-e=") || arg.startsWith("--add-external=")) {
if (arg.equals("-s") || arg.equals("--silent")) {
mapOptions.put(IFernflowerPreferences.LOG_LEVEL, "error");
} else if (arg.startsWith("-e=") || arg.startsWith("--add-external=")) {
addPath(libraries, arg.substring(arg.indexOf('=') + 1));
}
else if (arg.startsWith("-only=") || arg.startsWith("--only=")) {
} else if (arg.startsWith("-only=") || arg.startsWith("--only=")) {
whitelist.add(arg.substring(arg.indexOf('=') + 1));
}
else {
} else {
addPath(sources, arg);
}
}
Expand Down
Loading

0 comments on commit 875763c

Please sign in to comment.