Skip to content

Commit

Permalink
fix(cli): don't print stacktrace for incorrect options (#2140)
Browse files Browse the repository at this point in the history
  • Loading branch information
skylot committed Apr 20, 2024
1 parent bc70f8e commit f2ea641
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 22 deletions.
21 changes: 10 additions & 11 deletions jadx-cli/src/main/java/jadx/cli/JadxCLIArgs.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
import jadx.api.args.ResourceNameSource;
import jadx.api.args.UserRenamesMappingsMode;
import jadx.core.deobf.conditions.DeobfWhitelist;
import jadx.core.utils.exceptions.JadxException;
import jadx.core.utils.exceptions.JadxArgsValidateException;
import jadx.core.utils.files.FileUtils;

public class JadxCLIArgs {
Expand Down Expand Up @@ -287,14 +287,13 @@ private boolean process(JCommanderWrapper<JadxCLIArgs> jcw) {
System.out.println(JadxDecompiler.getVersion());
return false;
}
try {
if (threadsCount <= 0) {
throw new JadxException("Threads count must be positive, got: " + threadsCount);
if (threadsCount <= 0) {
throw new JadxArgsValidateException("Threads count must be positive, got: " + threadsCount);
}
for (String fileName : files) {
if (fileName.startsWith("-")) {
throw new JadxArgsValidateException("Unknown option: " + fileName);
}
} catch (JadxException e) {
System.err.println("ERROR: " + e.getMessage());
jcw.printUsage();
return false;
}
return true;
}
Expand Down Expand Up @@ -559,8 +558,8 @@ public Set<RenameEnum> convert(String value) {
for (String s : value.split(",")) {
try {
set.add(RenameEnum.valueOf(s.trim().toUpperCase(Locale.ROOT)));
} catch (IllegalArgumentException e) {
throw new IllegalArgumentException(
} catch (Exception e) {
throw new JadxArgsValidateException(
'\'' + s + "' is unknown for parameter " + paramName
+ ", possible values are " + enumValuesString(RenameEnum.values()));
}
Expand Down Expand Up @@ -625,7 +624,7 @@ public E convert(String value) {
try {
return parse.apply(stringAsEnumName(value));
} catch (Exception e) {
throw new IllegalArgumentException(
throw new JadxArgsValidateException(
'\'' + value + "' is unknown, possible values are: " + enumValuesString(values.get()));
}
}
Expand Down
4 changes: 3 additions & 1 deletion jadx-cli/src/main/java/jadx/cli/JadxCLICommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import jadx.cli.commands.CommandPlugins;
import jadx.cli.commands.ICommand;
import jadx.core.utils.exceptions.JadxArgsValidateException;

public class JadxCLICommands {
private static final Map<String, ICommand> COMMANDS_MAP = new TreeMap<>();
Expand All @@ -26,7 +27,8 @@ public static void append(JCommander.Builder builder) {
public static boolean process(JCommanderWrapper<?> jcw, JCommander jc, String parsedCommand) {
ICommand command = COMMANDS_MAP.get(parsedCommand);
if (command == null) {
throw new IllegalArgumentException("Unknown command: " + parsedCommand);
throw new JadxArgsValidateException("Unknown command: " + parsedCommand
+ ". Expected one of: " + COMMANDS_MAP.keySet());
}
JCommander subCommander = jc.getCommands().get(parsedCommand);
command.process(jcw, subCommander);
Expand Down
7 changes: 4 additions & 3 deletions jadx-cli/src/main/java/jadx/cli/SingleClassMode.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import jadx.core.dex.attributes.AFlag;
import jadx.core.dex.nodes.ClassNode;
import jadx.core.dex.visitors.SaveCode;
import jadx.core.utils.exceptions.JadxArgsValidateException;
import jadx.core.utils.exceptions.JadxRuntimeException;
import jadx.core.utils.files.FileUtils;

Expand All @@ -33,10 +34,10 @@ public static boolean process(JadxDecompiler jadx, JadxCLIArgs cliArgs) {
.findFirst().orElse(null);
}
if (clsForProcess == null) {
throw new JadxRuntimeException("Input class not found: " + singleClass);
throw new JadxArgsValidateException("Input class not found: " + singleClass);
}
if (clsForProcess.contains(AFlag.DONT_GENERATE)) {
throw new JadxRuntimeException("Input class can't be saved by current jadx settings (marked as DONT_GENERATE)");
throw new JadxArgsValidateException("Input class can't be saved by current jadx settings (marked as DONT_GENERATE)");
}
if (clsForProcess.isInner()) {
clsForProcess = clsForProcess.getTopParentClass();
Expand All @@ -52,7 +53,7 @@ public static boolean process(JadxDecompiler jadx, JadxCLIArgs cliArgs) {
if (size == 1) {
clsForProcess = classes.get(0);
} else {
throw new JadxRuntimeException("Found " + size + " classes, single class output can't be used");
throw new JadxArgsValidateException("Found " + size + " classes, single class output can't be used");
}
}
ICodeInfo codeInfo;
Expand Down
3 changes: 2 additions & 1 deletion jadx-cli/src/test/java/jadx/cli/RenameConverterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import jadx.api.JadxArgs.RenameEnum;
import jadx.cli.JadxCLIArgs.RenameConverter;
import jadx.core.utils.exceptions.JadxArgsValidateException;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
Expand Down Expand Up @@ -38,7 +39,7 @@ public void none() {

@Test
public void wrong() {
IllegalArgumentException thrown = assertThrows(IllegalArgumentException.class,
JadxArgsValidateException thrown = assertThrows(JadxArgsValidateException.class,
() -> converter.convert("wrong"),
"Expected convert() to throw, but it didn't");

Expand Down
6 changes: 0 additions & 6 deletions jadx-core/src/main/java/jadx/api/JadxArgsValidator.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,6 @@ private static void checkInputFiles(JadxDecompiler jadx, JadxArgs args) {
if (inputFiles.isEmpty() && jadx.getCustomCodeLoaders().isEmpty()) {
throw new JadxArgsValidateException("Please specify input file");
}
for (File inputFile : inputFiles) {
String fileName = inputFile.getName();
if (fileName.startsWith("--")) {
throw new JadxArgsValidateException("Unknown argument: " + fileName);
}
}
for (File file : inputFiles) {
checkFile(file);
}
Expand Down

0 comments on commit f2ea641

Please sign in to comment.