Look at the for loop, the first if branch, it only check the first type argument of the ParameterizedType and returned. When there are two type arguments, the result will be wrong.
private boolean isParameterizedTypeAssignable(ParameterizedType methodReturnType,
ParameterizedType failingMethodReturnType) {
Type[] methodActualArgs = methodReturnType.getActualTypeArguments();
Type[] failingMethodActualArgs = failingMethodReturnType.getActualTypeArguments();
if (methodActualArgs.length != failingMethodActualArgs.length) {
return false;
}
int startingIndex = 0;
for (int i = startingIndex; i < methodActualArgs.length; i++) {
Type methodArgType = methodActualArgs[i];
Type failingMethodArgType = failingMethodActualArgs[i];
if (methodArgType instanceof ParameterizedType && failingMethodArgType instanceof ParameterizedType) {
return isParameterizedTypeAssignable((ParameterizedType) methodArgType,
(ParameterizedType) failingMethodArgType);
}
if (methodArgType instanceof Class && failingMethodArgType instanceof Class
&& !failingMethodArgType.equals(methodArgType)) {
return false;
}
}
return true;
}
Here's the test:
public static void main(String[] args) throws NoSuchMethodException {
Class<?> clz = RecoverAnnotationRecoveryHandler.class;
Method m1 = clz.getDeclaredMethod("m1");
Method m2 = clz.getDeclaredMethod("m2");
ParameterizedType r1 = (ParameterizedType) m1.getGenericReturnType();
ParameterizedType r2 = (ParameterizedType) m2.getGenericReturnType();
System.out.println(isParameterizedTypeAssignable(r1, r2));
}
Map<List<String>, String> m1(){return null;}
Map<List<String>, Integer> m2(){return null;}
and the result is true, incorrect!
I make some correction, here's the code:
private static boolean isParameterizedTypeAssignable(ParameterizedType methodReturnType,
ParameterizedType failingMethodReturnType) {
Type[] methodActualArgs = methodReturnType.getActualTypeArguments();
Type[] failingMethodActualArgs = failingMethodReturnType.getActualTypeArguments();
if (methodActualArgs.length != failingMethodActualArgs.length) {
return false;
}
int startingIndex = 0;
for (int i = startingIndex; i < methodActualArgs.length; i++) {
Type methodArgType = methodActualArgs[i];
Type failingMethodArgType = failingMethodActualArgs[i];
if (methodArgType instanceof ParameterizedType && failingMethodArgType instanceof ParameterizedType) {
if (!isParameterizedTypeAssignable((ParameterizedType) methodArgType,
(ParameterizedType) failingMethodArgType);
return false;
} else if (methodArgType instanceof Class && failingMethodArgType instanceof Class) {
if (!failingMethodArgType.equals(methodArgType))
return false;
} else
return false;
}
return true;
}
and the result is false, which is correct.
Look at the for loop, the first if branch, it only check the first type argument of the ParameterizedType and returned. When there are two type arguments, the result will be wrong.
Here's the test:
and the result is
true, incorrect!I make some correction, here's the code:
and the result is
false, which is correct.