core: fix classes import naming

This commit is contained in:
Skylot 2013-09-26 22:28:27 +04:00
parent 6bc2d3321c
commit 04ac3b2eb7
2 changed files with 74 additions and 15 deletions

View File

@ -13,6 +13,7 @@ import jadx.core.dex.info.ClassInfo;
import jadx.core.dex.instructions.args.ArgType;
import jadx.core.dex.instructions.args.InsnArg;
import jadx.core.dex.nodes.ClassNode;
import jadx.core.dex.nodes.DexNode;
import jadx.core.dex.nodes.FieldNode;
import jadx.core.dex.nodes.MethodNode;
import jadx.core.dex.nodes.parser.FieldValueAttr;
@ -314,7 +315,7 @@ public class ClassGen {
}
public String useClass(ClassInfo classInfo) {
String baseClass = useClassInternal(classInfo);
String baseClass = useClassInternal(cls.getClassInfo(), classInfo);
ArgType[] generics = classInfo.getType().getGenericTypes();
if (generics != null) {
StringBuilder sb = new StringBuilder();
@ -338,46 +339,68 @@ public class ClassGen {
}
}
private String useClassInternal(ClassInfo classInfo) {
if (parentGen != null)
return parentGen.useClassInternal(classInfo);
private String useClassInternal(ClassInfo useCls, ClassInfo classInfo) {
if (parentGen != null) {
return parentGen.useClassInternal(useCls, classInfo);
}
String clsStr = classInfo.getFullName();
if (fallback)
if (fallback) {
return clsStr;
}
String shortName = classInfo.getShortName();
if (classInfo.getPackage().equals("java.lang") && classInfo.getParentClass() == null) {
return shortName;
} else {
// don't add import if this class inner for current class
if (isInner(classInfo, cls.getClassInfo()))
if (isClassInnerFor(classInfo, useCls)) {
return shortName;
}
// don't add import if this class from same package
if (classInfo.getPackage().equals(cls.getPackage()))
if (classInfo.getPackage().equals(useCls.getPackage()) && !classInfo.isInner()) {
return shortName;
}
if (classInfo.getPackage().equals(useCls.getPackage())) {
clsStr = classInfo.getNameWithoutPackage();
}
if (searchCollision(cls.dex(), useCls, shortName)) {
return clsStr;
}
for (ClassInfo cls : imports) {
if (!cls.equals(classInfo)) {
if (cls.getShortName().equals(shortName))
if (cls.getShortName().equals(shortName)) {
return clsStr;
}
}
}
imports.add(classInfo);
return shortName;
}
}
private boolean isInner(ClassInfo inner, ClassInfo parent) {
private static boolean isClassInnerFor(ClassInfo inner, ClassInfo parent) {
if (inner.isInner()) {
ClassInfo p = inner.getParentClass();
return p.equals(parent) || isInner(p, parent);
return p.equals(parent) || isClassInnerFor(p, parent);
}
return false;
}
private static boolean searchCollision(DexNode dex, ClassInfo useCls, String shortName) {
if (useCls == null) {
return false;
}
if (useCls.getShortName().equals(shortName)) {
return true;
}
ClassNode classNode = dex.resolveClass(useCls);
for (ClassNode inner : classNode.getInnerClasses()) {
if (inner.getShortName().equals(shortName)) {
return true;
}
}
return searchCollision(dex, useCls.getParentClass(), shortName);
}
private void insertSourceFileInfo(CodeWriter code, AttrNode node) {
IAttribute sourceFileAttr = node.getAttributes().get(AttributeType.SOURCE_FILE);
if (sourceFileAttr != null) {

View File

@ -0,0 +1,36 @@
package jadx.samples;
public class TestInnerNames extends AbstractTest {
public int D;
public class A extends TestInner.MyThread {
public A(String name) {
super(name);
}
}
public class B extends A {
public B(String name) {
super(name);
}
public class C extends TestInner2.B {
}
}
public class C extends TestInner2.B {
}
public class D extends TestInner2.D {
}
@Override
public boolean testRun() throws Exception {
return true;
}
public static void main(String[] args) throws Exception {
new TestInnerNames().testRun();
}
}