Commit e4f4de6c authored by Skylot's avatar Skylot

core: fix imports for inner classes

parent e6aa85e0
......@@ -462,44 +462,44 @@ public class ClassGen {
if (fallback) {
return fullName;
}
fullName = extClsInfo.getFullName();
String shortName = extClsInfo.getShortName();
if (extClsInfo.getPackage().equals("java.lang") && extClsInfo.getParentClass() == null) {
return shortName;
} else {
// don't add import if this class inner for current class
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;
}
}
if (isClassInnerFor(useCls, extClsInfo)) {
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;
}
addImport(extClsInfo);
return shortName;
}
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) {
if (inner.isInner()) {
ClassInfo p = inner.getParentClass();
......
......@@ -95,12 +95,13 @@ public final class ClassInfo {
parentClass = null;
}
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) {
return parentClass.fullName + "." + shortName;
String innerSep = raw ? "$" : ".";
return parentClass.makeFullClsName(parentClass.getShortName(), raw) + innerSep + shortName;
}
return pkg.isEmpty() ? shortName : pkg + "." + shortName;
}
......
......@@ -32,10 +32,9 @@ public class RenameVisitor extends AbstractVisitor {
if (deobfuscationOn) {
// TODO: check classes for case sensitive names (issue #24)
deobfuscator.execute();
} else {
for (ClassNode classNode : root.getClasses(true)) {
checkClassName(classNode);
}
}
for (ClassNode classNode : root.getClasses(true)) {
checkClassName(classNode);
}
}
......@@ -60,7 +59,7 @@ public class RenameVisitor extends AbstractVisitor {
newShortName = "C" + clsName;
}
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.tests.api.IntegrationTest;
import org.junit.Ignore;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.containsString;
......@@ -48,7 +47,6 @@ public class TestAnonymousClass2 extends IntegrationTest {
}
@Test
@Ignore
public void test() {
ClassNode cls = getClassNode(TestCls.class);
String code = cls.getCode().toString();
......
......@@ -3,7 +3,6 @@ package jadx.tests.integration.inner;
import jadx.core.dex.nodes.ClassNode;
import jadx.tests.api.IntegrationTest;
import org.junit.Ignore;
import org.junit.Test;
import static jadx.tests.api.utils.JadxMatchers.containsOne;
......@@ -33,7 +32,6 @@ public class TestAnonymousClass4 extends IntegrationTest {
}
@Test
@Ignore
public void test() {
ClassNode cls = getClassNode(TestCls.class);
String code = cls.getCode().toString();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment