Page 1 of 2 12 LastLast
Results 1 to 10 of 17

Thread: Intercepting all methods of an interface

  1. #1
    Join Date
    Mar 2007
    Posts
    566

    Default Intercepting all methods of an interface

    Hi,

    I want to intercept all methods of an interface. OK, no problem - implement a MethodInterceptor - done.

    But my interceptor should implement this interface too. How can I dispatch the methods of my adviced class to the methods of my interceptor?

    Code:
    interface Foo {
     Object method1(Object o, String s);
     void method2(Date d, int i);
    }
    
    class FooImpl implements Foo {
     Object method1(Object o, String s) {...}
     void method2(Date d, int i) {...}
    }
    
    class FooInterceptor implements MethodInterceptor, Foo {
     Object invoke(MethodInvocation methodinvocation) throws Throwable {
     // how can I dispatch here automatically to method1 and method2?
     }
    
     Object method1(Object o, String s) {...}
     void method2(Date d, int i) {...}
    
    }
    Thank you

  2. #2
    Join Date
    Nov 2007
    Posts
    420

    Default

    Quote Originally Posted by spgmx View Post
    Hi,

    I want to intercept all methods of an interface. OK, no problem - implement a MethodInterceptor - done.

    But my interceptor should implement this interface too. How can I dispatch the methods of my adviced class to the methods of my interceptor?

    Code:
    interface Foo {
     Object method1(Object o, String s);
     void method2(Date d, int i);
    }
    
    class FooImpl implements Foo {
     Object method1(Object o, String s) {...}
     void method2(Date d, int i) {...}
    }
    
    class FooInterceptor implements MethodInterceptor, Foo {
     Object invoke(MethodInvocation methodinvocation) throws Throwable {
     // how can I dispatch here automatically to method1 and method2?
     }
    
     Object method1(Object o, String s) {...}
     void method2(Date d, int i) {...}
    
    }
    Thank you
    You are introducing recursion here that will never end... You are intercepting on Foo via interceptor that is also Foo. so you'll just keep recursing without an end in sight...

  3. #3
    Join Date
    Mar 2007
    Posts
    566

    Default

    My pointcut will be on FooImpl not on Foo.

  4. #4
    Join Date
    Nov 2007
    Posts
    420

    Default

    Quote Originally Posted by spgmx View Post
    My pointcut will be on FooImpl not on Foo.
    why is the interceptor implementing Foo interface?

  5. #5
    Join Date
    Mar 2007
    Posts
    566

    Default

    Because it shall intercept every Foo method.

  6. #6
    Join Date
    Nov 2007
    Posts
    420

    Default

    Quote Originally Posted by spgmx View Post
    Because it shall intercept every Foo method.
    just take it out, you just need to implement method interceptor. you'll setup what you are intercepting (Foo) through Spring.

  7. #7
    Join Date
    Mar 2007
    Posts
    566

    Default

    OK, I can do this, but the main question is still not answered:

    How can I dispatch the methods of my adviced class to the methods of my interceptor?

    if then else?

  8. #8
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    What is it that your interceptor should do?! Why create an implementation if the real implementation is your interceptor. I don't understand what you are trying to do.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  9. #9
    Join Date
    Mar 2007
    Posts
    566

    Default

    I want to inspect/manipulate the method arguments and then delegate to the impl class or reject the call.

    But here is still a bug in my concept. This would lead to an endless loop...

    Hm...

  10. #10
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,695

    Default

    So then implement it that way... You are making it way to complex....

    Your MethodInterceptor can do just that... Have you actually looked at the documentation of the MethodInterceptor and MethodInvocation class?!

    Code:
    import org.aopalliance.intercept.MethodInterceptor;
    import org.aopalliance.intercept.MethodInvocation;
    import org.springframework.aop.framework.ReflectiveMethodInvocation;
    
    
    public class FooInterceptor implements MethodInterceptor {
    
    	@Override
    	public Object invoke(MethodInvocation invocation) throws Throwable {
    		ReflectiveMethodInvocation method = (ReflectiveMethodInvocation) invocation;
    		Object[] arguments = method.getArguments();
    		//modify arguments
    		method.setArguments(arguments);
    		if (ok) {
    			method.proceed();
    		} else {
    			return null; // or throw an exception
    		}
    	}
    
    }
    But why do it this way, using an AspectJ aspect would make it much easier.

    Code:
    @Aspect
    public class FooInterceptor {
    
    	@Pointcut("execution(* *..Foo+.*(..))")
    	void fooMethods() {}
    	
    	@Around("fooMethods()")
    	public Object intercept(ProceedingJoinPoint pjp) {
    		Object[] arguments = pjp.getArgs();
    		//change arguments
    		if (ok) {
    			pjp.proceed(arguments);
    		} else {
    			return null; // or throw an exception
    		}
    	}
    }
    Put it in your application context, include <aop:aspectj-autoproxy /> and presto...
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •