Commit e4f4de6c authored by Skylot's avatar Skylot

core: fix imports for inner classes

parent e6aa85e0
...@@ -462,13 +462,14 @@ public class ClassGen { ...@@ -462,13 +462,14 @@ 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;
}
if (isBothClassesInOneTopClass(useCls, extClsInfo)) {
return shortName; return shortName;
} }
// don't add import if this class from same package // don't add import if this class from same package
...@@ -500,7 +501,6 @@ public class ClassGen { ...@@ -500,7 +501,6 @@ public class ClassGen {
addImport(extClsInfo); addImport(extClsInfo);
return shortName; return shortName;
} }
}
private void addImport(ClassInfo classInfo) { private void addImport(ClassInfo classInfo) {
if (parentGen != null) { if (parentGen != null) {
...@@ -518,6 +518,16 @@ public class ClassGen { ...@@ -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 { ...@@ -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,12 +32,11 @@ public class RenameVisitor extends AbstractVisitor { ...@@ -32,12 +32,11 @@ 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);
} }
} }
}
@Override @Override
public boolean visit(ClassNode cls) throws JadxException { public boolean visit(ClassNode cls) throws JadxException {
...@@ -60,7 +59,7 @@ public class RenameVisitor extends AbstractVisitor { ...@@ -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; ...@@ -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 { ...@@ -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; ...@@ -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 { ...@@ -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();
......
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