-
Notifications
You must be signed in to change notification settings - Fork 5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: improve type inference for generics in invoke insn (#927)
- Loading branch information
Showing
4 changed files
with
203 additions
and
24 deletions.
There are no files selected for viewing
78 changes: 78 additions & 0 deletions
78
jadx-core/src/main/java/jadx/core/dex/visitors/typeinference/TypeBoundInvokeUse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package jadx.core.dex.visitors.typeinference; | ||
|
||
import jadx.core.dex.instructions.BaseInvokeNode; | ||
import jadx.core.dex.instructions.args.ArgType; | ||
import jadx.core.dex.instructions.args.RegisterArg; | ||
import jadx.core.dex.nodes.RootNode; | ||
|
||
/** | ||
* Special dynamic bound for invoke with generics. | ||
* Arguments bound type calculated using instance generic type. | ||
*/ | ||
public final class TypeBoundInvokeUse implements ITypeBoundDynamic { | ||
private final RootNode root; | ||
private final BaseInvokeNode invokeNode; | ||
private final RegisterArg arg; | ||
private final ArgType genericArgType; | ||
|
||
public TypeBoundInvokeUse(RootNode root, BaseInvokeNode invokeNode, RegisterArg arg, ArgType genericArgType) { | ||
this.root = root; | ||
this.invokeNode = invokeNode; | ||
this.arg = arg; | ||
this.genericArgType = genericArgType; | ||
} | ||
|
||
@Override | ||
public BoundEnum getBound() { | ||
return BoundEnum.USE; | ||
} | ||
|
||
@Override | ||
public ArgType getType(TypeUpdateInfo updateInfo) { | ||
return getArgType(updateInfo.getType(invokeNode.getInstanceArg()), updateInfo.getType(arg)); | ||
} | ||
|
||
@Override | ||
public ArgType getType() { | ||
return getArgType(invokeNode.getInstanceArg().getType(), arg.getType()); | ||
} | ||
|
||
private ArgType getArgType(ArgType instanceType, ArgType argType) { | ||
ArgType resultGeneric = root.getTypeUtils().replaceClassGenerics(instanceType, genericArgType); | ||
if (resultGeneric != null) { | ||
return resultGeneric; | ||
} | ||
return argType; | ||
} | ||
|
||
@Override | ||
public RegisterArg getArg() { | ||
return arg; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) { | ||
return true; | ||
} | ||
if (o == null || getClass() != o.getClass()) { | ||
return false; | ||
} | ||
TypeBoundInvokeUse that = (TypeBoundInvokeUse) o; | ||
return invokeNode.equals(that.invokeNode); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return invokeNode.hashCode(); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "InvokeAssign{" + invokeNode.getCallMth().getShortId() | ||
+ ", argType=" + genericArgType | ||
+ ", currentType=" + getType() | ||
+ ", instanceArg=" + invokeNode.getInstanceArg() | ||
+ '}'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
jadx-core/src/test/java/jadx/tests/integration/types/TestGenerics5.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package jadx.tests.integration.types; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import jadx.tests.api.IntegrationTest; | ||
|
||
import static jadx.tests.api.utils.assertj.JadxAssertions.assertThat; | ||
|
||
public class TestGenerics5 extends IntegrationTest { | ||
|
||
public static class TestCls { | ||
private InheritableThreadLocal<Map<String, String>> inheritableThreadLocal; | ||
|
||
public void put(String key, String val) { | ||
if (key == null) { | ||
throw new IllegalArgumentException("key cannot be null"); | ||
} | ||
Map<String, String> map = this.inheritableThreadLocal.get(); | ||
if (map == null) { | ||
map = new HashMap<>(); | ||
this.inheritableThreadLocal.set(map); | ||
} | ||
map.put(key, val); | ||
} | ||
|
||
public void remove(String key) { | ||
Map<String, String> map = this.inheritableThreadLocal.get(); | ||
if (map != null) { | ||
map.remove(key); | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
public void test() { | ||
noDebugInfo(); | ||
assertThat(getClassNode(TestCls.class)) | ||
.code() | ||
.countString(2, "Map<String, String> map = this.inheritableThreadLocal.get();"); | ||
} | ||
} |