Skip to content

Commit

Permalink
fix: improve wildcard types compare (#857)
Browse files Browse the repository at this point in the history
  • Loading branch information
skylot committed Feb 17, 2020
1 parent 1336c47 commit e444ecb
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,12 @@ private TypeCompareEnum compareObjects(ArgType first, ArgType second) {
if (firstGeneric != secondGeneric) {
return firstGeneric ? NARROW_BY_GENERIC : WIDER_BY_GENERIC;
}
// both generics on same object, compare generics arrays
// both generics on same object
if (first.getWildcardBound() != null && second.getWildcardBound() != null) {
// both wildcards
return compareWildcardTypes(first, second);
}
// compare generics arrays
ArgType[] firstGenericTypes = first.getGenericTypes();
ArgType[] secondGenericTypes = second.getGenericTypes();
int len = firstGenericTypes.length;
Expand Down Expand Up @@ -198,6 +203,22 @@ private TypeCompareEnum compareObjects(ArgType first, ArgType second) {
return TypeCompareEnum.CONFLICT;
}

private TypeCompareEnum compareWildcardTypes(ArgType first, ArgType second) {
WildcardBound firstWildcardBound = first.getWildcardBound();
WildcardBound secondWildcardBound = second.getWildcardBound();
if (firstWildcardBound == WildcardBound.UNBOUND) {
return WIDER;
}
if (secondWildcardBound == WildcardBound.UNBOUND) {
return NARROW;
}
TypeCompareEnum wildcardCompare = compareTypes(first.getWildcardType(), second.getWildcardType());
if (firstWildcardBound == secondWildcardBound) {
return wildcardCompare;
}
return CONFLICT;
}

private TypeCompareEnum compareGenericTypeWithObject(ArgType genericType, ArgType objType) {
List<ArgType> extendTypes = genericType.getExtendTypes();
if (extendTypes == null || extendTypes.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import jadx.NotYetImplementedExtension;
import jadx.api.JadxArgs;
import jadx.core.dex.instructions.args.ArgType;
import jadx.core.dex.instructions.args.ArgType.WildcardBound;
import jadx.core.dex.nodes.RootNode;

import static jadx.core.dex.instructions.args.ArgType.BOOLEAN;
Expand All @@ -28,6 +29,7 @@
import static jadx.core.dex.instructions.args.ArgType.UNKNOWN_OBJECT;
import static jadx.core.dex.instructions.args.ArgType.array;
import static jadx.core.dex.instructions.args.ArgType.generic;
import static jadx.core.dex.instructions.args.ArgType.object;
import static jadx.core.dex.instructions.args.ArgType.wildcard;
import static org.assertj.core.api.Assertions.assertThat;

Expand Down Expand Up @@ -83,7 +85,7 @@ public void compareArrays() {

check(array(OBJECT), array(INT), TypeCompareEnum.CONFLICT);

ArgType integerType = ArgType.object("java.lang.Integer");
ArgType integerType = object("java.lang.Integer");
check(array(OBJECT), array(integerType), TypeCompareEnum.WIDER);
check(array(INT), array(integerType), TypeCompareEnum.CONFLICT);
check(array(INT), array(INT), TypeCompareEnum.EQUAL);
Expand All @@ -95,8 +97,8 @@ public void compareArrays() {

@Test
public void compareGenerics() {
ArgType mapCls = ArgType.object("java.util.Map");
ArgType setCls = ArgType.object("java.util.Set");
ArgType mapCls = object("java.util.Map");
ArgType setCls = object("java.util.Set");

ArgType keyType = ArgType.genericType("K");
ArgType valueType = ArgType.genericType("V");
Expand All @@ -113,6 +115,20 @@ public void compareGenerics() {
// TODO implement compare for wildcard with bounds
}

@Test
public void compareWildCards() {
ArgType clsWildcard = generic(CLASS.getObject(), wildcard());
ArgType clsExtendedWildcard = generic(CLASS.getObject(), wildcard(STRING, WildcardBound.EXTENDS));
check(clsWildcard, clsExtendedWildcard, TypeCompareEnum.WIDER);

ArgType listWildcard = generic(CLASS.getObject(), wildcard(object("java.util.List"), WildcardBound.EXTENDS));
ArgType collWildcard = generic(CLASS.getObject(), wildcard(object("java.util.Collection"), WildcardBound.EXTENDS));
check(listWildcard, collWildcard, TypeCompareEnum.NARROW);

ArgType collSuperWildcard = generic(CLASS.getObject(), wildcard(object("java.util.Collection"), WildcardBound.SUPER));
check(collSuperWildcard, listWildcard, TypeCompareEnum.CONFLICT);
}

@Test
public void compareGenericTypes() {
ArgType vType = ArgType.genericType("V");
Expand Down

0 comments on commit e444ecb

Please sign in to comment.