1 Reply Latest reply on Mar 18, 2013 6:53 PM by Mathieu Lachance

    add interceptor binding programmatically

    Mathieu Lachance Novice

      Hello everyone,


      I was wondering if it was possible to add intercepting binding programmatically. My goal is to add a custom interceptor to all @ApplicationScoped and @Singleton programmatically instead of specifying it everywhere.


      My idea come from this 2012 java one conference :

      http://www.oracle.com/openworld/lad-en/session-schedule/con21001-graciano-ok-1887773.pdf (see slide 30 portable extension, and slide 39)




      Though I haven't found a way to reproduce the AnnotatedTypeBuilder using the Weld (note that I'm using Weld 2.0.0.Beta5)


      So instead I have followed the documentation to add programmatically the @Named annotation while processing annotation type :



      at the only difference of adding instead of the @Named, the following interceptor binding :


      @Target( { ElementType.TYPE, ElementType.METHOD })
      public @interface MyInterceptorBinding


      though later, when injecting my service that has been wrapped, my interceptor defined by my interceptor binding doesn't get called.


      If someone have any idea that could help it would be greatly appreciated.


      Big thanks,

        • 1. Re: add interceptor binding programmatically
          Mathieu Lachance Novice

          I have finally noticed that Seam has been halted : http://seamframework.org

          Since I figured also that Solder was providing the AnnotatedTypeBuilder, I just grabbed the apache deltaspike one (where solder moved) and now I can add my interceptor binding dynamically :


          public class MyInterceptorExtension implements Extension {
              <T> void processAnnotatedType(@Observes final ProcessAnnotatedType<T> pat) {
                  class MyInterceptorLiteral extends AnnotationLiteral<MyInterceptor> {
                      private static final long serialVersionUID = 1L;
                  if (pat.getAnnotatedType().getJavaClass().isAnnotationPresent(ApplicationScoped.class)){
                      final AnnotatedTypeBuilder<T> builder = new AnnotatedTypeBuilder<T>()
                              .readFromType(pat.getAnnotatedType(), true)
                              .addToClass(new ValidateMethodLiteral());