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 {
return process(jcw);
}
private boolean process(JCommanderWrapper jcw) {
private boolean process(JCommanderWrapper<?> jcw) {
if (printHelp) {
jcw.printUsage();
return false;
......
......@@ -151,7 +151,7 @@ public class AnnotationGen {
InsnGen.makeStaticFieldAccess(code, field, classGen);
} else if (val instanceof Iterable) {
code.add('{');
Iterator<?> it = ((Iterable) val).iterator();
Iterator<?> it = ((Iterable<?>) val).iterator();
while (it.hasNext()) {
Object obj = it.next();
encodeValue(code, obj);
......
......@@ -144,7 +144,7 @@ public class ClassGen {
clsCode.attachDefinition(cls);
clsCode.add(cls.getShortName());
addGenericMap(clsCode, cls.getGenericMap());
addGenericMap(clsCode, cls.getGenericMap(), true);
clsCode.add(' ');
ArgType sup = cls.getSuperClass();
......@@ -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()) {
return false;
}
......@@ -200,6 +200,10 @@ public class ClassGen {
code.add(g.getObject());
} else {
useClass(code, g);
if (classDeclaration && !cls.getAlias().isInner()) {
addImport(ClassInfo.extCls(cls.root(), g));
}
}
if (it.hasNext()) {
code.add(" & ");
......
......@@ -90,7 +90,7 @@ public class MethodGen {
code.add(mth.isVirtual() ? "/* virtual */ " : "/* direct */ ");
}
if (classGen.addGenericMap(code, mth.getGenericMap())) {
if (classGen.addGenericMap(code, mth.getGenericMap(), false)) {
code.add(' ');
}
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