Method Interceptor with Annotation Arguments

1,492 views
Skip to first unread message

Sasha Bermeister

unread,
Sep 19, 2013, 9:06:36 PM9/19/13
to google...@googlegroups.com
Hi all,

I've been following the tutorial on AOP at https://code.google.com/p/google-guice/wiki/AOP. In the tutorial, it shows how to use an annotation with no arguments:

public class RealBillingService implements BillingService {

 
@NotOnWeekends
 
public Receipt chargeOrder(PizzaOrder order, CreditCard creditCard) {
   
...
 
}
}

For my application, I need this annotation to take arguments, e.g.:

@NotOnWeekends("Foo")

And these arguments need to be accessible from within the MethodInterceptor itself (since I need to perform something different based on what the arguments might be).

I've looked around for guides and tutorials, but nothing seems to cover this particular case. Any ideas?

Thanks,

Sasha

Laszlo Ferenczi

unread,
Sep 20, 2013, 3:36:09 AM9/20/13
to google...@googlegroups.com
Hi Sasha,

You can access all annotations on the method and/or the defining class using the MethodInvocation parameter of MethodInterceptor.invoke(). Something like this:

public class MyInterceptor implements MethodInterceptor {

    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        Method method = methodInvocation.getMethod();
        MyAnnotation annotation = method.getAnnotation(Transactional.class);
        ....
    }
}

For a full example check out guice-persist's transactional interceptor:

Line 56 is where the annotation is extracted. 

HTH


--
L


--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice...@googlegroups.com.
To post to this group, send email to google...@googlegroups.com.
Visit this group at http://groups.google.com/group/google-guice.
For more options, visit https://groups.google.com/groups/opt_out.

Reply all
Reply to author
Forward
0 new messages