Skip to content

[code error] in org.springframework.retry.annotation.RecoverAnnotationRecoveryHandler#isParameterizedTypeAssignable #328

@lijinliangyihao

Description

@lijinliangyihao

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions