From 540787cc9906e1df1f4d367e1f7d3ca500463f50 Mon Sep 17 00:00:00 2001 From: Piszmog Date: Tue, 14 Aug 2018 19:20:58 -0600 Subject: [PATCH] handle nulls and add a test --- .../RecoverAnnotationRecoveryHandler.java | 14 +++++++++----- .../RecoverAnnotationRecoveryHandlerTests.java | 15 +++++++++++++-- 2 files changed, 22 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/springframework/retry/annotation/RecoverAnnotationRecoveryHandler.java b/src/main/java/org/springframework/retry/annotation/RecoverAnnotationRecoveryHandler.java index fd2db51..f4dac9d 100644 --- a/src/main/java/org/springframework/retry/annotation/RecoverAnnotationRecoveryHandler.java +++ b/src/main/java/org/springframework/retry/annotation/RecoverAnnotationRecoveryHandler.java @@ -16,6 +16,10 @@ package org.springframework.retry.annotation; +import java.lang.reflect.Method; +import java.util.HashMap; +import java.util.Map; + import org.springframework.classify.SubclassClassifier; import org.springframework.core.annotation.AnnotationUtils; import org.springframework.retry.ExhaustedRetryException; @@ -23,10 +27,6 @@ import org.springframework.retry.interceptor.MethodInvocationRecoverer; import org.springframework.util.ReflectionUtils; import org.springframework.util.ReflectionUtils.MethodCallback; -import java.lang.reflect.Method; -import java.util.HashMap; -import java.util.Map; - /** * A recoverer for method invocations based on the @Recover annotation. A * suitable recovery method is one with a Throwable type as the first parameter and the @@ -121,7 +121,11 @@ public class RecoverAnnotationRecoveryHandler implements MethodInvocationReco startingIndex = 1; } for (int i = startingIndex; i < parameterTypes.length; i++) { - if (parameterTypes[i] != args[i-1].getClass()) { + final Object argument = args[i-1]; + if (argument == null) { + continue; + } + if (parameterTypes[i] != argument.getClass()) { return false; } } diff --git a/src/test/java/org/springframework/retry/annotation/RecoverAnnotationRecoveryHandlerTests.java b/src/test/java/org/springframework/retry/annotation/RecoverAnnotationRecoveryHandlerTests.java index 5b4da88..d00da5c 100644 --- a/src/test/java/org/springframework/retry/annotation/RecoverAnnotationRecoveryHandlerTests.java +++ b/src/test/java/org/springframework/retry/annotation/RecoverAnnotationRecoveryHandlerTests.java @@ -115,7 +115,7 @@ public class RecoverAnnotationRecoveryHandlerTests { } @Test - public void multipleQualifyingRecoverMethods (){ + public void multipleQualifyingRecoverMethods(){ Method foo = ReflectionUtils.findMethod(MultipleQualifyingRecovers.class, "foo", String.class); RecoverAnnotationRecoveryHandler handler = new RecoverAnnotationRecoveryHandler( @@ -126,7 +126,18 @@ public class RecoverAnnotationRecoveryHandlerTests { } @Test - public void multipleQualifyingRecoverMethodsReOrdered (){ + public void multipleQualifyingRecoverMethodsWithNull(){ + Method foo = ReflectionUtils.findMethod(MultipleQualifyingRecovers.class, + "foo", String.class); + RecoverAnnotationRecoveryHandler handler = new RecoverAnnotationRecoveryHandler( + new MultipleQualifyingRecovers(), foo); + assertEquals(1, + handler.recover(new Object[] { null }, new RuntimeException("Planned"))); + + } + + @Test + public void multipleQualifyingRecoverMethodsReOrdered(){ Method foo = ReflectionUtils.findMethod(MultipleQualifyingRecoversReOrdered.class, "foo", String.class); RecoverAnnotationRecoveryHandler handler = new RecoverAnnotationRecoveryHandler(