[cdi-dev] Getting injection point from Bean#create
arjan tijms
arjan.tijms at gmail.com
Thu Nov 20 17:01:20 EST 2014
Hi,
On Thu, Nov 20, 2014 at 9:42 PM, Jozef Hartinger <jharting at redhat.com> wrote:
> The simplest thing you can do is:
>
> Bean<InjectionPoint> bean = (Bean<InjectionPoint>)
> manager.resolve(manager.getBeans(InjectionPoint.class));
> InjectionPoint ip = (InjectionPoint) manager.getReference(bean,
> InjectionPoint.class, manager.createCreationalContext(bean));
That's exactly what I initially started with ;) It works perfectly on
Weld, but unfortunately doesn't work on OWB (I tried 1.5.0).
There's the following exception thrown with that code:
java.lang.NullPointerException
at org.apache.webbeans.portable.InjectionPointProducer.produce(InjectionPointProducer.java:59)
at org.apache.webbeans.portable.InjectionPointProducer.produce(InjectionPointProducer.java:43)
at org.apache.webbeans.portable.AbstractProducer.produce(AbstractProducer.java:195)
at org.apache.webbeans.component.AbstractOwbBean.create(AbstractOwbBean.java:126)
at org.apache.webbeans.context.DependentContext.getInstance(DependentContext.java:68)
at org.apache.webbeans.context.AbstractContext.get(AbstractContext.java:124)
at org.apache.webbeans.container.BeanManagerImpl.getReference(BeanManagerImpl.java:756)
at org.apache.webbeans.container.InjectableBeanManager.getReference(InjectableBeanManager.java:165)
It tries to execute the following code, where "first" turns up to be null:
// the first injection point on the stack is of type InjectionPoint,
so we need the second one
CreationalContextImpl<InjectionPoint> creationalContextImpl =
(CreationalContextImpl<InjectionPoint>)creationalContext;
InjectionPoint first = creationalContextImpl.removeInjectionPoint();
if (!InjectionPoint.class.isAssignableFrom(ClassUtil.getClass(first.getType())))
I tried some variants on the above, like using the existing
creationalContext in the manager.getReference call instead of creating
a new one via manager.createCreationalContext(bean), but that too
didn't work (throws java.lang.IllegalStateException: Inconsistent
injection point stack).
Kind regards,
Arjan Tijms
>
>
> On 11/19/2014 05:06 PM, arjan tijms wrote:
>>
>> Hi,
>>
>> In a producer method it's trivial to get access to an InjectionPoint
>> instance representing the point where the value produced by the
>> producer will be injected.
>>
>> When registering a Bean manually from an extension using
>> AfterBeanDiscovery#addBean, this is not immediately obvious.
>>
>> After some fumbling with the CDI APIs I came up with the following
>> code that seems to work on both Weld and OWB (didn't test CanDI yet).
>>
>> It uses a small "dummy" class, which is used to grab an InjectionPoint
>> off:
>>
>> In a Bean:
>>
>> public Object create(CreationalContext<Object> creationalContext) {
>>
>> InjectionPoint injectionPoint = (InjectionPoint)
>> beanManager.getInjectableReference(
>> resolve(beanManager,
>> InjectionPointGenerator.class).getInjectionPoints().iterator().next(),
>> creationalContext
>> );
>>
>> With InjectionPointGenerator being the following class:
>>
>> public class InjectionPointGenerator {
>> @Inject
>> private InjectionPoint injectionPoint;
>> }
>>
>> And resolve being the following method:
>>
>> public static <T> Bean<T> resolve(BeanManager beanManager, Class<T>
>> beanClass) {
>> Set<Bean<?>> beans = beanManager.getBeans(beanClass);
>>
>> for (Bean<?> bean : beans) {
>> if (bean.getBeanClass() == beanClass) {
>> return (Bean<T>)
>> beanManager.resolve(Collections.<Bean<?>>singleton(bean));
>> }
>> }
>>
>> return (Bean<T>) beanManager.resolve(beans);
>> }
>>
>> As mentioned, while this seems to work, I wonder if it's the best
>> approach.
>>
>> Kind regards,
>> Arjan
>> _______________________________________________
>> cdi-dev mailing list
>> cdi-dev at lists.jboss.org
>> https://lists.jboss.org/mailman/listinfo/cdi-dev
>>
>> Note that for all code provided on this list, the provider licenses the
>> code under the Apache License, Version 2
>> (http://www.apache.org/licenses/LICENSE-2.0.html). For all other ideas
>> provided on this list, the provider waives all patent and other intellectual
>> property rights inherent in such information.
>
>
More information about the cdi-dev
mailing list