Skip to content

Commit

Permalink
fix: add missing import for class generics map (PR #480)
Browse files Browse the repository at this point in the history
* Fix missing import for class Generics map.
* Add import only when needed (in non-inner class declaration)
* Remove unneeded import
  • Loading branch information
asashour authored and skylot committed Mar 21, 2019
1 parent 9797fe5 commit dd2e7e8
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 5 deletions.
2 changes: 1 addition & 1 deletion jadx-cli/src/main/java/jadx/cli/JadxCLIArgs.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public boolean overrideProvided(String[] args) {
return process(jcw);
}

private boolean process(JCommanderWrapper jcw) {
private boolean process(JCommanderWrapper<?> jcw) {
if (printHelp) {
jcw.printUsage();
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public void encodeValue(CodeWriter code, Object val) {
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);
Expand Down
8 changes: 6 additions & 2 deletions jadx-core/src/main/java/jadx/core/codegen/ClassGen.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ public void addClassDeclaration(CodeWriter clsCode) {
clsCode.attachDefinition(cls);
clsCode.add(cls.getShortName());

addGenericMap(clsCode, cls.getGenericMap());
addGenericMap(clsCode, cls.getGenericMap(), true);
clsCode.add(' ');

ArgType sup = cls.getSuperClass();
Expand Down Expand Up @@ -175,7 +175,7 @@ public void addClassDeclaration(CodeWriter clsCode) {
}
}

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;
}
Expand All @@ -200,6 +200,10 @@ public boolean addGenericMap(CodeWriter code, Map<ArgType, List<ArgType>> gmap)
code.add(g.getObject());
} else {
useClass(code, g);

if (classDeclaration && !cls.getAlias().isInner()) {
addImport(ClassInfo.extCls(cls.root(), g));
}
}
if (it.hasNext()) {
code.add(" & ");
Expand Down
2 changes: 1 addition & 1 deletion jadx-core/src/main/java/jadx/core/codegen/MethodGen.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public boolean addDefinition(CodeWriter code) {
code.add(mth.isVirtual() ? "/* virtual */ " : "/* direct */ ");
}

if (classGen.addGenericMap(code, mth.getGenericMap())) {
if (classGen.addGenericMap(code, mth.getGenericMap(), false)) {
code.add(' ');
}
if (ai.isConstructor()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
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) {
}

}

0 comments on commit dd2e7e8

Please sign in to comment.