core: fix imports for inner classes
This commit is contained in:
@@ -462,44 +462,44 @@ public class ClassGen {
|
|||||||
if (fallback) {
|
if (fallback) {
|
||||||
return fullName;
|
return fullName;
|
||||||
}
|
}
|
||||||
fullName = extClsInfo.getFullName();
|
|
||||||
String shortName = extClsInfo.getShortName();
|
String shortName = extClsInfo.getShortName();
|
||||||
if (extClsInfo.getPackage().equals("java.lang") && extClsInfo.getParentClass() == null) {
|
if (extClsInfo.getPackage().equals("java.lang") && extClsInfo.getParentClass() == null) {
|
||||||
return shortName;
|
return shortName;
|
||||||
} else {
|
}
|
||||||
// don't add import if this class inner for current class
|
if (isClassInnerFor(useCls, extClsInfo)) {
|
||||||
if (isClassInnerFor(extClsInfo, useCls)) {
|
|
||||||
return shortName;
|
|
||||||
}
|
|
||||||
// don't add import if this class from same package
|
|
||||||
if (extClsInfo.getPackage().equals(useCls.getPackage()) && !extClsInfo.isInner()) {
|
|
||||||
return shortName;
|
|
||||||
}
|
|
||||||
// don't add import if class not public (must be accessed using inheritance)
|
|
||||||
ClassNode classNode = cls.dex().resolveClass(extClsInfo);
|
|
||||||
if (classNode != null && !classNode.getAccessFlags().isPublic()) {
|
|
||||||
return shortName;
|
|
||||||
}
|
|
||||||
if (searchCollision(cls.dex(), useCls, extClsInfo)) {
|
|
||||||
return fullName;
|
|
||||||
}
|
|
||||||
if (extClsInfo.getPackage().equals(useCls.getPackage())) {
|
|
||||||
fullName = extClsInfo.getNameWithoutPackage();
|
|
||||||
}
|
|
||||||
for (ClassInfo importCls : getImports()) {
|
|
||||||
if (!importCls.equals(extClsInfo)
|
|
||||||
&& importCls.getShortName().equals(shortName)) {
|
|
||||||
if (extClsInfo.isInner()) {
|
|
||||||
String parent = useClassInternal(useCls, extClsInfo.getParentClass().getAlias());
|
|
||||||
return parent + "." + shortName;
|
|
||||||
} else {
|
|
||||||
return fullName;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
addImport(extClsInfo);
|
|
||||||
return shortName;
|
return shortName;
|
||||||
}
|
}
|
||||||
|
if (isBothClassesInOneTopClass(useCls, extClsInfo)) {
|
||||||
|
return shortName;
|
||||||
|
}
|
||||||
|
// don't add import if this class from same package
|
||||||
|
if (extClsInfo.getPackage().equals(useCls.getPackage()) && !extClsInfo.isInner()) {
|
||||||
|
return shortName;
|
||||||
|
}
|
||||||
|
// don't add import if class not public (must be accessed using inheritance)
|
||||||
|
ClassNode classNode = cls.dex().resolveClass(extClsInfo);
|
||||||
|
if (classNode != null && !classNode.getAccessFlags().isPublic()) {
|
||||||
|
return shortName;
|
||||||
|
}
|
||||||
|
if (searchCollision(cls.dex(), useCls, extClsInfo)) {
|
||||||
|
return fullName;
|
||||||
|
}
|
||||||
|
if (extClsInfo.getPackage().equals(useCls.getPackage())) {
|
||||||
|
fullName = extClsInfo.getNameWithoutPackage();
|
||||||
|
}
|
||||||
|
for (ClassInfo importCls : getImports()) {
|
||||||
|
if (!importCls.equals(extClsInfo)
|
||||||
|
&& importCls.getShortName().equals(shortName)) {
|
||||||
|
if (extClsInfo.isInner()) {
|
||||||
|
String parent = useClassInternal(useCls, extClsInfo.getParentClass().getAlias());
|
||||||
|
return parent + "." + shortName;
|
||||||
|
} else {
|
||||||
|
return fullName;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
addImport(extClsInfo);
|
||||||
|
return shortName;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void addImport(ClassInfo classInfo) {
|
private void addImport(ClassInfo classInfo) {
|
||||||
@@ -518,6 +518,16 @@ public class ClassGen {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static boolean isBothClassesInOneTopClass(ClassInfo useCls, ClassInfo extClsInfo) {
|
||||||
|
ClassInfo a = useCls.getTopParentClass();
|
||||||
|
ClassInfo b = extClsInfo.getTopParentClass();
|
||||||
|
if (a != null) {
|
||||||
|
return a.equals(b);
|
||||||
|
}
|
||||||
|
// useCls - is a top class
|
||||||
|
return useCls.equals(b);
|
||||||
|
}
|
||||||
|
|
||||||
private static boolean isClassInnerFor(ClassInfo inner, ClassInfo parent) {
|
private static boolean isClassInnerFor(ClassInfo inner, ClassInfo parent) {
|
||||||
if (inner.isInner()) {
|
if (inner.isInner()) {
|
||||||
ClassInfo p = inner.getParentClass();
|
ClassInfo p = inner.getParentClass();
|
||||||
|
|||||||
@@ -95,12 +95,13 @@ public final class ClassInfo {
|
|||||||
parentClass = null;
|
parentClass = null;
|
||||||
}
|
}
|
||||||
this.name = clsName;
|
this.name = clsName;
|
||||||
this.fullName = makeFullClsName(clsName);
|
this.fullName = makeFullClsName(clsName, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
public String makeFullClsName(String shortName) {
|
public String makeFullClsName(String shortName, boolean raw) {
|
||||||
if (parentClass != null) {
|
if (parentClass != null) {
|
||||||
return parentClass.fullName + "." + shortName;
|
String innerSep = raw ? "$" : ".";
|
||||||
|
return parentClass.makeFullClsName(parentClass.getShortName(), raw) + innerSep + shortName;
|
||||||
}
|
}
|
||||||
return pkg.isEmpty() ? shortName : pkg + "." + shortName;
|
return pkg.isEmpty() ? shortName : pkg + "." + shortName;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,10 +32,9 @@ public class RenameVisitor extends AbstractVisitor {
|
|||||||
if (deobfuscationOn) {
|
if (deobfuscationOn) {
|
||||||
// TODO: check classes for case sensitive names (issue #24)
|
// TODO: check classes for case sensitive names (issue #24)
|
||||||
deobfuscator.execute();
|
deobfuscator.execute();
|
||||||
} else {
|
}
|
||||||
for (ClassNode classNode : root.getClasses(true)) {
|
for (ClassNode classNode : root.getClasses(true)) {
|
||||||
checkClassName(classNode);
|
checkClassName(classNode);
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -60,7 +59,7 @@ public class RenameVisitor extends AbstractVisitor {
|
|||||||
newShortName = "C" + clsName;
|
newShortName = "C" + clsName;
|
||||||
}
|
}
|
||||||
if (newShortName != null) {
|
if (newShortName != null) {
|
||||||
classInfo.rename(cls.dex(), classInfo.makeFullClsName(newShortName));
|
classInfo.rename(cls.dex(), classInfo.makeFullClsName(newShortName, true));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package jadx.tests.integration.inner;
|
|||||||
import jadx.core.dex.nodes.ClassNode;
|
import jadx.core.dex.nodes.ClassNode;
|
||||||
import jadx.tests.api.IntegrationTest;
|
import jadx.tests.api.IntegrationTest;
|
||||||
|
|
||||||
import org.junit.Ignore;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import static org.hamcrest.CoreMatchers.containsString;
|
import static org.hamcrest.CoreMatchers.containsString;
|
||||||
@@ -48,7 +47,6 @@ public class TestAnonymousClass2 extends IntegrationTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Ignore
|
|
||||||
public void test() {
|
public void test() {
|
||||||
ClassNode cls = getClassNode(TestCls.class);
|
ClassNode cls = getClassNode(TestCls.class);
|
||||||
String code = cls.getCode().toString();
|
String code = cls.getCode().toString();
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ package jadx.tests.integration.inner;
|
|||||||
import jadx.core.dex.nodes.ClassNode;
|
import jadx.core.dex.nodes.ClassNode;
|
||||||
import jadx.tests.api.IntegrationTest;
|
import jadx.tests.api.IntegrationTest;
|
||||||
|
|
||||||
import org.junit.Ignore;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import static jadx.tests.api.utils.JadxMatchers.containsOne;
|
import static jadx.tests.api.utils.JadxMatchers.containsOne;
|
||||||
@@ -33,7 +32,6 @@ public class TestAnonymousClass4 extends IntegrationTest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
@Ignore
|
|
||||||
public void test() {
|
public void test() {
|
||||||
ClassNode cls = getClassNode(TestCls.class);
|
ClassNode cls = getClassNode(TestCls.class);
|
||||||
String code = cls.getCode().toString();
|
String code = cls.getCode().toString();
|
||||||
|
|||||||
Reference in New Issue
Block a user