Skip to content

Commit

Permalink
fix: improve rename using source class name (#2397)
Browse files Browse the repository at this point in the history
  • Loading branch information
skylot committed Jan 20, 2025
1 parent bc4bb0d commit a43b475
Show file tree
Hide file tree
Showing 15 changed files with 67 additions and 13 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ options:
'always' - always use source name if it's available
'if-better' - use source name if it seems better than the current one
'never' - never use source name, even if it's available
--source-name-repeat-limit - allow using source name if it appears less than a limit number, default: 10
--use-kotlin-methods-for-var-names - use kotlin intrinsic methods to rename variables, values: disable, apply, apply-and-hide, default: apply
--rename-flags - fix options (comma-separated list of):
'case' - fix case sensitivity issues (according to --fs-case-sensitive option),
Expand Down
11 changes: 11 additions & 0 deletions jadx-cli/src/main/java/jadx/cli/JadxCLIArgs.java
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,12 @@ public class JadxCLIArgs {
)
protected UseSourceNameAsClassNameAlias useSourceNameAsClassNameAlias = null;

@Parameter(
names = { "--source-name-repeat-limit" },
description = "allow using source name if it appears less than a limit number"
)
protected int sourceNameRepeatLimit = 10;

@Parameter(
names = { "--use-kotlin-methods-for-var-names" },
description = "use kotlin intrinsic methods to rename variables, values: disable, apply, apply-and-hide",
Expand Down Expand Up @@ -352,6 +358,7 @@ public JadxArgs toJadxArgs() {
args.setDeobfuscationMaxLength(deobfuscationMaxLength);
args.setDeobfuscationWhitelist(Arrays.asList(deobfuscationWhitelistStr.split(" ")));
args.setUseSourceNameAsClassNameAlias(getUseSourceNameAsClassNameAlias());
args.setSourceNameRepeatLimit(sourceNameRepeatLimit);
args.setUseKotlinMethodsForVarNames(useKotlinMethodsForVarNames);
args.setResourceNameSource(resourceNameSource);
args.setEscapeUnicode(escapeUnicode);
Expand Down Expand Up @@ -508,6 +515,10 @@ public UseSourceNameAsClassNameAlias getUseSourceNameAsClassNameAlias() {
}
}

public int getSourceNameRepeatLimit() {
return sourceNameRepeatLimit;
}

/**
* @deprecated Use {@link #getUseSourceNameAsClassNameAlias()} instead.
*/
Expand Down
12 changes: 11 additions & 1 deletion jadx-core/src/main/java/jadx/api/JadxArgs.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ public class JadxArgs implements Closeable {

private boolean deobfuscationOn = false;
private UseSourceNameAsClassNameAlias useSourceNameAsClassNameAlias = UseSourceNameAsClassNameAlias.getDefault();
private int sourceNameRepeatLimit = 10;

private File generatedRenamesMappingFile = null;
private GeneratedRenamesMappingFileMode generatedRenamesMappingFileMode = GeneratedRenamesMappingFileMode.getDefault();
Expand Down Expand Up @@ -460,6 +461,14 @@ public void setUseSourceNameAsClassNameAlias(UseSourceNameAsClassNameAlias useSo
this.useSourceNameAsClassNameAlias = useSourceNameAsClassNameAlias;
}

public int getSourceNameRepeatLimit() {
return sourceNameRepeatLimit;
}

public void setSourceNameRepeatLimit(int sourceNameRepeatLimit) {
this.sourceNameRepeatLimit = sourceNameRepeatLimit;
}

/**
* @deprecated Use {@link #getUseSourceNameAsClassNameAlias()} instead.
*/
Expand Down Expand Up @@ -800,7 +809,7 @@ public String makeCodeArgsHash(@Nullable JadxDecompiler decompiler) {
String argStr = "args:" + decompilationMode + useImports + showInconsistentCode
+ inlineAnonymousClasses + inlineMethods + moveInnerClasses + allowInlineKotlinLambda
+ deobfuscationOn + deobfuscationMinLength + deobfuscationMaxLength + deobfuscationWhitelist
+ useSourceNameAsClassNameAlias
+ useSourceNameAsClassNameAlias + sourceNameRepeatLimit
+ resourceNameSource
+ useKotlinMethodsForVarNames
+ insertDebugLines + extractFinally
Expand Down Expand Up @@ -841,6 +850,7 @@ public String toString() {
+ ", generatedRenamesMappingFileMode=" + generatedRenamesMappingFileMode
+ ", resourceNameSource=" + resourceNameSource
+ ", useSourceNameAsClassNameAlias=" + useSourceNameAsClassNameAlias
+ ", sourceNameRepeatLimit=" + sourceNameRepeatLimit
+ ", useKotlinMethodsForVarNames=" + useKotlinMethodsForVarNames
+ ", insertDebugLines=" + insertDebugLines
+ ", extractFinally=" + extractFinally
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,15 @@ public void init(RootNode root) throws JadxException {
if (useSourceName == UseSourceNameAsClassNameAlias.NEVER) {
return;
}
int repeatLimit = root.getArgs().getSourceNameRepeatLimit();
if (repeatLimit <= 1) {
return;
}

List<ClassNode> classes = root.getClasses();
Map<String, Boolean> canUseAlias = new HashMap<>();
Map<String, Integer> aliasUseCount = new HashMap<>();
for (ClassNode cls : classes) {
canUseAlias.put(cls.getClassInfo().getShortName(), Boolean.FALSE);
aliasUseCount.put(cls.getClassInfo().getShortName(), 1);
}
List<ClsRename> renames = new ArrayList<>();
for (ClassNode cls : classes) {
Expand All @@ -47,19 +51,17 @@ public void init(RootNode root) throws JadxException {
}
String alias = getAliasFromSourceFile(cls);
if (alias != null) {
Boolean prev = canUseAlias.get(alias);
if (prev == null) {
canUseAlias.put(alias, Boolean.TRUE);
renames.add(new ClsRename(cls, alias));
} else if (prev == Boolean.TRUE) {
canUseAlias.put(alias, Boolean.FALSE);
int count = aliasUseCount.merge(alias, 1, Integer::sum);
if (count < repeatLimit) {
renames.add(new ClsRename(cls, alias, count));
}
}
}
for (ClsRename clsRename : renames) {
String alias = clsRename.getAlias();
if (canUseAlias.get(alias) == Boolean.TRUE) {
applyRename(clsRename.getCls(), alias, useSourceName);
Integer count = aliasUseCount.get(alias);
if (count < repeatLimit) {
applyRename(clsRename.getCls(), clsRename.buildAlias(), useSourceName);
}
}
}
Expand Down Expand Up @@ -112,10 +114,12 @@ private static String getBetterName(String currentName, String sourceName, UseSo
private static final class ClsRename {
private final ClassNode cls;
private final String alias;
private final int suffix;

private ClsRename(ClassNode cls, String alias) {
private ClsRename(ClassNode cls, String alias, int suffix) {
this.cls = cls;
this.alias = alias;
this.suffix = suffix;
}

public ClassNode getCls() {
Expand All @@ -126,9 +130,17 @@ public String getAlias() {
return alias;
}

public int getSuffix() {
return suffix;
}

public String buildAlias() {
return suffix < 2 ? alias : alias + suffix;
}

@Override
public String toString() {
return "ClsRename{" + cls + " -> '" + alias + "'}";
return "ClsRename{" + cls + " -> '" + alias + suffix + "'}";
}
}
}
4 changes: 4 additions & 0 deletions jadx-gui/src/main/java/jadx/gui/settings/JadxSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,10 @@ public void setUseSourceNameAsClassNameAlias(UseSourceNameAsClassNameAlias useSo
this.useSourceNameAsClassNameAlias = useSourceNameAsClassNameAlias;
}

public void setSourceNameRepeatLimit(int sourceNameRepeatLimit) {
this.sourceNameRepeatLimit = sourceNameRepeatLimit;
}

/**
* @deprecated Use {@link #setUseSourceNameAsClassNameAlias(UseSourceNameAsClassNameAlias)} instead.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,11 +302,18 @@ private SettingsGroup makeRenameGroup() {
needReload();
});

JSpinner repeatLimit = new JSpinner(new SpinnerNumberModel(settings.getSourceNameRepeatLimit(), 1, Integer.MAX_VALUE, 1));
repeatLimit.addChangeListener(e -> {
settings.setSourceNameRepeatLimit((Integer) repeatLimit.getValue());
needReload();
});

SettingsGroup group = new SettingsGroup(NLS.str("preferences.rename"));
group.addRow(NLS.str("preferences.rename_case"), renameCaseSensitive);
group.addRow(NLS.str("preferences.rename_valid"), renameValid);
group.addRow(NLS.str("preferences.rename_printable"), renamePrintable);
group.addRow(NLS.str("preferences.rename_use_source_name_as_class_name_alias"), useSourceNameAsClassNameAlias);
group.addRow(NLS.str("preferences.rename_source_name_repeat_limit"), repeatLimit);
return group;
}

Expand Down
1 change: 1 addition & 0 deletions jadx-gui/src/main/resources/i18n/Messages_de_DE.properties
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ preferences.rename_case=System unterscheidet zwischen Groß/Kleinschreibung
preferences.rename_valid=Ist eine gültige Kennung
preferences.rename_printable=Ist druckbar
preferences.rename_use_source_name_as_class_name_alias=Quelldateiname als Klassennamen-Alias verwenden
#preferences.rename_source_name_repeat_limit=Allow using source name if it appears less than a limit number
preferences.search_group_title=Ressourcen durchsuchen
#preferences.search_results_per_page=Results per page (0 - no limit)
preferences.res_file_ext=Dateierweiterungen (z.B. .xml|.html), * bedeutet alle
Expand Down
1 change: 1 addition & 0 deletions jadx-gui/src/main/resources/i18n/Messages_en_US.properties
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ preferences.rename_case=To fix case sensitivity issues
preferences.rename_valid=To make them valid
preferences.rename_printable=To make printable
preferences.rename_use_source_name_as_class_name_alias=Use source file name as class name alias
preferences.rename_source_name_repeat_limit=Allow using source name if it appears less than a limit number
preferences.search_group_title=Search
preferences.search_results_per_page=Results per page (0 - no limit)
preferences.res_file_ext=Resource files extensions ('xml|html', * for all)
Expand Down
1 change: 1 addition & 0 deletions jadx-gui/src/main/resources/i18n/Messages_es_ES.properties
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ preferences.reset_title=Reestablecer preferencias
#preferences.rename_valid=To make them valid
#preferences.rename_printable=To make printable
preferences.rename_use_source_name_as_class_name_alias=Usar el nombre del source como alias para la clase
#preferences.rename_source_name_repeat_limit=Allow using source name if it appears less than a limit number
#preferences.search_group_title=Search
#preferences.search_results_per_page=Results per page (0 - no limit)
#preferences.res_file_ext=Resource files extensions ('xml|html', * for all)
Expand Down
1 change: 1 addition & 0 deletions jadx-gui/src/main/resources/i18n/Messages_id_ID.properties
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ preferences.rename_case=Untuk memperbaiki masalah sensitivitas huruf besar-kecil
preferences.rename_valid=Untuk membuatnya valid
preferences.rename_printable=Untuk membuatnya dapat dicetak
preferences.rename_use_source_name_as_class_name_alias=Gunakan nama berkas sumber sebagai alias nama kelas
#preferences.rename_source_name_repeat_limit=Allow using source name if it appears less than a limit number
preferences.search_group_title=Pencarian
preferences.search_results_per_page=Hasil per halaman (0 - tanpa batas)
preferences.res_file_ext=Ekstensi berkas sumber daya ('xml|html', * untuk semua)
Expand Down
1 change: 1 addition & 0 deletions jadx-gui/src/main/resources/i18n/Messages_ko_KR.properties
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ preferences.rename_case=시스템 대소문자 구분
preferences.rename_valid=유효한 식별자로 바꾸기
preferences.rename_printable=출력 가능하게 바꾸기
preferences.rename_use_source_name_as_class_name_alias=소스 파일 이름을 클래스 이름 별칭으로 사용
#preferences.rename_source_name_repeat_limit=Allow using source name if it appears less than a limit number
preferences.search_group_title=리소스 검색
#preferences.search_results_per_page=Results per page (0 - no limit)
preferences.res_file_ext=파일 확장자 (예: .xml|.html) (* 은 전체를 의미)
Expand Down
1 change: 1 addition & 0 deletions jadx-gui/src/main/resources/i18n/Messages_pt_BR.properties
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ preferences.rename_case=Corrigir problemas de capitalização (case sensitivity)
preferences.rename_valid=Deixá-las válidas
preferences.rename_printable=Deixá-las imprimíveis (printable)
preferences.rename_use_source_name_as_class_name_alias=Utilizar nome do arquivo como apelido da classe
#preferences.rename_source_name_repeat_limit=Allow using source name if it appears less than a limit number
preferences.search_group_title=Buscar recursos
#preferences.search_results_per_page=Results per page (0 - no limit)
preferences.res_file_ext=Extensões de arquivos (ex: .xml|.html), * significa todas
Expand Down
1 change: 1 addition & 0 deletions jadx-gui/src/main/resources/i18n/Messages_ru_RU.properties
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ preferences.rename_case=И исправить проблемы именован
preferences.rename_valid=И сделать их верными
preferences.rename_printable=И сделать их доступными для печати
preferences.rename_use_source_name_as_class_name_alias=Иcпользовать атрибут SOURCE
#preferences.rename_source_name_repeat_limit=Allow using source name if it appears less than a limit number
preferences.search_group_title=Поиск
preferences.search_results_per_page=Результатов на страницу (0 - без лимита)
preferences.res_file_ext=Расширения файлов ресурсов ('xml|html', * для всех)
Expand Down
1 change: 1 addition & 0 deletions jadx-gui/src/main/resources/i18n/Messages_zh_CN.properties
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ preferences.rename_case=标识符要能够区分大小写
preferences.rename_valid=标识符应该符合标准规范
preferences.rename_printable=标识符必须要能正常显示
preferences.rename_use_source_name_as_class_name_alias=使用资源名作为类的别名
#preferences.rename_source_name_repeat_limit=Allow using source name if it appears less than a limit number
preferences.search_group_title=搜索资源
preferences.search_results_per_page=每页结果数(0 - 无限制)
preferences.res_file_ext=文件扩展名(比如 .xml|.html),* 表示所有
Expand Down
1 change: 1 addition & 0 deletions jadx-gui/src/main/resources/i18n/Messages_zh_TW.properties
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,7 @@ preferences.rename_case=以修復區分大小寫問題
preferences.rename_valid=以使其有效
preferences.rename_printable=以使其可列印
preferences.rename_use_source_name_as_class_name_alias=將原始檔案名稱作為類別別名
#preferences.rename_source_name_repeat_limit=Allow using source name if it appears less than a limit number
preferences.search_group_title=搜尋資源
preferences.search_results_per_page=每頁的搜尋結果數 (0 - 無限制)
preferences.res_file_ext=副檔名 (e.g. .xml|.html), * 表示全部
Expand Down

0 comments on commit a43b475

Please sign in to comment.