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)

      https://github.com/mgraciano/javaone-2012/

      https://github.com/mgraciano/javaone-2012/blob/master/extension-demo/extension-demo-extensions/src/main/java/mgt/BeanDetailsExtension.java

       

      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 :

      http://docs.jboss.org/weld/reference/latest/en-US/html_single/#d0e5048

       

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

       

      @InterceptorBinding
      @Inherited
      @Documented
      @Target( { ElementType.TYPE, ElementType.METHOD })
      @Retention(RetentionPolicy.RUNTIME)
      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());
                      pat.setAnnotatedType(builder.create());
                  }
              }
          
          }