Skip to content

Commit

Permalink
fix: use correct imports for classes from default package (#2027)
Browse files Browse the repository at this point in the history
# Conflicts:
#	jadx-core/src/main/java/jadx/core/codegen/ClassGen.java
  • Loading branch information
skylot committed Oct 11, 2023
1 parent 6098f2b commit 4e23b9c
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 4 deletions.
8 changes: 4 additions & 4 deletions jadx-core/src/main/java/jadx/core/codegen/ClassGen.java
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,10 @@ private String useClassInternal(ClassInfo useCls, ClassInfo extClsInfo) {
if (useCls.equals(extClsInfo)) {
return shortName;
}
if (extClsInfo.getAliasPkg().isEmpty()) {
// omit import for default package
return shortName;
}
if (isClassInnerFor(useCls, extClsInfo)) {
return shortName;
}
Expand All @@ -678,10 +682,6 @@ private String useClassInternal(ClassInfo useCls, ClassInfo extClsInfo) {
if (extClsInfo.getPackage().equals(useCls.getPackage()) && !extClsInfo.isInner()) {
return shortName;
}
// ignore classes from default package
if (extClsInfo.isDefaultPackage()) {
return fullName;
}
if (extClsInfo.getAliasPkg().equals(useCls.getAliasPkg())) {
fullName = extClsInfo.getAliasNameWithoutPackage();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package jadx.tests.integration.names;

import java.util.Collections;
import java.util.List;

import org.junit.jupiter.api.Test;

import jadx.core.dex.nodes.ClassNode;
import jadx.tests.api.SmaliTest;

import static jadx.tests.api.utils.assertj.JadxAssertions.assertThat;

public class TestDefPkgRename extends SmaliTest {

@Test
public void test() {
List<ClassNode> clsList = loadFromSmaliFiles();
// class A moved to 'defpackage'
assertThat(searchCls(clsList, "A"))
.code()
.containsOne("package defpackage;");
assertThat(searchCls(clsList, "pkg.B"))
.code()
.containsOne("import defpackage.A;")
.containsOne("public A a;");
}

@Test
public void testNoImports() {
args.setUseImports(false);
List<ClassNode> clsList = loadFromSmaliFiles();
// class A moved to 'defpackage', but use full names
assertThat(searchCls(clsList, "A"))
.code()
.containsOne("package defpackage;");
assertThat(searchCls(clsList, "pkg.B"))
.code()
.doesNotContain("import")
.containsOne("public defpackage.A a;");
}

@Test
public void testDeobf() {
enableDeobfuscation();
List<ClassNode> clsList = loadFromSmaliFiles();
// package for class A deobfuscated
assertThat(searchCls(clsList, "pkg.B"))
.code()
.containsOne("import p000.C0000A;")
.containsOne("public C0000A f0a;");
}

@Test
public void testRenameDisabled() {
disableCompilation();
args.setRenameFlags(Collections.emptySet());
List<ClassNode> clsList = loadFromSmaliFiles();
// no renaming, code will not compile
assertThat(searchCls(clsList, "A"))
.code()
.containsOne("// default package");
assertThat(searchCls(clsList, "pkg.B"))
.code()
.doesNotContain("import") // omit import
.containsOne("public A a;");
}
}
2 changes: 2 additions & 0 deletions jadx-core/src/test/smali/names/TestDefPkgRename/a.smali
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.class public LA;
.super Ljava/lang/Object;
4 changes: 4 additions & 0 deletions jadx-core/src/test/smali/names/TestDefPkgRename/b.smali
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.class public Lpkg/B;
.super Ljava/lang/Object;

.field public a:LA;

0 comments on commit 4e23b9c

Please sign in to comment.