fix: resolve NPE while compare outer generic types

This commit is contained in:
Skylot
2020-02-29 19:55:16 +00:00
parent 5eec8f754d
commit 89dbae8f8e
2 changed files with 26 additions and 7 deletions
@@ -174,15 +174,24 @@ public class TypeCompare {
// both wildcards
return compareWildcardTypes(first, second);
}
// compare generics arrays
ArgType[] firstGenericTypes = first.getGenericTypes();
ArgType[] secondGenericTypes = second.getGenericTypes();
int len = firstGenericTypes.length;
if (len == secondGenericTypes.length) {
for (int i = 0; i < len; i++) {
TypeCompareEnum res = compareTypes(firstGenericTypes[i], secondGenericTypes[i]);
if (res != EQUAL) {
return res;
if (firstGenericTypes == null || secondGenericTypes == null) {
// check outer types
ArgType firstOuterType = first.getOuterType();
ArgType secondOuterType = second.getOuterType();
if (firstOuterType != null && secondOuterType != null) {
return compareTypes(firstOuterType, secondOuterType);
}
} else {
// compare generics arrays
int len = firstGenericTypes.length;
if (len == secondGenericTypes.length) {
for (int i = 0; i < len; i++) {
TypeCompareEnum res = compareTypes(firstGenericTypes[i], secondGenericTypes[i]);
if (res != EQUAL) {
return res;
}
}
}
}
@@ -155,6 +155,16 @@ public class TypeCompareTest {
check(vType, ArgType.STRING, TypeCompareEnum.CONFLICT);
}
@Test
public void compareOuterGenerics() {
ArgType hashMapType = object("java.util.HashMap");
ArgType innerEntrySetType = object("EntrySet");
ArgType firstInstance = ArgType.outerGeneric(generic(hashMapType, STRING, STRING), innerEntrySetType);
ArgType secondInstance = ArgType.outerGeneric(generic(hashMapType, OBJECT, OBJECT), innerEntrySetType);
check(firstInstance, secondInstance, TypeCompareEnum.NARROW);
}
private void firstIsNarrow(ArgType first, ArgType second) {
check(first, second, TypeCompareEnum.NARROW);
}