fix: resolve NPE while compare outer generic types
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user