[weld-dev] Proper way to get to EnhancedAnnotatedConstructor (CDI autofactories implementation)

Martin Kouba mkouba at redhat.com
Tue Aug 9 08:05:12 EDT 2016


Hi Jan-Willem,

first of all it's not a good idea to depend on Weld internals - the 
implementation can change any time without any warning. On the other 
hand, I understand that you're just "patching" an existing extension.

WRT the way to get to the EnhancedAnnotatedConstructor - there is 
org.jboss.weld.injection.InjectionPointFactory.createConstructorInjectionPoint(Bean<T>, 
EnhancedAnnotatedType<T>, BeanManagerImpl) which itself performs the 
lookup (and attempts to find a bean constructor as defined in 3.9. Bean 
constructors).

Moreover, you should probably use a "silent" instance of 
InjectionPointFactory, otherwise ProcessInjectionPoint event will be 
fired for each injection point created.

Also I would recommend to analyze the extension code so that it's clear 
what exactly is missing in the CDI 1.2 extension SPI (a lot of stuff was 
added since CDI 1.0/Weld 1). This will help improve the CDI 2.0 SPI 
(under development).

Martin

Dne 8.8.2016 v 17:36 Jan-Willem Gmelig Meyling napsal(a):
> Hello everyone,
>
>
> I am in the process of patching the softwaremill-common CDI extensions
> [1] from Weld 1.1 to Weld 2.x. I am currently working on their extension
> for autofactories. I stumbled upon the following piece of code I would
> like to migrate:
>
>             CurrentInjectionPoint currentInjectionPoint =
> Container.instance().services().get(CurrentInjectionPoint.class);
>
> currentInjectionPoint.push(ConstructorInjectionPoint.of(bean,
> (WeldConstructor<T>) createdTypeData.getCreatedTypeConstructor()));
>             instance = newInstance(parameters);
>             currentInjectionPoint.pop();
>
> Source: [2]
>
> I see how the pop should now be invoked on the
> `ThreadLocalStackReference` returned by the push method. I have also
> found the InjectionPointFactory#createConstructorInjectionPoint(Bean,
> Class, EnhancedAnnotatedConstructor, BeanManagerImpl) method [3]. Now I
> am wondering how I can get to the `EnhancedAnnotatedConstructor`, as the
> approach I am currently using feels plain wrong.
>
> My code:
>
>             CurrentInjectionPoint currentInjectionPoint =
> Container.instance().services().get(CurrentInjectionPoint.class);
>             Class<?> declaringComponentClass = (Class<T>)
> createdTypeData.getCreatedTypeConstructor().getBaseType();
>
>
>             BeanManagerImpl manager = ((BeanManagerProxy)
> beanManager).delegate();
>             EnhancedAnnotatedConstructor<T> constructor =
> (EnhancedAnnotatedConstructor<T>) manager
>                 .createEnhancedAnnotatedType(declaringComponentClass)
>                 .getEnhancedConstructors()
>                 .stream().findAny().get();
>
>             ConstructorInjectionPoint<T> actualInjectionPoint =
> InjectionPointFactory.instance()
>                 .createConstructorInjectionPoint(bean,
> declaringComponentClass, constructor, manager);
>             ThreadLocalStackReference<InjectionPoint> ref =
> currentInjectionPoint.push(actualInjectionPoint);
>             instance = newInstance(parameters);
>
>
> My code is also available on Github at [4]. My question is also posted
> on Stackoverflow [5], so points will be awarded for the answer.
>
> Thanks in advance!
>
> Jan-Willem Gmelig Meyling
>
>
>
>
> [1]
> https://github.com/softwaremill/softwaremill-common/tree/master/softwaremill-cdi
> [2] https://github.com/softwaremill/softwaremill-common/blob/master/softwaremill-cdi/src/main/java/com/softwaremill/common/cdi/autofactory/extension/FactoryInvocationHandler.java#L35-L48
> [3]
> http://javadox.com/org.jboss.weld.servlet/weld-servlet/2.3.1.Final/org/jboss/weld/injection/InjectionPointFactory.html#createConstructorInjectionPoint-javax.enterprise.inject.spi.Bean-java.lang.Class-org.jboss.weld.annotated.enhanced.EnhancedAnnotatedConstructor-org.jboss.weld.manager.BeanManagerImpl-
> [4] https://github.com/JWGmeligMeyling/cdi-autofactories/blob/8346cf269d73a8bd455c12c4d467df7bcb8f3920/src/main/java/com/softwaremill/common/cdi/autofactory/extension/FactoryInvocationHandler.java#L50-L60
> [5] http://stackoverflow.com/questions/38436110/proper-way-to-get-enhancedannotatedconstructor
>
>
>
>
> _______________________________________________
> weld-dev mailing list
> weld-dev at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/weld-dev
>

-- 
Martin Kouba
Software Engineer
Red Hat, Czech Republic


More information about the weld-dev mailing list