2 Replies Latest reply on May 14, 2012 10:38 AM by potamus

    Create producers at runtime

    potamus

      I have many annotated interfaces like this.

       

      @A
      public interface Interface1 {
      }
      

       

      At runtime there will be somehow created implementations of these interfaces. In the code I want to inject this implementation like this:

       

      @Inject
      // Optional qualifiers.
      private Interface1 myInterface1;
      
      @Inject
      private void test(Interface1 arg0) {
          ...
      }
      

       

      Now I'm using an extension in which I have one producer:

       

      @Produces
      @MyInternalQualifier
      Object getImplemenation(InjectionPoint injectionPoint) {
          ...
      }
      

      And ProcessAnnotatedType observer in which I replace the type of the field/parameter to Object and add a special internal annotation @MyInternalQualifier.

       

      The disadvantage of this approach is that there is no real producer for interface types, so you cannot create an implementation programmatically with BeanManager. Is there any way to create real producers at runtime?

        • 1. Re: Create producers at runtime
          jharting

          You cannot register producers at runtime. You can create "real" producers using an extension at the deployment time but you have to know the return type upfront.

          • 2. Re: Create producers at runtime
            potamus

            I also tried another approach. It look horrible, but works perfect.

            In Extension I observe ProcessAnnotatedType event where for each interface I create a class using ASM with a static @Produces method. Instead of creating AnnotatedType for interface I set annotated type of event object to the annotated type of newly created class. What issues can this approach introduce?