Skip to content

Throwable as first argument of explicit recover method should be optional #371

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
* @author Artem Bilan
* @author Gianluca Medici
* @author Lijinliang
* @author Yanming Zhou
*/
public class RecoverAnnotationRecoveryHandler<T> implements MethodInvocationRecoverer<T> {

Expand Down Expand Up @@ -130,7 +131,7 @@ private Method findClosestMatch(Object[] args, Class<? extends Throwable> cause)
}
else if (distance == min) {
boolean parametersMatch = compareParameters(args, meta.getArgCount(),
method.getParameterTypes());
method.getParameterTypes(), false);
if (parametersMatch) {
result = method;
}
Expand All @@ -143,8 +144,8 @@ else if (distance == min) {
Method method = entry.getKey();
if (method.getName().equals(this.recoverMethodName)) {
SimpleMetadata meta = entry.getValue();
if (meta.type.isAssignableFrom(cause)
&& compareParameters(args, meta.getArgCount(), method.getParameterTypes())) {
if ((meta.type == null || meta.type.isAssignableFrom(cause))
&& compareParameters(args, meta.getArgCount(), method.getParameterTypes(), true)) {
result = method;
break;
}
Expand All @@ -164,8 +165,9 @@ private int calculateDistance(Class<? extends Throwable> cause, Class<? extends
return result;
}

private boolean compareParameters(Object[] args, int argCount, Class<?>[] parameterTypes) {
if (argCount == (args.length + 1)) {
private boolean compareParameters(Object[] args, int argCount, Class<?>[] parameterTypes,
boolean withRecoverMethodName) {
if ((withRecoverMethodName && argCount == args.length) || argCount == (args.length + 1)) {
int startingIndex = 0;
if (parameterTypes.length > 0 && Throwable.class.isAssignableFrom(parameterTypes[0])) {
startingIndex = 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,24 @@ public void recovery() {
context.close();
}

@Test
public void recoveryWithoutParam() {
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
TestConfiguration.class)) {
RecoverableService service = context.getBean(RecoverableService.class);
assertThat(service.serviceWithoutParam()).isEqualTo("test");
}
}

@Test
public void recoveryWithParam() {
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(
TestConfiguration.class)) {
RecoverableService service = context.getBean(RecoverableService.class);
assertThat(service.serviceWithParam("test")).isEqualTo("test");
}
}

@Test
public void type() {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class);
Expand Down Expand Up @@ -637,6 +655,26 @@ public void recover(Throwable cause) {
this.cause = cause;
}

@Retryable(retryFor = RuntimeException.class, recover = "recoverWithoutParam")
public String serviceWithoutParam() {
throw new RuntimeException("Planned");
}

@Recover
public String recoverWithoutParam() {
return "test";
}

@Retryable(retryFor = RuntimeException.class, recover = "recoverWithParam")
public String serviceWithParam(String param) {
throw new RuntimeException("Planned");
}

@Recover
public String recoverWithParam(String param) {
return param;
}

public Throwable getCause() {
return this.cause;
}
Expand Down