From fbdfd135da108be90e6cdd82b47e24d906e18a29 Mon Sep 17 00:00:00 2001 From: Skylot Date: Mon, 27 Feb 2023 19:28:24 +0000 Subject: [PATCH] fix(cli): use common enum args parser (#1787) --- .../src/main/java/jadx/cli/JadxCLIArgs.java | 91 +++++++++---------- .../src/main/java/jadx/cli/LogHelper.java | 16 ---- 2 files changed, 42 insertions(+), 65 deletions(-) diff --git a/jadx-cli/src/main/java/jadx/cli/JadxCLIArgs.java b/jadx-cli/src/main/java/jadx/cli/JadxCLIArgs.java index 4d6152044..d89cb2eab 100644 --- a/jadx-cli/src/main/java/jadx/cli/JadxCLIArgs.java +++ b/jadx-cli/src/main/java/jadx/cli/JadxCLIArgs.java @@ -7,6 +7,8 @@ import java.util.List; import java.util.Locale; import java.util.Map; import java.util.Set; +import java.util.function.Function; +import java.util.function.Supplier; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -187,7 +189,7 @@ public class JadxCLIArgs { @Parameter( names = { "--log-level" }, description = "set log level, values: quiet, progress, error, warn, info, debug", - converter = LogHelper.LogLevelConverter.class + converter = LogLevelConverter.class ) protected LogHelper.LogLevelEnum logLevel = LogHelper.LogLevelEnum.PROGRESS; @@ -487,67 +489,58 @@ public class JadxCLIArgs { } } - public static class CommentsLevelConverter implements IStringConverter { - @Override - public CommentsLevel convert(String value) { - try { - return CommentsLevel.valueOf(stringAsEnumName(value)); - } catch (Exception e) { - throw new IllegalArgumentException( - '\'' + value + "' is unknown comments level, possible values are: " - + JadxCLIArgs.enumValuesString(CommentsLevel.values())); - } + public static class CommentsLevelConverter extends BaseEnumConverter { + public CommentsLevelConverter() { + super(CommentsLevel::valueOf, CommentsLevel::values); } } - public static class UseKotlinMethodsForVarNamesConverter implements IStringConverter { - @Override - public UseKotlinMethodsForVarNames convert(String value) { - try { - return UseKotlinMethodsForVarNames.valueOf(stringAsEnumName(value)); - } catch (Exception e) { - throw new IllegalArgumentException( - '\'' + value + "' is unknown, possible values are: " - + JadxCLIArgs.enumValuesString(CommentsLevel.values())); - } + public static class UseKotlinMethodsForVarNamesConverter extends BaseEnumConverter { + public UseKotlinMethodsForVarNamesConverter() { + super(UseKotlinMethodsForVarNames::valueOf, UseKotlinMethodsForVarNames::values); } } - public static class DeobfuscationMapFileModeConverter implements IStringConverter { - @Override - public DeobfuscationMapFileMode convert(String value) { - try { - return DeobfuscationMapFileMode.valueOf(stringAsEnumName(value)); - } catch (Exception e) { - throw new IllegalArgumentException( - '\'' + value + "' is unknown, possible values are: " - + JadxCLIArgs.enumValuesString(DeobfuscationMapFileMode.values())); - } + public static class DeobfuscationMapFileModeConverter extends BaseEnumConverter { + public DeobfuscationMapFileModeConverter() { + super(DeobfuscationMapFileMode::valueOf, DeobfuscationMapFileMode::values); } } - public static class ResourceNameSourceConverter implements IStringConverter { - @Override - public ResourceNameSource convert(String value) { - try { - return ResourceNameSource.valueOf(stringAsEnumName(value)); - } catch (Exception e) { - throw new IllegalArgumentException( - '\'' + value + "' is unknown, possible values are: " - + JadxCLIArgs.enumValuesString(ResourceNameSource.values())); - } + public static class ResourceNameSourceConverter extends BaseEnumConverter { + public ResourceNameSourceConverter() { + super(ResourceNameSource::valueOf, ResourceNameSource::values); } } - public static class DecompilationModeConverter implements IStringConverter { + public static class DecompilationModeConverter extends BaseEnumConverter { + public DecompilationModeConverter() { + super(DecompilationMode::valueOf, DecompilationMode::values); + } + } + + public static class LogLevelConverter extends BaseEnumConverter { + public LogLevelConverter() { + super(LogHelper.LogLevelEnum::valueOf, LogHelper.LogLevelEnum::values); + } + } + + public abstract static class BaseEnumConverter> implements IStringConverter { + private final Function parse; + private final Supplier values; + + public BaseEnumConverter(Function parse, Supplier values) { + this.parse = parse; + this.values = values; + } + @Override - public DecompilationMode convert(String value) { + public E convert(String value) { try { - return DecompilationMode.valueOf(stringAsEnumName(value)); + return parse.apply(stringAsEnumName(value)); } catch (Exception e) { throw new IllegalArgumentException( - '\'' + value + "' is unknown, possible values are: " - + JadxCLIArgs.enumValuesString(DecompilationMode.values())); + '\'' + value + "' is unknown, possible values are: " + enumValuesString(values.get())); } } } @@ -558,8 +551,8 @@ public class JadxCLIArgs { .collect(Collectors.joining(", ")); } - private static String stringAsEnumName(String raw) { - // inverse of enumValuesString conversion - return value.replace('-', '_').toUpperCase(); + private static String stringAsEnumName(String value) { + // inverse of enumValuesString conversion + return value.replace('-', '_').toUpperCase(Locale.ROOT); } } diff --git a/jadx-cli/src/main/java/jadx/cli/LogHelper.java b/jadx-cli/src/main/java/jadx/cli/LogHelper.java index 6ab9b149d..44afa170f 100644 --- a/jadx-cli/src/main/java/jadx/cli/LogHelper.java +++ b/jadx-cli/src/main/java/jadx/cli/LogHelper.java @@ -4,8 +4,6 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.slf4j.LoggerFactory; -import com.beust.jcommander.IStringConverter; - import ch.qos.logback.classic.Level; import ch.qos.logback.classic.Logger; @@ -119,18 +117,4 @@ public class LogHelper { } return false; } - - public static class LogLevelConverter implements IStringConverter { - - @Override - public LogLevelEnum convert(String value) { - try { - return LogLevelEnum.valueOf(value.toUpperCase()); - } catch (Exception e) { - throw new IllegalArgumentException( - '\'' + value + "' is unknown log level, possible values are " - + JadxCLIArgs.enumValuesString(LogLevelEnum.values())); - } - } - } }