1 Reply Latest reply on Jan 28, 2019 3:51 AM by Martin Kouba

    Is adding an InjectionPoint to synthetic bean sufficient to cause injection to happen?

    Laird Nelson Expert

      I am adding a synthetic bean.

       

      The Java object that I am "beanifying" does not have any @Inject-annotated methods on it.

       

      I would like to turn one method into an injection point.  The method has one parameter.

       

      I'm doing this:

      afterBeanDiscoveryEvent.addBean()
        .addTransitiveTypeClosure(Pojo.class)   .scope(ApplicationScoped.class)   .qualifiers(qualifiers)   .addInjectionPoint(handcraftedInjectionPoint) // see below   .createWith(cc -> new Pojo());

      handcraftedInjectionPoint is an InjectionPoint that am creating like this:

       

      1. I wrote a class called DelegatingInjectionPoint that implements InjectionPoint and forwards all operations to an InjectionPoint it receives at construction time.  (Incidentally, I've had to write classes like this hundreds of times over the years!)
      2. I create a subclass of DelegatingInjectionPoint passing it a container-created InjectionPoint for the sole parameter of the method that I'm talking about.  I got this container-created InjectionPoint by calling the beanManager.createInjectionPoint(AnnotatedParameter) method.  Obviously the parameter that I am passing to that method is the sole parameter of the method I am talking about above.
      3. I override getQualifiers() in my DelegatingInjectionPoint subclass to return the container-created InjectionPoint's qualifiers plus one other that I know exists.

       

      I am observing that Pojo instances are being created, i.e. the bean is registered and valid, so that's good, but no attempt is made by the container to call this injection point.

       

      Why not?

       

      Weld 3.0.5.Final if it matters.

       

      Best,

      Laird

        • 1. Re: Is adding an InjectionPoint to synthetic bean sufficient to cause injection to happen?
          Martin Kouba Master

          Hi Laird,

          Unfortunately, synthetic beans do not work this way - javax.enterprise.inject.spi.Bean.getInjectionPoints() is only used for validation. Instead, the extension is responsible for creating a new fully initialized bean instance. In theory, you could try to use BeanManager.createAnnotatedType(Class<T>) and BeanManager.getInjectionTargetFactory(AnnotatedType<T>) and javax.enterprise.inject.spi.InjectionTargetFactory.configure() to turn a Pojo method into an injection point. And afterwards use the injection target to produce a new Pojo instance.