From 8f3ceedd29d42b04c157e9fc21d4f5426cd71cdb Mon Sep 17 00:00:00 2001 From: Aaron Lucia Date: Tue, 7 Apr 2020 11:46:22 -0400 Subject: [PATCH] Support @Recovery method that is not defined in interface --- .../RecoverAnnotationRecoveryHandler.java | 2 +- .../retry/annotation/EnableRetryTests.java | 39 +++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/springframework/retry/annotation/RecoverAnnotationRecoveryHandler.java b/src/main/java/org/springframework/retry/annotation/RecoverAnnotationRecoveryHandler.java index 0a1d4b5..edcde9d 100644 --- a/src/main/java/org/springframework/retry/annotation/RecoverAnnotationRecoveryHandler.java +++ b/src/main/java/org/springframework/retry/annotation/RecoverAnnotationRecoveryHandler.java @@ -166,7 +166,7 @@ public class RecoverAnnotationRecoveryHandler implements MethodInvocationReco if (retryable != null) { this.recoverMethodName = retryable.recover(); } - ReflectionUtils.doWithMethods(failingMethod.getDeclaringClass(), new MethodCallback() { + ReflectionUtils.doWithMethods(target.getClass(), new MethodCallback() { @Override public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException { Recover recover = AnnotationUtils.findAnnotation(method, Recover.class); diff --git a/src/test/java/org/springframework/retry/annotation/EnableRetryTests.java b/src/test/java/org/springframework/retry/annotation/EnableRetryTests.java index 2658879..da0da2f 100644 --- a/src/test/java/org/springframework/retry/annotation/EnableRetryTests.java +++ b/src/test/java/org/springframework/retry/annotation/EnableRetryTests.java @@ -181,6 +181,14 @@ public class EnableRetryTests { context.close(); } + @Test + public void testInterfaceWithNoRecover() { + AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class); + NoRecoverInterface service = context.getBean(NoRecoverInterface.class); + service.service(); + assertTrue(service.isRecovered()); + } + @Test public void testImplementation() { AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TestConfiguration.class); @@ -351,6 +359,11 @@ public class EnableRetryTests { return new TheClass(); } + @Bean + public NoRecoverInterface anInterfaceWithNoRecover() { + return new NoRecoverClass(); + } + @Bean public NotAnnotatedInterface notAnnotatedInterface() { return new RetryableImplementation(); @@ -637,6 +650,32 @@ public class EnableRetryTests { } + public static interface NoRecoverInterface { + void service(); + boolean isRecovered(); + } + + public static class NoRecoverClass implements NoRecoverInterface { + + private boolean recovered; + + @Override + @Retryable + public void service() { + throw new RuntimeException("Planned"); + } + + @Recover + public void recover(Exception e) { + this.recovered = true; + } + + @Override + public boolean isRecovered() { + return this.recovered; + } + } + public static interface NotAnnotatedInterface { void service1();