fix: ignore source name if current alias is better (#1795)

This commit is contained in:
Skylot 2023-03-13 21:25:41 +00:00
parent e933b41236
commit 1891f6fd7e
No known key found for this signature in database
GPG Key ID: 1E23F5B52567AA39
4 changed files with 61 additions and 2 deletions

View File

@ -8,6 +8,7 @@ import jadx.core.deobf.NameMapper;
import jadx.core.dex.attributes.AFlag;
import jadx.core.dex.nodes.ClassNode;
import jadx.core.dex.nodes.RootNode;
import jadx.core.utils.BetterName;
import jadx.core.utils.StringUtils;
public class SourceFileRename {
@ -45,6 +46,15 @@ public class SourceFileRename {
if (otherCls != null) {
return null;
}
if (cls.getClassInfo().hasAlias()) {
// ignore source name if current alias is "better"
String currentAlias = cls.getAlias();
String betterName = BetterName.compareAndGet(name, currentAlias);
if (betterName.equals(currentAlias)) {
return null;
}
}
cls.remove(JadxAttrType.SOURCE_FILE);
return name;
}

View File

@ -25,9 +25,9 @@ public class BetterName {
boolean firstBetter = firstRating >= secondRating;
if (DEBUG) {
if (firstBetter) {
LOG.info("Better name: '{}' > '{}' ({} > {})", first, second, firstRating, secondRating);
LOG.debug("Better name: '{}' > '{}' ({} > {})", first, second, firstRating, secondRating);
} else {
LOG.info("Better name: '{}' > '{}' ({} > {})", second, first, secondRating, firstRating);
LOG.debug("Better name: '{}' > '{}' ({} > {})", second, first, secondRating, firstRating);
}
}
return firstBetter ? first : second;

View File

@ -0,0 +1,40 @@
package jadx.tests.integration.deobf;
import org.junit.jupiter.api.Test;
import jadx.tests.api.SmaliTest;
import static jadx.tests.api.utils.assertj.JadxAssertions.assertThat;
public class TestBadSourceFile extends SmaliTest {
@Test
public void test() {
// use source name disabled by default
enableDeobfuscation();
args.setDeobfuscationMinLength(100); // rename everything
assertThat(searchCls(loadFromSmaliFiles(), "b"))
.code()
.containsOne("class C0000b {");
}
@Test
public void testWithUseSourceName() {
args.setUseSourceNameAsClassAlias(true);
// deobfuscation disabled
assertThat(searchCls(loadFromSmaliFiles(), "b"))
.code()
.containsOne("class a {");
}
@Test
public void testWithUseSourceNameAndDeobf() {
args.setUseSourceNameAsClassAlias(true);
enableDeobfuscation();
args.setDeobfuscationMinLength(100); // rename everything
assertThat(searchCls(loadFromSmaliFiles(), "b"))
.code()
.containsOne("class C0000b {")
.containsOne("/* compiled from: a.java */");
}
}

View File

@ -0,0 +1,9 @@
.class Lb;
.super Ljava/lang/Object;
.source "a.java"
.method constructor <init>()V
.registers 1
invoke-direct {p0}, Ljava/lang/Object;-><init>()V
return-void
.end method