Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add an initial interceptor capable of handling undeclared exceptions

  • Loading branch information...
commit f90379281298227709146dbc07897ea40a5c4c34 1 parent 4dce7ff
John E. Bailey authored
View
40 src/main/java/org/jboss/invocation/InitialInterceptor.java
@@ -0,0 +1,40 @@
+package org.jboss.invocation;
+
+import java.io.Serializable;
+import java.lang.reflect.Method;
+import java.lang.reflect.UndeclaredThrowableException;
+
+/**
+ * The initial interceptor that is called during an invocation.
+ *
+ * @author John Bailey
+ */
+public class InitialInterceptor implements Interceptor, Serializable {
+
+ static final InitialInterceptor INSTANCE = new InitialInterceptor();
+ static final InterceptorFactory FACTORY = new ImmediateInterceptorFactory(INSTANCE);
+
+
+ public Object processInvocation(final InterceptorContext context) throws Exception {
+ final Method method = context.getMethod();
+ try {
+ return context.proceed();
+ } catch (RuntimeException e) {
+ throw e;
+ } catch (Error e) {
+ throw e;
+ } catch (Exception e) {
+ if (method != null) {
+ for (Class<?> expected : method.getExceptionTypes()) {
+ if (expected.isAssignableFrom(e.getClass())) {
+ throw e;
+ }
+ }
+ }
+ throw new UndeclaredThrowableException(e);
+ } catch (Throwable t) {
+ throw new UndeclaredThrowableException(t); //We don't seem to get here
+ }
+ }
+}
+
View
19 src/main/java/org/jboss/invocation/Interceptors.java
@@ -37,6 +37,25 @@ private Interceptors() {
}
/**
+ * Get an interceptor that is always invoked first. This interceptor is responsible for correctly handling any initialization
+ * and cleanup for the interceptor chain. For example, this interceptor is responsible for handling undeclared checked exceptions.
+ *
+ * @return the interceptor
+ */
+ public static Interceptor getInitialInterceptor() {
+ return InitialInterceptor.INSTANCE;
+ }
+
+ /**
+ * Get the interceptor factory for the initial interceptor.
+ *
+ * @return the factory
+ */
+ public static InterceptorFactory getInitialInterceptorFactory() {
+ return InitialInterceptor.FACTORY;
+ }
+
+ /**
* Get an interceptor which always returns {@code null}.
*
* @return the interceptor
View
80 src/test/java/org/jboss/invocation/test/InitialInterceptorTestCase.java
@@ -0,0 +1,80 @@
+package org.jboss.invocation.test;
+
+import java.lang.reflect.UndeclaredThrowableException;
+import org.jboss.invocation.Interceptor;
+import org.jboss.invocation.InterceptorContext;
+import org.jboss.invocation.Interceptors;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.fail;
+import org.junit.Test;
+
+/**
+ * @author John Bailey
+ */
+public class InitialInterceptorTestCase {
+
+ private class ExceptionalInterceptor implements Interceptor {
+ public Object processInvocation(InterceptorContext context) throws Exception {
+ throw new Exception("Ahhh");
+ }
+ }
+
+ private class EvenMoreExceptionalInterceptor implements Interceptor {
+ public Object processInvocation(InterceptorContext context) throws Exception {
+ throw new ChildException();
+ }
+ }
+
+ @Test
+ public void testSuccess() throws Exception {
+ final Interceptor interceptor = Interceptors.getChainedInterceptor(Interceptors.getInitialInterceptor(), Interceptors.getInvokingInterceptor());
+ final InterceptorContext context = new InterceptorContext();
+ context.setMethod(InitialInterceptorTestCase.class.getMethod("test"));
+ context.setTarget(this);
+ assertEquals("test", interceptor.processInvocation(context));
+ }
+
+ @Test
+ public void testException() throws Exception {
+ final Interceptor interceptor = Interceptors.getChainedInterceptor(Interceptors.getInitialInterceptor(), new ExceptionalInterceptor(), Interceptors.getInvokingInterceptor());
+ final InterceptorContext context = new InterceptorContext();
+ context.setMethod(InitialInterceptorTestCase.class.getMethod("test"));
+ context.setTarget(this);
+ try {
+ interceptor.processInvocation(context);
+ fail("Should have thrown UndeclaredThrowableException");
+ } catch (UndeclaredThrowableException expected) {
+ }
+ }
+
+ @Test
+ public void testChildException() throws Exception {
+ final Interceptor interceptor = Interceptors.getChainedInterceptor(Interceptors.getInitialInterceptor(), new EvenMoreExceptionalInterceptor(), Interceptors.getInvokingInterceptor());
+ final InterceptorContext context = new InterceptorContext();
+ context.setMethod(InitialInterceptorTestCase.class.getMethod("other"));
+ try {
+ interceptor.processInvocation(context);
+ fail("Should have thrown ChildException");
+ } catch (ChildException expected) {
+ }
+ }
+
+ public String test() {
+ return "test";
+ }
+
+ public String other() throws ParentException {
+ return "test";
+ }
+
+
+ public String throwsThrowable() throws Throwable {
+ throw new Throwable();
+ }
+
+ private class ParentException extends Exception {
+ }
+
+ private class ChildException extends ParentException {
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.