Skip to content

Commit

Permalink
GH-328: Fix RecoverAnnRH return types matching
Browse files Browse the repository at this point in the history
Fixes #328

The loop in the `RecoverAnnotationRecoveryHandler.isParameterizedTypeAssignable`
only checks the first type argument of the `ParameterizedType` and returned.
When there are two type arguments, the result will be wrong.

* Fix `RecoverAnnotationRecoveryHandler.isParameterizedTypeAssignable` to continue matching
for next generic arguments
  • Loading branch information
jinliang.li authored and artembilan committed Feb 8, 2023
1 parent 8730729 commit 065c309
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2006-2022 the original author or authors.
* Copyright 2006-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -53,6 +53,7 @@
* @author Gary Russell
* @author Artem Bilan
* @author Gianluca Medici
* @author Lijinliang
*/
public class RecoverAnnotationRecoveryHandler<T> implements MethodInvocationRecoverer<T> {

Expand Down Expand Up @@ -224,12 +225,12 @@ else if (recover != null && candidate.getReturnType().isAssignableFrom(failingMe
* Returns {@code true} if the input methodReturnType is a direct match of the
* failingMethodReturnType. Takes nested generics into consideration as well, while
* deciding a match.
* @param methodReturnType
* @param failingMethodReturnType
* @param methodReturnType the method return type
* @param failingMethodReturnType the failing method return type
* @return true if the parameterized return types match.
* @since 1.3.2
*/
private boolean isParameterizedTypeAssignable(ParameterizedType methodReturnType,
private static boolean isParameterizedTypeAssignable(ParameterizedType methodReturnType,
ParameterizedType failingMethodReturnType) {

Type[] methodActualArgs = methodReturnType.getActualTypeArguments();
Expand All @@ -242,11 +243,18 @@ private boolean isParameterizedTypeAssignable(ParameterizedType methodReturnType
Type methodArgType = methodActualArgs[i];
Type failingMethodArgType = failingMethodActualArgs[i];
if (methodArgType instanceof ParameterizedType && failingMethodArgType instanceof ParameterizedType) {
return isParameterizedTypeAssignable((ParameterizedType) methodArgType,
(ParameterizedType) failingMethodArgType);
if (!isParameterizedTypeAssignable((ParameterizedType) methodArgType,
(ParameterizedType) failingMethodArgType)) {

return false;
}
}
else if (methodArgType instanceof Class && failingMethodArgType instanceof Class) {
if (!failingMethodArgType.equals(methodArgType)) {
return false;
}
}
if (methodArgType instanceof Class && failingMethodArgType instanceof Class
&& !failingMethodArgType.equals(methodArgType)) {
else if (!methodArgType.equals(failingMethodArgType)) {
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2022 the original author or authors.
* Copyright 2013-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -21,7 +21,9 @@
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
Expand All @@ -44,9 +46,31 @@
* @author Nathanaël Roberts
* @author Maksim Kita
* @author Gianluca Medici
* @author Lijinliang
*/
public class RecoverAnnotationRecoveryHandlerTests {

@Test
public void genericReturnTypesMatch() throws InvocationTargetException, IllegalAccessException {
Method isParameterizedTypeAssignable =
ReflectionUtils.findMethod(RecoverAnnotationRecoveryHandler.class, "isParameterizedTypeAssignable",
ParameterizedType.class, ParameterizedType.class);
isParameterizedTypeAssignable.setAccessible(true);

assertThat(isParameterizedTypeAssignable.invoke(null, getGenericReturnTypeByName("m1"),
getGenericReturnTypeByName("m2"))).isEqualTo(Boolean.TRUE);
assertThat(isParameterizedTypeAssignable.invoke(null, getGenericReturnTypeByName("m2"),
getGenericReturnTypeByName("m2_1"))).isEqualTo(Boolean.FALSE);
assertThat(isParameterizedTypeAssignable.invoke(null, getGenericReturnTypeByName("m3"),
getGenericReturnTypeByName("m4"))).isEqualTo(Boolean.FALSE);
assertThat(isParameterizedTypeAssignable.invoke(null, getGenericReturnTypeByName("m5"),
getGenericReturnTypeByName("m6"))).isEqualTo(Boolean.TRUE);
}

private static ParameterizedType getGenericReturnTypeByName(String name) {
return (ParameterizedType) ReflectionUtils.findMethod(ParameterTest.class, name).getGenericReturnType();
}

@Test
public void defaultRecoverMethod() {
RecoverAnnotationRecoveryHandler<?> handler = new RecoverAnnotationRecoveryHandler<Integer>(
Expand Down Expand Up @@ -293,6 +317,38 @@ public void recoverByComposedRetryableAnnotationName() {
assertThat(handler.recover(new Object[] { "Kevin" }, new RuntimeException("Planned"))).isEqualTo(4);
}

private static class ParameterTest<T, M> {

List<T> m1() {
return null;
}

List<T> m2() {
return null;
}

List<M> m2_1() {
return null;
}

Map<List<String>, Byte> m3() {
return null;
}

Map<List<String>, Integer> m4() {
return null;
}

Map<List<Integer>, Byte> m5() {
return null;
}

Map<List<Integer>, Byte> m6() {
return null;
}

}

private static class InAccessibleRecover {

@Retryable
Expand Down

0 comments on commit 065c309

Please sign in to comment.