Skip to content

Commit

Permalink
fix: generics constructor types (PR #594)
Browse files Browse the repository at this point in the history
  • Loading branch information
asashour authored and skylot committed Apr 14, 2019
1 parent 4cb9f23 commit bcfed5b
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 23 deletions.
29 changes: 26 additions & 3 deletions jadx-core/src/main/java/jadx/core/codegen/InsnGen.java
Original file line number Diff line number Diff line change
Expand Up @@ -569,6 +569,21 @@ private void makeConstructor(ConstructorInsn insn, CodeWriter code)
} else {
code.add("new ");
useClass(code, insn.getClassType());
ArgType argType = insn.getResult().getSVar().getCodeVar().getType();
if (argType.isGeneric()) {
code.add('<');
if (insn.contains(AFlag.EXPLICIT_GENERICS)) {
boolean first = true;
for (ArgType type : argType.getGenericTypes()) {
if (!first) {
code.add(',');
}
mgen.getClassGen().useType(code, type);
first = false;
}
}
code.add('>');
}
}
MethodNode callMth = mth.dex().resolveMethod(insn.getCallMth());
generateMethodArguments(code, insn, 0, callMth);
Expand Down Expand Up @@ -749,9 +764,17 @@ private boolean processOverloadedArg(CodeWriter code, MethodNode callMth, InsnAr
if (argType.equals(origType)) {
return false;
}
if (origType.isGeneric()
&& origType.getObject().equals(argType.getObject())) {
return false;
if (origType.isGeneric()) {
if (!argType.isGeneric() && arg.isInsnWrap()) {
((InsnWrapArg) arg).getWrapInsn().getResult().setType(
ArgType.generic(argType.getObject(), origType.getGenericTypes()));
}
if (origType.getObject().equals(argType.getObject())) {
return false;
}
if (arg.isInsnWrap()) {
((InsnWrapArg) arg).getWrapInsn().add(AFlag.EXPLICIT_GENERICS);
}
}
code.add('(');
useType(code, origType);
Expand Down
2 changes: 2 additions & 0 deletions jadx-core/src/main/java/jadx/core/dex/attributes/AFlag.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,7 @@ public enum AFlag {

FALL_THROUGH,

EXPLICIT_GENERICS,

INCONSISTENT_CODE, // warning about incorrect decompilation
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

import org.junit.jupiter.api.Test;

import jadx.NotYetImplemented;
import jadx.core.dex.nodes.ClassNode;
import jadx.tests.api.IntegrationTest;

Expand Down Expand Up @@ -55,18 +54,6 @@ public void test() {
ClassNode cls = getClassNode(TestCls.class);
String code = cls.getCode().toString();

assertThat(code, containsOne("call(new ArrayList());"));
assertThat(code, containsOne("call((List<String>) new ArrayList());"));

assertThat(code, containsOne("call((String) obj);"));
}

@Test
@NotYetImplemented
public void testNYI() {
ClassNode cls = getClassNode(TestCls.class);
String code = cls.getCode().toString();

assertThat(code, containsOne("call(new ArrayList<>());"));
assertThat(code, containsOne("call((List<String>) new ArrayList<String>());"));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@ public class TestVariablesUsageWithLoops extends IntegrationTest {

public static class TestEnhancedFor {

@SuppressWarnings("rawtypes")
public void test() {
List list;
List<Object> list;
synchronized (this) {
list = new ArrayList();
list = new ArrayList<>();
}
for (Object o : list) {
System.out.println(o);
Expand All @@ -32,15 +31,15 @@ public void testEnhancedFor() {
ClassNode cls = getClassNode(TestEnhancedFor.class);
String code = cls.getCode().toString();

assertThat(code, containsString(" list = new ArrayList"));
assertThat(code, containsString(" list = new ArrayList<>"));
}

public static class TestForLoop {

public void test() {
List list;
List<Object> list;
synchronized (this) {
list = new ArrayList();
list = new ArrayList<>();
}
for (int i = 0; i < list.size(); i++) {
System.out.println(i);
Expand All @@ -53,6 +52,6 @@ public void testForLoop() {
ClassNode cls = getClassNode(TestForLoop.class);
String code = cls.getCode().toString();

assertThat(code, containsString(" list = new ArrayList"));
assertThat(code, containsString(" list = new ArrayList<>"));
}
}

0 comments on commit bcfed5b

Please sign in to comment.