Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Add more flexibility to the method interceptor

  • Loading branch information...
commit 0b3de784989f990df6031453457b466ccac48392 1 parent fc537d2
David M. Lloyd dmlloyd authored
42 src/main/java/org/jboss/invocation/MethodInterceptor.java
@@ -31,7 +31,7 @@
31 31 import static org.jboss.invocation.InvocationMessages.msg;
32 32
33 33 /**
34   - * A method interceptor. The target method should be non-final and must accept a single
  34 + * A method interceptor. The target method should be non-final and must accept no parameters or a single
35 35 * parameter of type {@link InvocationContext} (or any supertype thereof). The method must belong to the given
36 36 * interceptor object's class or one of its supertypes.
37 37 *
@@ -41,14 +41,18 @@
41 41
42 42 private final Object interceptorInstance;
43 43 private final Method method;
  44 + private final boolean withContext;
  45 + private final boolean changeMethod;
44 46
45 47 /**
46 48 * Construct a new instance. The given method should be a proper interceptor method; otherwise invocation may fail.
47 49 *
48 50 * @param interceptorInstance the interceptor object instance
49 51 * @param method the interceptor method
  52 + * @param changeMethod {@code true} to change the method on the context to equal the given method, {@code false} to leave it as-is
50 53 */
51   - public MethodInterceptor(final Object interceptorInstance, final Method method) {
  54 + public MethodInterceptor(final Object interceptorInstance, final Method method, final boolean changeMethod) {
  55 + this.changeMethod = changeMethod;
52 56 if (interceptorInstance == null) {
53 57 throw msg.nullParameter("interceptorInstance");
54 58 }
@@ -58,12 +62,39 @@ public MethodInterceptor(final Object interceptorInstance, final Method method)
58 62 this.method = method;
59 63 this.interceptorInstance = interceptorInstance;
60 64 checkMethodType(interceptorInstance);
  65 + withContext = method.getParameterTypes().length == 1;
  66 + }
  67 +
  68 + /**
  69 + * Construct a new instance. The given method should be a proper interceptor method; otherwise invocation may fail.
  70 + *
  71 + * @param interceptorInstance the interceptor object instance
  72 + * @param method the interceptor method
  73 + */
  74 + public MethodInterceptor(final Object interceptorInstance, final Method method) {
  75 + this(interceptorInstance, method, false);
61 76 }
62 77
63 78 /** {@inheritDoc} */
64 79 public Object processInvocation(final InterceptorContext context) throws Exception {
65 80 try {
66   - return method.invoke(interceptorInstance, context.getInvocationContext());
  81 + Method method = this.method;
  82 + if (withContext) {
  83 + if (changeMethod) {
  84 + final Method oldMethod = context.getMethod();
  85 + context.setMethod(method);
  86 + try {
  87 + return method.invoke(interceptorInstance, context.getInvocationContext());
  88 + } finally {
  89 + context.setMethod(oldMethod);
  90 + }
  91 + } else {
  92 + return method.invoke(interceptorInstance, context.getInvocationContext());
  93 + }
  94 + } else {
  95 + method.invoke(interceptorInstance, null);
  96 + return context.proceed();
  97 + }
67 98 } catch (IllegalAccessException e) {
68 99 final IllegalAccessError n = new IllegalAccessError(e.getMessage());
69 100 n.setStackTrace(e.getStackTrace());
@@ -83,11 +114,12 @@ private void checkMethodType(final Object interceptorInstance) {
83 114 throw msg.interceptorInaccessible();
84 115 }
85 116 final Class<?>[] parameterTypes = method.getParameterTypes();
86   - if (parameterTypes.length != 1) {
  117 + final int length = parameterTypes.length;
  118 + if (length > 1) {
87 119 throw msg.interceptorTargetOneParam();
88 120 }
89 121 // allow contravariant parameter types
90   - if (! parameterTypes[0].isAssignableFrom(InvocationContext.class)) {
  122 + if (length == 1 && ! parameterTypes[0].isAssignableFrom(InvocationContext.class)) {
91 123 throw msg.interceptorTargetAssignableFrom(InvocationContext.class);
92 124 }
93 125 // allow covariant return types (but not primitives, which are not Objects); also allow void for lifecycle interceptors
17 src/main/java/org/jboss/invocation/MethodInterceptorFactory.java
@@ -34,16 +34,29 @@
34 34 public final class MethodInterceptorFactory implements InterceptorFactory {
35 35 private final InterceptorInstanceFactory instanceFactory;
36 36 private final Method interceptorMethod;
  37 + private final boolean changeMethod;
37 38
38 39 /**
39 40 * Construct a new instance.
40 41 *
41 42 * @param instanceFactory the instance factory for the interceptor instance
42 43 * @param interceptorMethod the interceptor method
  44 + * @param changeMethod {@code true} to change the method on the context to equal the given method, {@code false} to leave it as-is
43 45 */
44   - public MethodInterceptorFactory(final InterceptorInstanceFactory instanceFactory, final Method interceptorMethod) {
  46 + public MethodInterceptorFactory(final InterceptorInstanceFactory instanceFactory, final Method interceptorMethod, final boolean changeMethod) {
45 47 this.instanceFactory = instanceFactory;
46 48 this.interceptorMethod = interceptorMethod;
  49 + this.changeMethod = changeMethod;
  50 + }
  51 +
  52 + /**
  53 + * Construct a new instance.
  54 + *
  55 + * @param instanceFactory the instance factory for the interceptor instance
  56 + * @param interceptorMethod the interceptor method
  57 + */
  58 + public MethodInterceptorFactory(final InterceptorInstanceFactory instanceFactory, final Method interceptorMethod) {
  59 + this(instanceFactory, interceptorMethod, false);
47 60 }
48 61
49 62 /** {@inheritDoc} */
@@ -52,7 +65,7 @@ public Interceptor create(final InterceptorFactoryContext context) {
52 65 if (map.containsKey(this)) {
53 66 return (Interceptor) map.get(this);
54 67 } else {
55   - final MethodInterceptor interceptor = new MethodInterceptor(instanceFactory.createInstance(context), interceptorMethod);
  68 + final MethodInterceptor interceptor = new MethodInterceptor(instanceFactory.createInstance(context), interceptorMethod, changeMethod);
56 69 map.put(this, interceptor);
57 70 return interceptor;
58 71 }

0 comments on commit 0b3de78

Please sign in to comment.
Something went wrong with that request. Please try again.