Commit ab8fa23f authored by Skylot's avatar Skylot

cli: move specific code from common jadx args

parent 79854662
package jadx.cli; package jadx.cli;
import jadx.api.Decompiler; import jadx.api.Decompiler;
import jadx.core.utils.exceptions.JadxException;
import java.io.File;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class JadxCLI { public class JadxCLI {
private static final Logger LOG = LoggerFactory.getLogger(JadxCLI.class);
public static void main(String[] args) { public static void main(String[] args) {
JadxArgs jadxArgs = new JadxArgs(args, true); try {
JadxCLIArgs jadxArgs = new JadxCLIArgs(args);
checkArgs(jadxArgs);
Decompiler jadx = new Decompiler(jadxArgs); Decompiler jadx = new Decompiler(jadxArgs);
jadx.processAndSaveAll(); jadx.processAndSaveAll();
System.exit(jadx.getErrorsCount()); System.exit(jadx.getErrorsCount());
} catch (Throwable e) {
LOG.error(e.getMessage());
System.exit(1);
}
}
private static void checkArgs(JadxCLIArgs jadxArgs) throws JadxException {
if (jadxArgs.getInput().isEmpty())
throw new JadxException("Please specify input file");
File outputDir = jadxArgs.getOutDir();
if (outputDir == null) {
String outDirName;
File file = jadxArgs.getInput().get(0);
String name = file.getName();
int pos = name.lastIndexOf('.');
if (pos != -1)
outDirName = name.substring(0, pos);
else
outDirName = name + "-jadx-out";
LOG.info("output directory: " + outDirName);
outputDir = new File(outDirName);
jadxArgs.setOutputDir(outputDir);
}
if (outputDir.exists() && !outputDir.isDirectory())
throw new JadxException("Output directory exists as file " + outputDir);
} }
} }
package jadx.cli; package jadx.cli;
import jadx.core.Consts;
import jadx.api.IJadxArgs; import jadx.api.IJadxArgs;
import jadx.core.Consts;
import jadx.core.utils.exceptions.JadxException; import jadx.core.utils.exceptions.JadxException;
import java.io.File; import java.io.File;
...@@ -18,10 +18,9 @@ import com.beust.jcommander.Parameter; ...@@ -18,10 +18,9 @@ import com.beust.jcommander.Parameter;
import com.beust.jcommander.ParameterDescription; import com.beust.jcommander.ParameterDescription;
import com.beust.jcommander.ParameterException; import com.beust.jcommander.ParameterException;
public class JadxArgs implements IJadxArgs { public final class JadxCLIArgs implements IJadxArgs {
private static final Logger LOG = LoggerFactory.getLogger(JadxArgs.class);
@Parameter(description = "<input files> (.dex, .apk, .jar or .class)") @Parameter(description = "<input file> (.dex, .apk, .jar or .class)")
protected List<String> files; protected List<String> files;
@Parameter(names = {"-d", "--output-dir"}, description = "output directory") @Parameter(names = {"-d", "--output-dir"}, description = "output directory")
...@@ -45,50 +44,30 @@ public class JadxArgs implements IJadxArgs { ...@@ -45,50 +44,30 @@ public class JadxArgs implements IJadxArgs {
@Parameter(names = {"-h", "--help"}, description = "print this help", help = true) @Parameter(names = {"-h", "--help"}, description = "print this help", help = true)
protected boolean printHelp = false; protected boolean printHelp = false;
private final List<File> input = new ArrayList<File>(); private final List<File> input = new ArrayList<File>(1);
private File outputDir; private File outputDir;
private final boolean inputRequired; public JadxCLIArgs(String[] args) {
public JadxArgs(String[] args, boolean inputRequired) {
this.inputRequired = inputRequired;
parse(args); parse(args);
checkArguments(); processArgs();
} }
private void parse(String[] args) { private void parse(String[] args) {
try { try {
new JCommander(this, args); new JCommander(this, args);
} catch (ParameterException e) { } catch (ParameterException e) {
System.out.println("Arguments parse error: " + e.getMessage()); System.err.println("Arguments parse error: " + e.getMessage());
System.out.println(); printUsage();
printHelp = true; System.exit(1);
} }
} }
private void checkArguments() { public void processArgs() {
if (isPrintHelp()) { if (isPrintHelp()) {
printUsage(); printUsage();
System.exit(0); System.exit(0);
} }
if (isVerbose()) {
ch.qos.logback.classic.Logger rootLogger =
(ch.qos.logback.classic.Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
rootLogger.setLevel(ch.qos.logback.classic.Level.DEBUG);
}
try { try {
processArgs();
} catch (JadxException e) {
LOG.error(e.getMessage());
printUsage();
System.exit(1);
}
}
public void processArgs() throws JadxException {
if (printHelp)
return;
if (threadsCount <= 0) if (threadsCount <= 0)
throw new JadxException("Threads count must be positive"); throw new JadxException("Threads count must be positive");
...@@ -102,31 +81,22 @@ public class JadxArgs implements IJadxArgs { ...@@ -102,31 +81,22 @@ public class JadxArgs implements IJadxArgs {
} }
} }
if (input.isEmpty()) { if (input.size() > 1)
if (inputRequired)
throw new JadxException("Please specify input file");
else
return;
}
if (input.size() > 1) {
throw new JadxException("Only one input file is supported"); throw new JadxException("Only one input file is supported");
}
if (outDirName == null) { if (outDirName != null)
File file = new File(files.get(0)); outputDir = new File(outDirName);
String name = file.getName();
int pos = name.lastIndexOf('.');
if (pos != -1)
outDirName = name.substring(0, pos);
else
outDirName = name + "-jadx-out";
LOG.info("output directory: " + outDirName); if (isVerbose()) {
ch.qos.logback.classic.Logger rootLogger =
(ch.qos.logback.classic.Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
rootLogger.setLevel(ch.qos.logback.classic.Level.DEBUG);
}
} catch (JadxException e) {
System.err.println("ERROR: " + e.getMessage());
printUsage();
System.exit(1);
} }
outputDir = new File(outDirName);
if (outputDir.exists() && !outputDir.isDirectory())
throw new JadxException("Output directory exists as file " + outputDir);
} }
public void printUsage() { public void printUsage() {
...@@ -157,9 +127,6 @@ public class JadxArgs implements IJadxArgs { ...@@ -157,9 +127,6 @@ public class JadxArgs implements IJadxArgs {
opt.append(' ').append(p.getNames()); opt.append(' ').append(p.getNames());
addSpaces(opt, maxNamesLen - opt.length() + 2); addSpaces(opt, maxNamesLen - opt.length() + 2);
opt.append("- ").append(p.getDescription()); opt.append("- ").append(p.getDescription());
if (inputRequired && name.equals("files")) {
opt.append(" [required]");
}
out.println(opt.toString()); out.println(opt.toString());
break; break;
} }
...@@ -179,6 +146,10 @@ public class JadxArgs implements IJadxArgs { ...@@ -179,6 +146,10 @@ public class JadxArgs implements IJadxArgs {
return outputDir; return outputDir;
} }
public void setOutputDir(File outputDir) {
this.outputDir = outputDir;
}
@Override @Override
public int getThreadsCount() { public int getThreadsCount() {
return threadsCount; return threadsCount;
......
package jadx.gui; package jadx.gui;
import jadx.cli.JadxArgs; import jadx.cli.JadxCLIArgs;
import javax.swing.SwingUtilities; import javax.swing.SwingUtilities;
import javax.swing.UIManager; import javax.swing.UIManager;
...@@ -12,18 +12,13 @@ public class JadxGUI { ...@@ -12,18 +12,13 @@ public class JadxGUI {
private static final Logger LOG = LoggerFactory.getLogger(JadxGUI.class); private static final Logger LOG = LoggerFactory.getLogger(JadxGUI.class);
public static void main(String[] args) { public static void main(String[] args) {
final JadxArgs jadxArgs = new JadxArgs(args, false);
try { try {
final JadxCLIArgs jadxArgs = new JadxCLIArgs(args);
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Throwable e) {
LOG.error("Error: " + e.getMessage());
System.exit(1);
}
SwingUtilities.invokeLater(new Runnable() { SwingUtilities.invokeLater(new Runnable() {
public void run() { public void run() {
MainWindow mainWindow = new MainWindow(jadxArgs); JadxWrapper wrapper = new JadxWrapper(jadxArgs);
MainWindow mainWindow = new MainWindow(wrapper);
mainWindow.setVisible(true); mainWindow.setVisible(true);
if (!jadxArgs.getInput().isEmpty()) { if (!jadxArgs.getInput().isEmpty()) {
...@@ -31,6 +26,10 @@ public class JadxGUI { ...@@ -31,6 +26,10 @@ public class JadxGUI {
} }
} }
}); });
} catch (Throwable e) {
LOG.error("Error: " + e.getMessage());
System.exit(1);
}
} }
} }
package jadx.gui; package jadx.gui;
import jadx.cli.JadxArgs;
import jadx.gui.treemodel.JClass; import jadx.gui.treemodel.JClass;
import jadx.gui.treemodel.JNode; import jadx.gui.treemodel.JNode;
import jadx.gui.treemodel.JRoot; import jadx.gui.treemodel.JRoot;
...@@ -66,8 +65,8 @@ public class MainWindow extends JFrame { ...@@ -66,8 +65,8 @@ public class MainWindow extends JFrame {
private JToolBar searchToolBar; private JToolBar searchToolBar;
private SearchBar searchBar; private SearchBar searchBar;
public MainWindow(JadxArgs jadxArgs) { public MainWindow(JadxWrapper wrapper) {
this.wrapper = new JadxWrapper(jadxArgs); this.wrapper = wrapper;
initUI(); initUI();
initMenuAndToolbar(); initMenuAndToolbar();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment