From 7e0c8127f4dfc09742e2f97c48e0f424f0ee0c28 Mon Sep 17 00:00:00 2001 From: dsyer Date: Fri, 28 Sep 2007 08:28:56 +0000 Subject: [PATCH] Throw illegal state exception (unlikely) wrong MethodInvocation type detected --- .../aop/RepeatOperationsInterceptor.java | 2 + .../retry/aop/RetryOperationsInterceptor.java | 2 + .../aop/RepeatOperationsInterceptorTests.java | 45 ++++++++++++++++--- .../aop/RetryOperationsInterceptorTests.java | 33 ++++++++++++++ 4 files changed, 75 insertions(+), 7 deletions(-) diff --git a/infrastructure/src/main/java/org/springframework/batch/repeat/aop/RepeatOperationsInterceptor.java b/infrastructure/src/main/java/org/springframework/batch/repeat/aop/RepeatOperationsInterceptor.java index 3bd510f18..f4360aabc 100644 --- a/infrastructure/src/main/java/org/springframework/batch/repeat/aop/RepeatOperationsInterceptor.java +++ b/infrastructure/src/main/java/org/springframework/batch/repeat/aop/RepeatOperationsInterceptor.java @@ -62,6 +62,8 @@ public class RepeatOperationsInterceptor implements MethodInterceptor { if (invocation instanceof ReflectiveMethodInvocation) { clone = ((ReflectiveMethodInvocation) invocation) .invocableClone(); + } else { + throw new IllegalStateException("MethodInvocation of the wrong type detected - this should not happen with Spring AOP, so please raise an issue if you see this exception"); } // N.B. discards return value if there is one diff --git a/infrastructure/src/main/java/org/springframework/batch/retry/aop/RetryOperationsInterceptor.java b/infrastructure/src/main/java/org/springframework/batch/retry/aop/RetryOperationsInterceptor.java index 5adc8479b..7d416586e 100644 --- a/infrastructure/src/main/java/org/springframework/batch/retry/aop/RetryOperationsInterceptor.java +++ b/infrastructure/src/main/java/org/springframework/batch/retry/aop/RetryOperationsInterceptor.java @@ -56,6 +56,8 @@ public class RetryOperationsInterceptor implements MethodInterceptor { if (invocation instanceof ReflectiveMethodInvocation) { clone = ((ReflectiveMethodInvocation) invocation) .invocableClone(); + } else { + throw new IllegalStateException("MethodInvocation of the wrong type detected - this should not happen with Spring AOP, so please raise an issue if you see this exception"); } return clone.proceed(); diff --git a/infrastructure/src/test/java/org/springframework/batch/repeat/aop/RepeatOperationsInterceptorTests.java b/infrastructure/src/test/java/org/springframework/batch/repeat/aop/RepeatOperationsInterceptorTests.java index baf04f5d7..b7bce1438 100644 --- a/infrastructure/src/test/java/org/springframework/batch/repeat/aop/RepeatOperationsInterceptorTests.java +++ b/infrastructure/src/test/java/org/springframework/batch/repeat/aop/RepeatOperationsInterceptorTests.java @@ -16,6 +16,8 @@ package org.springframework.batch.repeat.aop; +import java.lang.reflect.AccessibleObject; +import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; @@ -44,7 +46,8 @@ public class RepeatOperationsInterceptorTests extends TestCase { super.setUp(); interceptor = new RepeatOperationsInterceptor(); target = new ServiceImpl(); - ProxyFactory factory = new ProxyFactory(RepeatOperations.class.getClassLoader()); + ProxyFactory factory = new ProxyFactory(RepeatOperations.class + .getClassLoader()); factory.setInterfaces(new Class[] { Service.class }); factory.setTarget(target); service = (Service) factory.getProxy(); @@ -75,8 +78,7 @@ public class RepeatOperationsInterceptorTests extends TestCase { try { service.exception(); fail("Expected RuntimeException"); - } - catch (RuntimeException e) { + } catch (RuntimeException e) { assertEquals("Duh", e.getMessage().substring(0, 3)); } } @@ -86,8 +88,7 @@ public class RepeatOperationsInterceptorTests extends TestCase { try { service.error(); fail("Expected BatchException"); - } - catch (RepeatException e) { + } catch (RepeatException e) { assertEquals("Unexpected", e.getMessage().substring(0, 10)); } } @@ -108,6 +109,37 @@ public class RepeatOperationsInterceptorTests extends TestCase { assertEquals(3, list.size()); } + public void testIllegalMethodInvocationType() throws Throwable { + try { + interceptor.invoke(new MethodInvocation() { + public Method getMethod() { + return null; + } + + public Object[] getArguments() { + return null; + } + + public AccessibleObject getStaticPart() { + return null; + } + + public Object getThis() { + return null; + } + + public Object proceed() throws Throwable { + return null; + } + }); + fail("IllegalStateException expected"); + } catch (IllegalStateException e) { + assertTrue("Exception message should contain MethodInvocation: " + + e.getMessage(), e.getMessage() + .indexOf("MethodInvocation") >= 0); + } + } + private interface Service { Object service() throws Exception; @@ -123,8 +155,7 @@ public class RepeatOperationsInterceptorTests extends TestCase { count++; if (count <= 2) { return new Integer(count); - } - else { + } else { return null; } } diff --git a/infrastructure/src/test/java/org/springframework/batch/retry/aop/RetryOperationsInterceptorTests.java b/infrastructure/src/test/java/org/springframework/batch/retry/aop/RetryOperationsInterceptorTests.java index 7cc4dcd82..2fef895d5 100644 --- a/infrastructure/src/test/java/org/springframework/batch/retry/aop/RetryOperationsInterceptorTests.java +++ b/infrastructure/src/test/java/org/springframework/batch/retry/aop/RetryOperationsInterceptorTests.java @@ -16,6 +16,8 @@ package org.springframework.batch.retry.aop; +import java.lang.reflect.AccessibleObject; +import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; @@ -82,6 +84,37 @@ public class RetryOperationsInterceptorTests extends TestCase { } assertEquals(1, target.count); } + + public void testIllegalMethodInvocationType() throws Throwable { + try { + interceptor.invoke(new MethodInvocation() { + public Method getMethod() { + return null; + } + + public Object[] getArguments() { + return null; + } + + public AccessibleObject getStaticPart() { + return null; + } + + public Object getThis() { + return null; + } + + public Object proceed() throws Throwable { + return null; + } + }); + fail("IllegalStateException expected"); + } catch (IllegalStateException e) { + assertTrue("Exception message should contain MethodInvocation: " + + e.getMessage(), e.getMessage() + .indexOf("MethodInvocation") >= 0); + } + } private interface Service { void service() throws Exception;