Commit dd2e7e87 authored by Ahmed Ashour's avatar Ahmed Ashour Committed by skylot

fix: add missing import for class generics map (PR #480)

* Fix missing import for class Generics map.
* Add import only when needed (in non-inner class declaration)
* Remove unneeded import
parent 9797fe5b
...@@ -107,7 +107,7 @@ public class JadxCLIArgs { ...@@ -107,7 +107,7 @@ public class JadxCLIArgs {
return process(jcw); return process(jcw);
} }
private boolean process(JCommanderWrapper jcw) { private boolean process(JCommanderWrapper<?> jcw) {
if (printHelp) { if (printHelp) {
jcw.printUsage(); jcw.printUsage();
return false; return false;
......
...@@ -151,7 +151,7 @@ public class AnnotationGen { ...@@ -151,7 +151,7 @@ public class AnnotationGen {
InsnGen.makeStaticFieldAccess(code, field, classGen); InsnGen.makeStaticFieldAccess(code, field, classGen);
} else if (val instanceof Iterable) { } else if (val instanceof Iterable) {
code.add('{'); code.add('{');
Iterator<?> it = ((Iterable) val).iterator(); Iterator<?> it = ((Iterable<?>) val).iterator();
while (it.hasNext()) { while (it.hasNext()) {
Object obj = it.next(); Object obj = it.next();
encodeValue(code, obj); encodeValue(code, obj);
......
...@@ -144,7 +144,7 @@ public class ClassGen { ...@@ -144,7 +144,7 @@ public class ClassGen {
clsCode.attachDefinition(cls); clsCode.attachDefinition(cls);
clsCode.add(cls.getShortName()); clsCode.add(cls.getShortName());
addGenericMap(clsCode, cls.getGenericMap()); addGenericMap(clsCode, cls.getGenericMap(), true);
clsCode.add(' '); clsCode.add(' ');
ArgType sup = cls.getSuperClass(); ArgType sup = cls.getSuperClass();
...@@ -175,7 +175,7 @@ public class ClassGen { ...@@ -175,7 +175,7 @@ public class ClassGen {
} }
} }
public boolean addGenericMap(CodeWriter code, Map<ArgType, List<ArgType>> gmap) { public boolean addGenericMap(CodeWriter code, Map<ArgType, List<ArgType>> gmap, boolean classDeclaration) {
if (gmap == null || gmap.isEmpty()) { if (gmap == null || gmap.isEmpty()) {
return false; return false;
} }
...@@ -200,6 +200,10 @@ public class ClassGen { ...@@ -200,6 +200,10 @@ public class ClassGen {
code.add(g.getObject()); code.add(g.getObject());
} else { } else {
useClass(code, g); useClass(code, g);
if (classDeclaration && !cls.getAlias().isInner()) {
addImport(ClassInfo.extCls(cls.root(), g));
}
} }
if (it.hasNext()) { if (it.hasNext()) {
code.add(" & "); code.add(" & ");
......
...@@ -90,7 +90,7 @@ public class MethodGen { ...@@ -90,7 +90,7 @@ public class MethodGen {
code.add(mth.isVirtual() ? "/* virtual */ " : "/* direct */ "); code.add(mth.isVirtual() ? "/* virtual */ " : "/* direct */ ");
} }
if (classGen.addGenericMap(code, mth.getGenericMap())) { if (classGen.addGenericMap(code, mth.getGenericMap(), false)) {
code.add(' '); code.add(' ');
} }
if (ai.isConstructor()) { if (ai.isConstructor()) {
......
package jadx.tests.integration.generics;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.assertThat;
import org.junit.Test;
import jadx.core.dex.nodes.ClassNode;
import jadx.tests.api.IntegrationTest;
public class TestImportGenericMap extends IntegrationTest {
@Test
public void test() {
ClassNode cls = getClassNode(SuperClass.class);
String code = cls.getCode().toString();
assertThat(code, containsString(
"import " + SuperClass.ToImport.class.getName().replace('$', '.') + ';'));
assertThat(code, not(containsString(
"import " + SuperClass.NotToImport.class.getName().replace('$', '.') + ';')));
}
}
final class SuperClass<O extends SuperClass.ToImport> {
interface ToImport {
}
interface NotToImport {
}
static final class Class1<C extends NotToImport> {
}
public <C extends NotToImport> SuperClass(Class1<C> zzf) {
}
}
\ No newline at end of file
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