Getting injection point from Bean#create

classic Classic list List threaded Threaded
22 messages Options
12
Reply | Threaded
Open this post in threaded view
|

Getting injection point from Bean#create

arjan tijms
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
[hidden email]
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.
Reply | Threaded
Open this post in threaded view
|

Re: Getting injection point from Bean#create

Jozef Hartinger
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));

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
> [hidden email]
> 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.

_______________________________________________
cdi-dev mailing list
[hidden email]
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.
Reply | Threaded
Open this post in threaded view
|

Re: Getting injection point from Bean#create

arjan tijms
Hi,

On Thu, Nov 20, 2014 at 9:42 PM, Jozef Hartinger <[hidden email]> 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
>> [hidden email]
>> 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.
>
>
_______________________________________________
cdi-dev mailing list
[hidden email]
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.
Reply | Threaded
Open this post in threaded view
|

Re: Getting injection point from Bean#create

arjan tijms
Hi,

On Thu, Nov 20, 2014 at 11:07 PM, Romain Manni-Bucau
<[hidden email]> wrote:
> Not sure what it means actually. InjectionPoint is highly contextual so
> having an exception (not a npe of course) would make sense to me.
>
> Bean#create is a "you know what you do" from my understanding since
> interceptors/decorators are not supported for instance so it shouldnt rely
> of things like that, no?

Sure, no interceptor/decorators, but the injection point -is- there of
course. I can see it being set in OWB as a special property on the
creational context if I walk down the stack trace in a debugger when
my Bean#create method is being called. An injection point is something
that implementations of Bean could always need, for instance to
retrieve the name of the field into which injection is taking place.

Of course, it being a "you know what you do", it's okay that it's not
as simple as having it injected into the Bean, and that some extra
code is needed to obtain it. As long as there is at least -a- way to
get hold of it.

The method I posted in the openings post does in fact work, but it
feels slightly hacky. If that's an acceptable way to get the injection
point, then so be it. But just wondering if it's not something that
works by chance and may break later.

Kind regards,
Arjan
_______________________________________________
cdi-dev mailing list
[hidden email]
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.
Reply | Threaded
Open this post in threaded view
|

Re: Getting injection point from Bean#create

Jozef Hartinger
The workaround is very ugly. Instead of going that path OWB should be
fixed to support the simple way.

On 11/20/2014 11:22 PM, arjan tijms wrote:

> Hi,
>
> On Thu, Nov 20, 2014 at 11:07 PM, Romain Manni-Bucau
> <[hidden email]> wrote:
>> Not sure what it means actually. InjectionPoint is highly contextual so
>> having an exception (not a npe of course) would make sense to me.
>>
>> Bean#create is a "you know what you do" from my understanding since
>> interceptors/decorators are not supported for instance so it shouldnt rely
>> of things like that, no?
> Sure, no interceptor/decorators, but the injection point -is- there of
> course. I can see it being set in OWB as a special property on the
> creational context if I walk down the stack trace in a debugger when
> my Bean#create method is being called. An injection point is something
> that implementations of Bean could always need, for instance to
> retrieve the name of the field into which injection is taking place.
>
> Of course, it being a "you know what you do", it's okay that it's not
> as simple as having it injected into the Bean, and that some extra
> code is needed to obtain it. As long as there is at least -a- way to
> get hold of it.
>
> The method I posted in the openings post does in fact work, but it
> feels slightly hacky. If that's an acceptable way to get the injection
> point, then so be it. But just wondering if it's not something that
> works by chance and may break later.
>
> Kind regards,
> Arjan
> _______________________________________________
> cdi-dev mailing list
> [hidden email]
> 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.

_______________________________________________
cdi-dev mailing list
[hidden email]
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.
Reply | Threaded
Open this post in threaded view
|

Re: Getting injection point from Bean#create

Romain Manni-Bucau
@Jozef: InjectionPoint ip = getBean(InjectionPoint.class); doesn't
mean anything in the absolute. So why should it be allowed?


Romain Manni-Bucau
@rmannibucau
http://www.tomitribe.com
http://rmannibucau.wordpress.com
https://github.com/rmannibucau


2014-11-21 9:57 GMT+01:00 Jozef Hartinger <[hidden email]>:

> The workaround is very ugly. Instead of going that path OWB should be fixed
> to support the simple way.
>
>
> On 11/20/2014 11:22 PM, arjan tijms wrote:
>>
>> Hi,
>>
>> On Thu, Nov 20, 2014 at 11:07 PM, Romain Manni-Bucau
>> <[hidden email]> wrote:
>>>
>>> Not sure what it means actually. InjectionPoint is highly contextual so
>>> having an exception (not a npe of course) would make sense to me.
>>>
>>> Bean#create is a "you know what you do" from my understanding since
>>> interceptors/decorators are not supported for instance so it shouldnt
>>> rely
>>> of things like that, no?
>>
>> Sure, no interceptor/decorators, but the injection point -is- there of
>> course. I can see it being set in OWB as a special property on the
>> creational context if I walk down the stack trace in a debugger when
>> my Bean#create method is being called. An injection point is something
>> that implementations of Bean could always need, for instance to
>> retrieve the name of the field into which injection is taking place.
>>
>> Of course, it being a "you know what you do", it's okay that it's not
>> as simple as having it injected into the Bean, and that some extra
>> code is needed to obtain it. As long as there is at least -a- way to
>> get hold of it.
>>
>> The method I posted in the openings post does in fact work, but it
>> feels slightly hacky. If that's an acceptable way to get the injection
>> point, then so be it. But just wondering if it's not something that
>> works by chance and may break later.
>>
>> Kind regards,
>> Arjan
>> _______________________________________________
>> cdi-dev mailing list
>> [hidden email]
>> 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.
>
>
_______________________________________________
cdi-dev mailing list
[hidden email]
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.
Reply | Threaded
Open this post in threaded view
|

Re: Getting injection point from Bean#create

Jozef Hartinger
It means "give me an instance of type InjectionPoint and @Default
qualifier". In default setup this should be served by the built-in
Bean<InjectionPoint>".

On 11/21/2014 10:12 AM, Romain Manni-Bucau wrote:

> @Jozef: InjectionPoint ip = getBean(InjectionPoint.class); doesn't
> mean anything in the absolute. So why should it be allowed?
>
>
> Romain Manni-Bucau
> @rmannibucau
> http://www.tomitribe.com
> http://rmannibucau.wordpress.com
> https://github.com/rmannibucau
>
>
> 2014-11-21 9:57 GMT+01:00 Jozef Hartinger <[hidden email]>:
>> The workaround is very ugly. Instead of going that path OWB should be fixed
>> to support the simple way.
>>
>>
>> On 11/20/2014 11:22 PM, arjan tijms wrote:
>>> Hi,
>>>
>>> On Thu, Nov 20, 2014 at 11:07 PM, Romain Manni-Bucau
>>> <[hidden email]> wrote:
>>>> Not sure what it means actually. InjectionPoint is highly contextual so
>>>> having an exception (not a npe of course) would make sense to me.
>>>>
>>>> Bean#create is a "you know what you do" from my understanding since
>>>> interceptors/decorators are not supported for instance so it shouldnt
>>>> rely
>>>> of things like that, no?
>>> Sure, no interceptor/decorators, but the injection point -is- there of
>>> course. I can see it being set in OWB as a special property on the
>>> creational context if I walk down the stack trace in a debugger when
>>> my Bean#create method is being called. An injection point is something
>>> that implementations of Bean could always need, for instance to
>>> retrieve the name of the field into which injection is taking place.
>>>
>>> Of course, it being a "you know what you do", it's okay that it's not
>>> as simple as having it injected into the Bean, and that some extra
>>> code is needed to obtain it. As long as there is at least -a- way to
>>> get hold of it.
>>>
>>> The method I posted in the openings post does in fact work, but it
>>> feels slightly hacky. If that's an acceptable way to get the injection
>>> point, then so be it. But just wondering if it's not something that
>>> works by chance and may break later.
>>>
>>> Kind regards,
>>> Arjan
>>> _______________________________________________
>>> cdi-dev mailing list
>>> [hidden email]
>>> 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.
>>

_______________________________________________
cdi-dev mailing list
[hidden email]
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.
Reply | Threaded
Open this post in threaded view
|

Re: Getting injection point from Bean#create

Romain Manni-Bucau
yes but what would mean any of the values? Not bound to any injection
it doesn't mean anything


Romain Manni-Bucau
@rmannibucau
http://www.tomitribe.com
http://rmannibucau.wordpress.com
https://github.com/rmannibucau


2014-11-21 10:19 GMT+01:00 Jozef Hartinger <[hidden email]>:

> It means "give me an instance of type InjectionPoint and @Default
> qualifier". In default setup this should be served by the built-in
> Bean<InjectionPoint>".
>
>
> On 11/21/2014 10:12 AM, Romain Manni-Bucau wrote:
>>
>> @Jozef: InjectionPoint ip = getBean(InjectionPoint.class); doesn't
>> mean anything in the absolute. So why should it be allowed?
>>
>>
>> Romain Manni-Bucau
>> @rmannibucau
>> http://www.tomitribe.com
>> http://rmannibucau.wordpress.com
>> https://github.com/rmannibucau
>>
>>
>> 2014-11-21 9:57 GMT+01:00 Jozef Hartinger <[hidden email]>:
>>>
>>> The workaround is very ugly. Instead of going that path OWB should be
>>> fixed
>>> to support the simple way.
>>>
>>>
>>> On 11/20/2014 11:22 PM, arjan tijms wrote:
>>>>
>>>> Hi,
>>>>
>>>> On Thu, Nov 20, 2014 at 11:07 PM, Romain Manni-Bucau
>>>> <[hidden email]> wrote:
>>>>>
>>>>> Not sure what it means actually. InjectionPoint is highly contextual so
>>>>> having an exception (not a npe of course) would make sense to me.
>>>>>
>>>>> Bean#create is a "you know what you do" from my understanding since
>>>>> interceptors/decorators are not supported for instance so it shouldnt
>>>>> rely
>>>>> of things like that, no?
>>>>
>>>> Sure, no interceptor/decorators, but the injection point -is- there of
>>>> course. I can see it being set in OWB as a special property on the
>>>> creational context if I walk down the stack trace in a debugger when
>>>> my Bean#create method is being called. An injection point is something
>>>> that implementations of Bean could always need, for instance to
>>>> retrieve the name of the field into which injection is taking place.
>>>>
>>>> Of course, it being a "you know what you do", it's okay that it's not
>>>> as simple as having it injected into the Bean, and that some extra
>>>> code is needed to obtain it. As long as there is at least -a- way to
>>>> get hold of it.
>>>>
>>>> The method I posted in the openings post does in fact work, but it
>>>> feels slightly hacky. If that's an acceptable way to get the injection
>>>> point, then so be it. But just wondering if it's not something that
>>>> works by chance and may break later.
>>>>
>>>> Kind regards,
>>>> Arjan
>>>> _______________________________________________
>>>> cdi-dev mailing list
>>>> [hidden email]
>>>> 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.
>>>
>>>
>
_______________________________________________
cdi-dev mailing list
[hidden email]
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.
Reply | Threaded
Open this post in threaded view
|

Re: Getting injection point from Bean#create

Jozef Hartinger
We may not be on the same page here. Here is my understanding of Arjan's
setup:

There is a custom Bean e.g. "FooBean implements Bean<Foo>" producing
instances of Foo. I assume the bean is @Dependent.

Then, there is a Bar class e.g.
public class Bar {
     @Inject Foo foo;
}

If FooBean#create() calls "InjectionPoint ip =
getBean(InjectionPoint.class);" it means "I want to see an
InjectionPoint representing where I am being injected". In our case this
would be InjectionPoint representing Bar.foo field.

This is no different from a producer method injecting InjectionPoint
directly.

On 11/21/2014 10:23 AM, Romain Manni-Bucau wrote:

> yes but what would mean any of the values? Not bound to any injection
> it doesn't mean anything
>
>
> Romain Manni-Bucau
> @rmannibucau
> http://www.tomitribe.com
> http://rmannibucau.wordpress.com
> https://github.com/rmannibucau
>
>
> 2014-11-21 10:19 GMT+01:00 Jozef Hartinger <[hidden email]>:
>> It means "give me an instance of type InjectionPoint and @Default
>> qualifier". In default setup this should be served by the built-in
>> Bean<InjectionPoint>".
>>
>>
>> On 11/21/2014 10:12 AM, Romain Manni-Bucau wrote:
>>> @Jozef: InjectionPoint ip = getBean(InjectionPoint.class); doesn't
>>> mean anything in the absolute. So why should it be allowed?
>>>
>>>
>>> Romain Manni-Bucau
>>> @rmannibucau
>>> http://www.tomitribe.com
>>> http://rmannibucau.wordpress.com
>>> https://github.com/rmannibucau
>>>
>>>
>>> 2014-11-21 9:57 GMT+01:00 Jozef Hartinger <[hidden email]>:
>>>> The workaround is very ugly. Instead of going that path OWB should be
>>>> fixed
>>>> to support the simple way.
>>>>
>>>>
>>>> On 11/20/2014 11:22 PM, arjan tijms wrote:
>>>>> Hi,
>>>>>
>>>>> On Thu, Nov 20, 2014 at 11:07 PM, Romain Manni-Bucau
>>>>> <[hidden email]> wrote:
>>>>>> Not sure what it means actually. InjectionPoint is highly contextual so
>>>>>> having an exception (not a npe of course) would make sense to me.
>>>>>>
>>>>>> Bean#create is a "you know what you do" from my understanding since
>>>>>> interceptors/decorators are not supported for instance so it shouldnt
>>>>>> rely
>>>>>> of things like that, no?
>>>>> Sure, no interceptor/decorators, but the injection point -is- there of
>>>>> course. I can see it being set in OWB as a special property on the
>>>>> creational context if I walk down the stack trace in a debugger when
>>>>> my Bean#create method is being called. An injection point is something
>>>>> that implementations of Bean could always need, for instance to
>>>>> retrieve the name of the field into which injection is taking place.
>>>>>
>>>>> Of course, it being a "you know what you do", it's okay that it's not
>>>>> as simple as having it injected into the Bean, and that some extra
>>>>> code is needed to obtain it. As long as there is at least -a- way to
>>>>> get hold of it.
>>>>>
>>>>> The method I posted in the openings post does in fact work, but it
>>>>> feels slightly hacky. If that's an acceptable way to get the injection
>>>>> point, then so be it. But just wondering if it's not something that
>>>>> works by chance and may break later.
>>>>>
>>>>> Kind regards,
>>>>> Arjan
>>>>> _______________________________________________
>>>>> cdi-dev mailing list
>>>>> [hidden email]
>>>>> 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.
>>>>

_______________________________________________
cdi-dev mailing list
[hidden email]
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.
Reply | Threaded
Open this post in threaded view
|

Re: Getting injection point from Bean#create

Romain Manni-Bucau
Ok makes sense so it means failing in getBean(Foo.class) is fine
right? Another issue is what if there is @inject InjectionPoint ip, in
Foo? This should work (validated in TCKs)


Romain Manni-Bucau
@rmannibucau
http://www.tomitribe.com
http://rmannibucau.wordpress.com
https://github.com/rmannibucau


2014-11-21 10:31 GMT+01:00 Jozef Hartinger <[hidden email]>:

> We may not be on the same page here. Here is my understanding of Arjan's
> setup:
>
> There is a custom Bean e.g. "FooBean implements Bean<Foo>" producing
> instances of Foo. I assume the bean is @Dependent.
>
> Then, there is a Bar class e.g.
> public class Bar {
>     @Inject Foo foo;
> }
>
> If FooBean#create() calls "InjectionPoint ip =
> getBean(InjectionPoint.class);" it means "I want to see an InjectionPoint
> representing where I am being injected". In our case this would be
> InjectionPoint representing Bar.foo field.
>
> This is no different from a producer method injecting InjectionPoint
> directly.
>
>
> On 11/21/2014 10:23 AM, Romain Manni-Bucau wrote:
>>
>> yes but what would mean any of the values? Not bound to any injection
>> it doesn't mean anything
>>
>>
>> Romain Manni-Bucau
>> @rmannibucau
>> http://www.tomitribe.com
>> http://rmannibucau.wordpress.com
>> https://github.com/rmannibucau
>>
>>
>> 2014-11-21 10:19 GMT+01:00 Jozef Hartinger <[hidden email]>:
>>>
>>> It means "give me an instance of type InjectionPoint and @Default
>>> qualifier". In default setup this should be served by the built-in
>>> Bean<InjectionPoint>".
>>>
>>>
>>> On 11/21/2014 10:12 AM, Romain Manni-Bucau wrote:
>>>>
>>>> @Jozef: InjectionPoint ip = getBean(InjectionPoint.class); doesn't
>>>> mean anything in the absolute. So why should it be allowed?
>>>>
>>>>
>>>> Romain Manni-Bucau
>>>> @rmannibucau
>>>> http://www.tomitribe.com
>>>> http://rmannibucau.wordpress.com
>>>> https://github.com/rmannibucau
>>>>
>>>>
>>>> 2014-11-21 9:57 GMT+01:00 Jozef Hartinger <[hidden email]>:
>>>>>
>>>>> The workaround is very ugly. Instead of going that path OWB should be
>>>>> fixed
>>>>> to support the simple way.
>>>>>
>>>>>
>>>>> On 11/20/2014 11:22 PM, arjan tijms wrote:
>>>>>>
>>>>>> Hi,
>>>>>>
>>>>>> On Thu, Nov 20, 2014 at 11:07 PM, Romain Manni-Bucau
>>>>>> <[hidden email]> wrote:
>>>>>>>
>>>>>>> Not sure what it means actually. InjectionPoint is highly contextual
>>>>>>> so
>>>>>>> having an exception (not a npe of course) would make sense to me.
>>>>>>>
>>>>>>> Bean#create is a "you know what you do" from my understanding since
>>>>>>> interceptors/decorators are not supported for instance so it shouldnt
>>>>>>> rely
>>>>>>> of things like that, no?
>>>>>>
>>>>>> Sure, no interceptor/decorators, but the injection point -is- there of
>>>>>> course. I can see it being set in OWB as a special property on the
>>>>>> creational context if I walk down the stack trace in a debugger when
>>>>>> my Bean#create method is being called. An injection point is something
>>>>>> that implementations of Bean could always need, for instance to
>>>>>> retrieve the name of the field into which injection is taking place.
>>>>>>
>>>>>> Of course, it being a "you know what you do", it's okay that it's not
>>>>>> as simple as having it injected into the Bean, and that some extra
>>>>>> code is needed to obtain it. As long as there is at least -a- way to
>>>>>> get hold of it.
>>>>>>
>>>>>> The method I posted in the openings post does in fact work, but it
>>>>>> feels slightly hacky. If that's an acceptable way to get the injection
>>>>>> point, then so be it. But just wondering if it's not something that
>>>>>> works by chance and may break later.
>>>>>>
>>>>>> Kind regards,
>>>>>> Arjan
>>>>>> _______________________________________________
>>>>>> cdi-dev mailing list
>>>>>> [hidden email]
>>>>>> 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.
>>>>>
>>>>>
>
_______________________________________________
cdi-dev mailing list
[hidden email]
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.
Reply | Threaded
Open this post in threaded view
|

Re: Getting injection point from Bean#create

Romain Manni-Bucau
PS: BTW should work on OWB 1.5.0-SNAPSHOT now


Romain Manni-Bucau
@rmannibucau
http://www.tomitribe.com
http://rmannibucau.wordpress.com
https://github.com/rmannibucau


2014-11-21 11:17 GMT+01:00 Romain Manni-Bucau <[hidden email]>:

> Ok makes sense so it means failing in getBean(Foo.class) is fine
> right? Another issue is what if there is @inject InjectionPoint ip, in
> Foo? This should work (validated in TCKs)
>
>
> Romain Manni-Bucau
> @rmannibucau
> http://www.tomitribe.com
> http://rmannibucau.wordpress.com
> https://github.com/rmannibucau
>
>
> 2014-11-21 10:31 GMT+01:00 Jozef Hartinger <[hidden email]>:
>> We may not be on the same page here. Here is my understanding of Arjan's
>> setup:
>>
>> There is a custom Bean e.g. "FooBean implements Bean<Foo>" producing
>> instances of Foo. I assume the bean is @Dependent.
>>
>> Then, there is a Bar class e.g.
>> public class Bar {
>>     @Inject Foo foo;
>> }
>>
>> If FooBean#create() calls "InjectionPoint ip =
>> getBean(InjectionPoint.class);" it means "I want to see an InjectionPoint
>> representing where I am being injected". In our case this would be
>> InjectionPoint representing Bar.foo field.
>>
>> This is no different from a producer method injecting InjectionPoint
>> directly.
>>
>>
>> On 11/21/2014 10:23 AM, Romain Manni-Bucau wrote:
>>>
>>> yes but what would mean any of the values? Not bound to any injection
>>> it doesn't mean anything
>>>
>>>
>>> Romain Manni-Bucau
>>> @rmannibucau
>>> http://www.tomitribe.com
>>> http://rmannibucau.wordpress.com
>>> https://github.com/rmannibucau
>>>
>>>
>>> 2014-11-21 10:19 GMT+01:00 Jozef Hartinger <[hidden email]>:
>>>>
>>>> It means "give me an instance of type InjectionPoint and @Default
>>>> qualifier". In default setup this should be served by the built-in
>>>> Bean<InjectionPoint>".
>>>>
>>>>
>>>> On 11/21/2014 10:12 AM, Romain Manni-Bucau wrote:
>>>>>
>>>>> @Jozef: InjectionPoint ip = getBean(InjectionPoint.class); doesn't
>>>>> mean anything in the absolute. So why should it be allowed?
>>>>>
>>>>>
>>>>> Romain Manni-Bucau
>>>>> @rmannibucau
>>>>> http://www.tomitribe.com
>>>>> http://rmannibucau.wordpress.com
>>>>> https://github.com/rmannibucau
>>>>>
>>>>>
>>>>> 2014-11-21 9:57 GMT+01:00 Jozef Hartinger <[hidden email]>:
>>>>>>
>>>>>> The workaround is very ugly. Instead of going that path OWB should be
>>>>>> fixed
>>>>>> to support the simple way.
>>>>>>
>>>>>>
>>>>>> On 11/20/2014 11:22 PM, arjan tijms wrote:
>>>>>>>
>>>>>>> Hi,
>>>>>>>
>>>>>>> On Thu, Nov 20, 2014 at 11:07 PM, Romain Manni-Bucau
>>>>>>> <[hidden email]> wrote:
>>>>>>>>
>>>>>>>> Not sure what it means actually. InjectionPoint is highly contextual
>>>>>>>> so
>>>>>>>> having an exception (not a npe of course) would make sense to me.
>>>>>>>>
>>>>>>>> Bean#create is a "you know what you do" from my understanding since
>>>>>>>> interceptors/decorators are not supported for instance so it shouldnt
>>>>>>>> rely
>>>>>>>> of things like that, no?
>>>>>>>
>>>>>>> Sure, no interceptor/decorators, but the injection point -is- there of
>>>>>>> course. I can see it being set in OWB as a special property on the
>>>>>>> creational context if I walk down the stack trace in a debugger when
>>>>>>> my Bean#create method is being called. An injection point is something
>>>>>>> that implementations of Bean could always need, for instance to
>>>>>>> retrieve the name of the field into which injection is taking place.
>>>>>>>
>>>>>>> Of course, it being a "you know what you do", it's okay that it's not
>>>>>>> as simple as having it injected into the Bean, and that some extra
>>>>>>> code is needed to obtain it. As long as there is at least -a- way to
>>>>>>> get hold of it.
>>>>>>>
>>>>>>> The method I posted in the openings post does in fact work, but it
>>>>>>> feels slightly hacky. If that's an acceptable way to get the injection
>>>>>>> point, then so be it. But just wondering if it's not something that
>>>>>>> works by chance and may break later.
>>>>>>>
>>>>>>> Kind regards,
>>>>>>> Arjan
>>>>>>> _______________________________________________
>>>>>>> cdi-dev mailing list
>>>>>>> [hidden email]
>>>>>>> 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.
>>>>>>
>>>>>>
>>
_______________________________________________
cdi-dev mailing list
[hidden email]
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.
Reply | Threaded
Open this post in threaded view
|

Re: Getting injection point from Bean#create

arjan tijms
In reply to this post by Jozef Hartinger
Hi,

On Fri, Nov 21, 2014 at 10:31 AM, Jozef Hartinger <[hidden email]> wrote:
> Here is my understanding of Arjan's
> setup:
> [...]
>
> This is no different from a producer method injecting InjectionPoint
> directly.

That's indeed the exact setup.

On Fri, Nov 21, 2014 at 11:34 AM, Romain Manni-Bucau
<[hidden email]> wrote:
> PS: BTW should work on OWB 1.5.0-SNAPSHOT now

Wow, I'll look at the latest SNAPSHOT of OWB then. Thanks Romain!

Kind regards,
Arjan




>
>
> On 11/21/2014 10:23 AM, Romain Manni-Bucau wrote:
>>
>> yes but what would mean any of the values? Not bound to any injection
>> it doesn't mean anything
>>
>>
>> Romain Manni-Bucau
>> @rmannibucau
>> http://www.tomitribe.com
>> http://rmannibucau.wordpress.com
>> https://github.com/rmannibucau
>>
>>
>> 2014-11-21 10:19 GMT+01:00 Jozef Hartinger <[hidden email]>:
>>>
>>> It means "give me an instance of type InjectionPoint and @Default
>>> qualifier". In default setup this should be served by the built-in
>>> Bean<InjectionPoint>".
>>>
>>>
>>> On 11/21/2014 10:12 AM, Romain Manni-Bucau wrote:
>>>>
>>>> @Jozef: InjectionPoint ip = getBean(InjectionPoint.class); doesn't
>>>> mean anything in the absolute. So why should it be allowed?
>>>>
>>>>
>>>> Romain Manni-Bucau
>>>> @rmannibucau
>>>> http://www.tomitribe.com
>>>> http://rmannibucau.wordpress.com
>>>> https://github.com/rmannibucau
>>>>
>>>>
>>>> 2014-11-21 9:57 GMT+01:00 Jozef Hartinger <[hidden email]>:
>>>>>
>>>>> The workaround is very ugly. Instead of going that path OWB should be
>>>>> fixed
>>>>> to support the simple way.
>>>>>
>>>>>
>>>>> On 11/20/2014 11:22 PM, arjan tijms wrote:
>>>>>>
>>>>>> Hi,
>>>>>>
>>>>>> On Thu, Nov 20, 2014 at 11:07 PM, Romain Manni-Bucau
>>>>>> <[hidden email]> wrote:
>>>>>>>
>>>>>>> Not sure what it means actually. InjectionPoint is highly contextual
>>>>>>> so
>>>>>>> having an exception (not a npe of course) would make sense to me.
>>>>>>>
>>>>>>> Bean#create is a "you know what you do" from my understanding since
>>>>>>> interceptors/decorators are not supported for instance so it shouldnt
>>>>>>> rely
>>>>>>> of things like that, no?
>>>>>>
>>>>>> Sure, no interceptor/decorators, but the injection point -is- there of
>>>>>> course. I can see it being set in OWB as a special property on the
>>>>>> creational context if I walk down the stack trace in a debugger when
>>>>>> my Bean#create method is being called. An injection point is something
>>>>>> that implementations of Bean could always need, for instance to
>>>>>> retrieve the name of the field into which injection is taking place.
>>>>>>
>>>>>> Of course, it being a "you know what you do", it's okay that it's not
>>>>>> as simple as having it injected into the Bean, and that some extra
>>>>>> code is needed to obtain it. As long as there is at least -a- way to
>>>>>> get hold of it.
>>>>>>
>>>>>> The method I posted in the openings post does in fact work, but it
>>>>>> feels slightly hacky. If that's an acceptable way to get the injection
>>>>>> point, then so be it. But just wondering if it's not something that
>>>>>> works by chance and may break later.
>>>>>>
>>>>>> Kind regards,
>>>>>> Arjan
>>>>>> _______________________________________________
>>>>>> cdi-dev mailing list
>>>>>> [hidden email]
>>>>>> 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.
>>>>>
>>>>>
>
_______________________________________________
cdi-dev mailing list
[hidden email]
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.
Reply | Threaded
Open this post in threaded view
|

Re: Getting injection point from Bean#create

Jozef Hartinger
In reply to this post by Romain Manni-Bucau

On 11/21/2014 11:17 AM, Romain Manni-Bucau wrote:
> Ok makes sense so it means failing in getBean(Foo.class) is fine
> right?
Correct, if Foo is not injected into an actual injection point but
instead BeanManager.getReference() is used instead, it is OK for Foo to
get null as the InjectionPoint
>   Another issue is what if there is @inject InjectionPoint ip, in
> Foo? This should work (validated in TCKs)
Yes, same as above.

>
>
> Romain Manni-Bucau
> @rmannibucau
> http://www.tomitribe.com
> http://rmannibucau.wordpress.com
> https://github.com/rmannibucau
>
>
> 2014-11-21 10:31 GMT+01:00 Jozef Hartinger <[hidden email]>:
>> We may not be on the same page here. Here is my understanding of Arjan's
>> setup:
>>
>> There is a custom Bean e.g. "FooBean implements Bean<Foo>" producing
>> instances of Foo. I assume the bean is @Dependent.
>>
>> Then, there is a Bar class e.g.
>> public class Bar {
>>      @Inject Foo foo;
>> }
>>
>> If FooBean#create() calls "InjectionPoint ip =
>> getBean(InjectionPoint.class);" it means "I want to see an InjectionPoint
>> representing where I am being injected". In our case this would be
>> InjectionPoint representing Bar.foo field.
>>
>> This is no different from a producer method injecting InjectionPoint
>> directly.
>>
>>
>> On 11/21/2014 10:23 AM, Romain Manni-Bucau wrote:
>>> yes but what would mean any of the values? Not bound to any injection
>>> it doesn't mean anything
>>>
>>>
>>> Romain Manni-Bucau
>>> @rmannibucau
>>> http://www.tomitribe.com
>>> http://rmannibucau.wordpress.com
>>> https://github.com/rmannibucau
>>>
>>>
>>> 2014-11-21 10:19 GMT+01:00 Jozef Hartinger <[hidden email]>:
>>>> It means "give me an instance of type InjectionPoint and @Default
>>>> qualifier". In default setup this should be served by the built-in
>>>> Bean<InjectionPoint>".
>>>>
>>>>
>>>> On 11/21/2014 10:12 AM, Romain Manni-Bucau wrote:
>>>>> @Jozef: InjectionPoint ip = getBean(InjectionPoint.class); doesn't
>>>>> mean anything in the absolute. So why should it be allowed?
>>>>>
>>>>>
>>>>> Romain Manni-Bucau
>>>>> @rmannibucau
>>>>> http://www.tomitribe.com
>>>>> http://rmannibucau.wordpress.com
>>>>> https://github.com/rmannibucau
>>>>>
>>>>>
>>>>> 2014-11-21 9:57 GMT+01:00 Jozef Hartinger <[hidden email]>:
>>>>>> The workaround is very ugly. Instead of going that path OWB should be
>>>>>> fixed
>>>>>> to support the simple way.
>>>>>>
>>>>>>
>>>>>> On 11/20/2014 11:22 PM, arjan tijms wrote:
>>>>>>> Hi,
>>>>>>>
>>>>>>> On Thu, Nov 20, 2014 at 11:07 PM, Romain Manni-Bucau
>>>>>>> <[hidden email]> wrote:
>>>>>>>> Not sure what it means actually. InjectionPoint is highly contextual
>>>>>>>> so
>>>>>>>> having an exception (not a npe of course) would make sense to me.
>>>>>>>>
>>>>>>>> Bean#create is a "you know what you do" from my understanding since
>>>>>>>> interceptors/decorators are not supported for instance so it shouldnt
>>>>>>>> rely
>>>>>>>> of things like that, no?
>>>>>>> Sure, no interceptor/decorators, but the injection point -is- there of
>>>>>>> course. I can see it being set in OWB as a special property on the
>>>>>>> creational context if I walk down the stack trace in a debugger when
>>>>>>> my Bean#create method is being called. An injection point is something
>>>>>>> that implementations of Bean could always need, for instance to
>>>>>>> retrieve the name of the field into which injection is taking place.
>>>>>>>
>>>>>>> Of course, it being a "you know what you do", it's okay that it's not
>>>>>>> as simple as having it injected into the Bean, and that some extra
>>>>>>> code is needed to obtain it. As long as there is at least -a- way to
>>>>>>> get hold of it.
>>>>>>>
>>>>>>> The method I posted in the openings post does in fact work, but it
>>>>>>> feels slightly hacky. If that's an acceptable way to get the injection
>>>>>>> point, then so be it. But just wondering if it's not something that
>>>>>>> works by chance and may break later.
>>>>>>>
>>>>>>> Kind regards,
>>>>>>> Arjan
>>>>>>> _______________________________________________
>>>>>>> cdi-dev mailing list
>>>>>>> [hidden email]
>>>>>>> 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.
>>>>>>

_______________________________________________
cdi-dev mailing list
[hidden email]
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.
Reply | Threaded
Open this post in threaded view
|

Re: Getting injection point from Bean#create

Arne Limburg
In reply to this post by arjan tijms
Hi Arjan,

I¹ve verified, that this is a bug in OpenWebBeans.
I've created
https://issues.apache.org/jira/browse/OWB-1030
and I¹m going to fix it.

Btw.: You have to use the CreationalContext of the bean (the way OWB
currently throws the "Inconsistent injection point stack" exception)

Cheers,
Arne


Am 21.11.14 12:58 schrieb "arjan tijms" unter <[hidden email]>:

>Hi,
>
>On Fri, Nov 21, 2014 at 10:31 AM, Jozef Hartinger <[hidden email]>
>wrote:
>> Here is my understanding of Arjan's
>> setup:
>> [...]
>>
>> This is no different from a producer method injecting InjectionPoint
>> directly.
>
>That's indeed the exact setup.
>
>On Fri, Nov 21, 2014 at 11:34 AM, Romain Manni-Bucau
><[hidden email]> wrote:
>> PS: BTW should work on OWB 1.5.0-SNAPSHOT now
>
>Wow, I'll look at the latest SNAPSHOT of OWB then. Thanks Romain!
>
>Kind regards,
>Arjan
>
>
>
>
>>
>>
>> On 11/21/2014 10:23 AM, Romain Manni-Bucau wrote:
>>>
>>> yes but what would mean any of the values? Not bound to any injection
>>> it doesn't mean anything
>>>
>>>
>>> Romain Manni-Bucau
>>> @rmannibucau
>>> http://www.tomitribe.com
>>> http://rmannibucau.wordpress.com
>>> https://github.com/rmannibucau
>>>
>>>
>>> 2014-11-21 10:19 GMT+01:00 Jozef Hartinger <[hidden email]>:
>>>>
>>>> It means "give me an instance of type InjectionPoint and @Default
>>>> qualifier". In default setup this should be served by the built-in
>>>> Bean<InjectionPoint>".
>>>>
>>>>
>>>> On 11/21/2014 10:12 AM, Romain Manni-Bucau wrote:
>>>>>
>>>>> @Jozef: InjectionPoint ip = getBean(InjectionPoint.class); doesn't
>>>>> mean anything in the absolute. So why should it be allowed?
>>>>>
>>>>>
>>>>> Romain Manni-Bucau
>>>>> @rmannibucau
>>>>> http://www.tomitribe.com
>>>>> http://rmannibucau.wordpress.com
>>>>> https://github.com/rmannibucau
>>>>>
>>>>>
>>>>> 2014-11-21 9:57 GMT+01:00 Jozef Hartinger <[hidden email]>:
>>>>>>
>>>>>> The workaround is very ugly. Instead of going that path OWB should
>>>>>>be
>>>>>> fixed
>>>>>> to support the simple way.
>>>>>>
>>>>>>
>>>>>> On 11/20/2014 11:22 PM, arjan tijms wrote:
>>>>>>>
>>>>>>> Hi,
>>>>>>>
>>>>>>> On Thu, Nov 20, 2014 at 11:07 PM, Romain Manni-Bucau
>>>>>>> <[hidden email]> wrote:
>>>>>>>>
>>>>>>>> Not sure what it means actually. InjectionPoint is highly
>>>>>>>>contextual
>>>>>>>> so
>>>>>>>> having an exception (not a npe of course) would make sense to me.
>>>>>>>>
>>>>>>>> Bean#create is a "you know what you do" from my understanding
>>>>>>>>since
>>>>>>>> interceptors/decorators are not supported for instance so it
>>>>>>>>shouldnt
>>>>>>>> rely
>>>>>>>> of things like that, no?
>>>>>>>
>>>>>>> Sure, no interceptor/decorators, but the injection point -is-
>>>>>>>there of
>>>>>>> course. I can see it being set in OWB as a special property on the
>>>>>>> creational context if I walk down the stack trace in a debugger
>>>>>>>when
>>>>>>> my Bean#create method is being called. An injection point is
>>>>>>>something
>>>>>>> that implementations of Bean could always need, for instance to
>>>>>>> retrieve the name of the field into which injection is taking
>>>>>>>place.
>>>>>>>
>>>>>>> Of course, it being a "you know what you do", it's okay that it's
>>>>>>>not
>>>>>>> as simple as having it injected into the Bean, and that some extra
>>>>>>> code is needed to obtain it. As long as there is at least -a- way
>>>>>>>to
>>>>>>> get hold of it.
>>>>>>>
>>>>>>> The method I posted in the openings post does in fact work, but it
>>>>>>> feels slightly hacky. If that's an acceptable way to get the
>>>>>>>injection
>>>>>>> point, then so be it. But just wondering if it's not something that
>>>>>>> works by chance and may break later.
>>>>>>>
>>>>>>> Kind regards,
>>>>>>> Arjan
>>>>>>> _______________________________________________
>>>>>>> cdi-dev mailing list
>>>>>>> [hidden email]
>>>>>>> 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.
>>>>>>
>>>>>>
>>
>_______________________________________________
>cdi-dev mailing list
>[hidden email]
>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.


_______________________________________________
cdi-dev mailing list
[hidden email]
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.
Reply | Threaded
Open this post in threaded view
|

Re: Getting injection point from Bean#create

arjan tijms
Hi

On Sat, Nov 22, 2014 at 11:23 AM, Arne Limburg
<[hidden email]> wrote:
> I¹ve verified, that this is a bug in OpenWebBeans.
> I've created
> https://issues.apache.org/jira/browse/OWB-1030
> and I¹m going to fix it.

Cool, thanks! Btw, didn't Romain already fix something? His last reply was:

>PS: BTW should work on OWB 1.5.0-SNAPSHOT now

> Btw.: You have to use the CreationalContext of the bean (the way OWB
> currently throws the "Inconsistent injection point stack" exception)

Okay, I tested whether that works on Weld too, and it does. So after
your fix the following should work on both Weld and OWB then:

public class SomeBean implements Bean<Object> {

   BeanManager beanManager;

    @Override
    public Object create(CreationalContext<Object> creationalContext) {

        Bean<InjectionPoint> bean = (Bean<InjectionPoint>)
beanManager.resolve(beanManager.getBeans(InjectionPoint.class));
        InjectionPoint ip = (InjectionPoint)
beanManager.getReference(bean, InjectionPoint.class,
creationalContext);

        // ...
    }

    // ctor assigning beanManager and other methods below
}

Kind regards,
Arjan




>
> Cheers,
> Arne
>
>
> Am 21.11.14 12:58 schrieb "arjan tijms" unter <[hidden email]>:
>
>>Hi,
>>
>>On Fri, Nov 21, 2014 at 10:31 AM, Jozef Hartinger <[hidden email]>
>>wrote:
>>> Here is my understanding of Arjan's
>>> setup:
>>> [...]
>>>
>>> This is no different from a producer method injecting InjectionPoint
>>> directly.
>>
>>That's indeed the exact setup.
>>
>>On Fri, Nov 21, 2014 at 11:34 AM, Romain Manni-Bucau
>><[hidden email]> wrote:
>>> PS: BTW should work on OWB 1.5.0-SNAPSHOT now
>>
>>Wow, I'll look at the latest SNAPSHOT of OWB then. Thanks Romain!
>>
>>Kind regards,
>>Arjan
>>
>>
>>
>>
>>>
>>>
>>> On 11/21/2014 10:23 AM, Romain Manni-Bucau wrote:
>>>>
>>>> yes but what would mean any of the values? Not bound to any injection
>>>> it doesn't mean anything
>>>>
>>>>
>>>> Romain Manni-Bucau
>>>> @rmannibucau
>>>> http://www.tomitribe.com
>>>> http://rmannibucau.wordpress.com
>>>> https://github.com/rmannibucau
>>>>
>>>>
>>>> 2014-11-21 10:19 GMT+01:00 Jozef Hartinger <[hidden email]>:
>>>>>
>>>>> It means "give me an instance of type InjectionPoint and @Default
>>>>> qualifier". In default setup this should be served by the built-in
>>>>> Bean<InjectionPoint>".
>>>>>
>>>>>
>>>>> On 11/21/2014 10:12 AM, Romain Manni-Bucau wrote:
>>>>>>
>>>>>> @Jozef: InjectionPoint ip = getBean(InjectionPoint.class); doesn't
>>>>>> mean anything in the absolute. So why should it be allowed?
>>>>>>
>>>>>>
>>>>>> Romain Manni-Bucau
>>>>>> @rmannibucau
>>>>>> http://www.tomitribe.com
>>>>>> http://rmannibucau.wordpress.com
>>>>>> https://github.com/rmannibucau
>>>>>>
>>>>>>
>>>>>> 2014-11-21 9:57 GMT+01:00 Jozef Hartinger <[hidden email]>:
>>>>>>>
>>>>>>> The workaround is very ugly. Instead of going that path OWB should
>>>>>>>be
>>>>>>> fixed
>>>>>>> to support the simple way.
>>>>>>>
>>>>>>>
>>>>>>> On 11/20/2014 11:22 PM, arjan tijms wrote:
>>>>>>>>
>>>>>>>> Hi,
>>>>>>>>
>>>>>>>> On Thu, Nov 20, 2014 at 11:07 PM, Romain Manni-Bucau
>>>>>>>> <[hidden email]> wrote:
>>>>>>>>>
>>>>>>>>> Not sure what it means actually. InjectionPoint is highly
>>>>>>>>>contextual
>>>>>>>>> so
>>>>>>>>> having an exception (not a npe of course) would make sense to me.
>>>>>>>>>
>>>>>>>>> Bean#create is a "you know what you do" from my understanding
>>>>>>>>>since
>>>>>>>>> interceptors/decorators are not supported for instance so it
>>>>>>>>>shouldnt
>>>>>>>>> rely
>>>>>>>>> of things like that, no?
>>>>>>>>
>>>>>>>> Sure, no interceptor/decorators, but the injection point -is-
>>>>>>>>there of
>>>>>>>> course. I can see it being set in OWB as a special property on the
>>>>>>>> creational context if I walk down the stack trace in a debugger
>>>>>>>>when
>>>>>>>> my Bean#create method is being called. An injection point is
>>>>>>>>something
>>>>>>>> that implementations of Bean could always need, for instance to
>>>>>>>> retrieve the name of the field into which injection is taking
>>>>>>>>place.
>>>>>>>>
>>>>>>>> Of course, it being a "you know what you do", it's okay that it's
>>>>>>>>not
>>>>>>>> as simple as having it injected into the Bean, and that some extra
>>>>>>>> code is needed to obtain it. As long as there is at least -a- way
>>>>>>>>to
>>>>>>>> get hold of it.
>>>>>>>>
>>>>>>>> The method I posted in the openings post does in fact work, but it
>>>>>>>> feels slightly hacky. If that's an acceptable way to get the
>>>>>>>>injection
>>>>>>>> point, then so be it. But just wondering if it's not something that
>>>>>>>> works by chance and may break later.
>>>>>>>>
>>>>>>>> Kind regards,
>>>>>>>> Arjan
>>>>>>>> _______________________________________________
>>>>>>>> cdi-dev mailing list
>>>>>>>> [hidden email]
>>>>>>>> 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.
>>>>>>>
>>>>>>>
>>>
>>_______________________________________________
>>cdi-dev mailing list
>>[hidden email]
>>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.
>

_______________________________________________
cdi-dev mailing list
[hidden email]
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.
Reply | Threaded
Open this post in threaded view
|

Re: Getting injection point from Bean#create

Arne Limburg
Romain did a quick fix and your code will work with that fix as well. So
with OWB trunk your code should work.

But we are currently discussing how this should be done right in OWB.

Cheers,
Arne

Am 22.11.14 15:14 schrieb "arjan tijms" unter <[hidden email]>:

>Hi
>
>On Sat, Nov 22, 2014 at 11:23 AM, Arne Limburg
><[hidden email]> wrote:
>> I¹ve verified, that this is a bug in OpenWebBeans.
>> I've created
>> https://issues.apache.org/jira/browse/OWB-1030
>> and I¹m going to fix it.
>
>Cool, thanks! Btw, didn't Romain already fix something? His last reply
>was:
>
>>PS: BTW should work on OWB 1.5.0-SNAPSHOT now
>
>> Btw.: You have to use the CreationalContext of the bean (the way OWB
>> currently throws the "Inconsistent injection point stack" exception)
>
>Okay, I tested whether that works on Weld too, and it does. So after
>your fix the following should work on both Weld and OWB then:
>
>public class SomeBean implements Bean<Object> {
>
>   BeanManager beanManager;
>
>    @Override
>    public Object create(CreationalContext<Object> creationalContext) {
>
>        Bean<InjectionPoint> bean = (Bean<InjectionPoint>)
>beanManager.resolve(beanManager.getBeans(InjectionPoint.class));
>        InjectionPoint ip = (InjectionPoint)
>beanManager.getReference(bean, InjectionPoint.class,
>creationalContext);
>
>        // ...
>    }
>
>    // ctor assigning beanManager and other methods below
>}
>
>Kind regards,
>Arjan
>
>
>
>
>>
>> Cheers,
>> Arne
>>
>>
>> Am 21.11.14 12:58 schrieb "arjan tijms" unter <[hidden email]>:
>>
>>>Hi,
>>>
>>>On Fri, Nov 21, 2014 at 10:31 AM, Jozef Hartinger <[hidden email]>
>>>wrote:
>>>> Here is my understanding of Arjan's
>>>> setup:
>>>> [...]
>>>>
>>>> This is no different from a producer method injecting InjectionPoint
>>>> directly.
>>>
>>>That's indeed the exact setup.
>>>
>>>On Fri, Nov 21, 2014 at 11:34 AM, Romain Manni-Bucau
>>><[hidden email]> wrote:
>>>> PS: BTW should work on OWB 1.5.0-SNAPSHOT now
>>>
>>>Wow, I'll look at the latest SNAPSHOT of OWB then. Thanks Romain!
>>>
>>>Kind regards,
>>>Arjan
>>>
>>>
>>>
>>>
>>>>
>>>>
>>>> On 11/21/2014 10:23 AM, Romain Manni-Bucau wrote:
>>>>>
>>>>> yes but what would mean any of the values? Not bound to any injection
>>>>> it doesn't mean anything
>>>>>
>>>>>
>>>>> Romain Manni-Bucau
>>>>> @rmannibucau
>>>>> http://www.tomitribe.com
>>>>> http://rmannibucau.wordpress.com
>>>>> https://github.com/rmannibucau
>>>>>
>>>>>
>>>>> 2014-11-21 10:19 GMT+01:00 Jozef Hartinger <[hidden email]>:
>>>>>>
>>>>>> It means "give me an instance of type InjectionPoint and @Default
>>>>>> qualifier". In default setup this should be served by the built-in
>>>>>> Bean<InjectionPoint>".
>>>>>>
>>>>>>
>>>>>> On 11/21/2014 10:12 AM, Romain Manni-Bucau wrote:
>>>>>>>
>>>>>>> @Jozef: InjectionPoint ip = getBean(InjectionPoint.class); doesn't
>>>>>>> mean anything in the absolute. So why should it be allowed?
>>>>>>>
>>>>>>>
>>>>>>> Romain Manni-Bucau
>>>>>>> @rmannibucau
>>>>>>> http://www.tomitribe.com
>>>>>>> http://rmannibucau.wordpress.com
>>>>>>> https://github.com/rmannibucau
>>>>>>>
>>>>>>>
>>>>>>> 2014-11-21 9:57 GMT+01:00 Jozef Hartinger <[hidden email]>:
>>>>>>>>
>>>>>>>> The workaround is very ugly. Instead of going that path OWB should
>>>>>>>>be
>>>>>>>> fixed
>>>>>>>> to support the simple way.
>>>>>>>>
>>>>>>>>
>>>>>>>> On 11/20/2014 11:22 PM, arjan tijms wrote:
>>>>>>>>>
>>>>>>>>> Hi,
>>>>>>>>>
>>>>>>>>> On Thu, Nov 20, 2014 at 11:07 PM, Romain Manni-Bucau
>>>>>>>>> <[hidden email]> wrote:
>>>>>>>>>>
>>>>>>>>>> Not sure what it means actually. InjectionPoint is highly
>>>>>>>>>>contextual
>>>>>>>>>> so
>>>>>>>>>> having an exception (not a npe of course) would make sense to
>>>>>>>>>>me.
>>>>>>>>>>
>>>>>>>>>> Bean#create is a "you know what you do" from my understanding
>>>>>>>>>>since
>>>>>>>>>> interceptors/decorators are not supported for instance so it
>>>>>>>>>>shouldnt
>>>>>>>>>> rely
>>>>>>>>>> of things like that, no?
>>>>>>>>>
>>>>>>>>> Sure, no interceptor/decorators, but the injection point -is-
>>>>>>>>>there of
>>>>>>>>> course. I can see it being set in OWB as a special property on
>>>>>>>>>the
>>>>>>>>> creational context if I walk down the stack trace in a debugger
>>>>>>>>>when
>>>>>>>>> my Bean#create method is being called. An injection point is
>>>>>>>>>something
>>>>>>>>> that implementations of Bean could always need, for instance to
>>>>>>>>> retrieve the name of the field into which injection is taking
>>>>>>>>>place.
>>>>>>>>>
>>>>>>>>> Of course, it being a "you know what you do", it's okay that it's
>>>>>>>>>not
>>>>>>>>> as simple as having it injected into the Bean, and that some
>>>>>>>>>extra
>>>>>>>>> code is needed to obtain it. As long as there is at least -a- way
>>>>>>>>>to
>>>>>>>>> get hold of it.
>>>>>>>>>
>>>>>>>>> The method I posted in the openings post does in fact work, but
>>>>>>>>>it
>>>>>>>>> feels slightly hacky. If that's an acceptable way to get the
>>>>>>>>>injection
>>>>>>>>> point, then so be it. But just wondering if it's not something
>>>>>>>>>that
>>>>>>>>> works by chance and may break later.
>>>>>>>>>
>>>>>>>>> Kind regards,
>>>>>>>>> Arjan
>>>>>>>>> _______________________________________________
>>>>>>>>> cdi-dev mailing list
>>>>>>>>> [hidden email]
>>>>>>>>> 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.
>>>>>>>>
>>>>>>>>
>>>>
>>>_______________________________________________
>>>cdi-dev mailing list
>>>[hidden email]
>>>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.
>>


_______________________________________________
cdi-dev mailing list
[hidden email]
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.
Reply | Threaded
Open this post in threaded view
|

Re: Getting injection point from Bean#create

arjan tijms
In reply to this post by Jozef Hartinger
Hi

On Thu, Nov 20, 2014 at 9:42 PM, Jozef Hartinger <[hidden email]> 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));

I just tried this again, and unfortunately it now fails on Weld.

I quickly tried a series of Weld versions and this is the result:

Weld
2.2.6 works
2.2.8 works
2.2.9 fails
2.2.13 fails
2.2.14 fails
2.2.16 fails
2.3.0 fails
2.3.2 fails

So apparently it broke between 2.2.8 and 2.2.9.

Would it be possible to add a TCK test for this? This would be really helpful as we'd like to use this for the JSF 2.3 RI.

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
[hidden email]
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.



_______________________________________________
cdi-dev mailing list
[hidden email]
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.
Reply | Threaded
Open this post in threaded view
|

Re: Getting injection point from Bean#create

Tomas Remes

Hi Arjan,

Ok I am going to check this. Wrt TCK I am not sure it's suitable to some spec assertion so I wouldn't add it to TCK but we can definitely include this to Weld internal testsuite.

Thank's

Tom

----- Original Message -----
From: "arjan tijms" <[hidden email]>
To: "Jozef Hartinger" <[hidden email]>
Cc: [hidden email]
Sent: Wednesday, January 27, 2016 2:42:00 PM
Subject: Re: [cdi-dev] Getting injection point from Bean#create

Hi

On Thu, Nov 20, 2014 at 9:42 PM, Jozef Hartinger < [hidden email] > 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));

I just tried this again, and unfortunately it now fails on Weld.

I quickly tried a series of Weld versions and this is the result:

Weld
2.2.6 works
2.2.8 works
2.2.9 fails
2.2.13 fails
2.2.14 fails
2.2.16 fails
2.3.0 fails
2.3.2 fails

So apparently it broke between 2.2.8 and 2.2.9.

Would it be possible to add a TCK test for this? This would be really helpful as we'd like to use this for the JSF 2.3 RI.

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
[hidden email]
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.



_______________________________________________
cdi-dev mailing list
[hidden email]
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.


_______________________________________________
cdi-dev mailing list
[hidden email]
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.
Reply | Threaded
Open this post in threaded view
|

Re: Getting injection point from Bean#create

Martin Kouba
Hi Arjan,

yep, the behavior changed because of fix for WELD-1823 [1]. The spec
(6.5.3. Contextual reference for a bean) requires to inject a null value
if a dependent object is not being injected into any injection point.
I'm not sure we can distinguish these different types of usage of
BeanManager.getReference().

By the way, Tomas tested the original way using
BeanManager.getInjectableReference() and it works fine. For the purpose
of this use case a custom IP with InjectionPoint as a requested type,
@Default as a qualifier and "this" as a declaring bean should be enough
(no need for InjectionPointGenerator).

We're going to add a test in the Weld test suite. I don't think we
should add this into TCK as it's rather corner case not clearly defined
in the spec.

Martin

[1]
https://issues.jboss.org/browse/WELD-1823

Dne 27.1.2016 v 15:20 Tomas Remes napsal(a):

>
> Hi Arjan,
>
> Ok I am going to check this. Wrt TCK I am not sure it's suitable to some spec assertion so I wouldn't add it to TCK but we can definitely include this to Weld internal testsuite.
>
> Thank's
>
> Tom
>
> ----- Original Message -----
> From: "arjan tijms" <[hidden email]>
> To: "Jozef Hartinger" <[hidden email]>
> Cc: [hidden email]
> Sent: Wednesday, January 27, 2016 2:42:00 PM
> Subject: Re: [cdi-dev] Getting injection point from Bean#create
>
> Hi
>
> On Thu, Nov 20, 2014 at 9:42 PM, Jozef Hartinger < [hidden email] > 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));
>
> I just tried this again, and unfortunately it now fails on Weld.
>
> I quickly tried a series of Weld versions and this is the result:
>
> Weld
> 2.2.6 works
> 2.2.8 works
> 2.2.9 fails
> 2.2.13 fails
> 2.2.14 fails
> 2.2.16 fails
> 2.3.0 fails
> 2.3.2 fails
>
> So apparently it broke between 2.2.8 and 2.2.9.
>
> Would it be possible to add a TCK test for this? This would be really helpful as we'd like to use this for the JSF 2.3 RI.
>
> 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
> [hidden email]
> 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.
>
>
>
> _______________________________________________
> cdi-dev mailing list
> [hidden email]
> 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.
>
>
> _______________________________________________
> cdi-dev mailing list
> [hidden email]
> 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.
>

--
Martin Kouba
Software Engineer
Red Hat, Czech Republic
_______________________________________________
cdi-dev mailing list
[hidden email]
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.
Reply | Threaded
Open this post in threaded view
|

Re: Getting injection point from Bean#create

arjan tijms
On Thu, Jan 28, 2016 at 4:43 PM, Martin Kouba <[hidden email]> wrote:
By the way, Tomas tested the original way using BeanManager.getInjectableReference() and it works fine. For the purpose of this use case a custom IP with InjectionPoint as a requested type, @Default as a qualifier and "this" as a declaring bean should be enough (no need for InjectionPointGenerator).

Okay, so you mean this code:

InjectionPoint injectionPoint = (InjectionPoint) beanManager.getInjectableReference(
     resolve(beanManager, InjectionPointGenerator.class).getInjectionPoints().iterator().next(),
     creationalContext
);

To what does that has to be changed exactly?

 

We're going to add a test in the Weld test suite.

Great! That test will be for this method?

Bean<InjectionPoint> bean = (Bean<InjectionPoint>) manager.resolve(manager.getBeans(InjectionPoint.class));
InjectionPoint ip = (InjectionPoint) manager.getReference(bean, InjectionPoint.class, manager.createCreationalContext(bean));

 
I don't think we should add this into TCK as it's rather corner case not clearly defined in the spec.

Would that be something that's beneficial to add to the 2.0 spec?

Kind regards,
Arjan Tijms



 

Martin

[1]
https://issues.jboss.org/browse/WELD-1823

Dne 27.1.2016 v 15:20 Tomas Remes napsal(a):


Hi Arjan,

Ok I am going to check this. Wrt TCK I am not sure it's suitable to some spec assertion so I wouldn't add it to TCK but we can definitely include this to Weld internal testsuite.

Thank's

Tom

----- Original Message -----
From: "arjan tijms" <[hidden email]>
To: "Jozef Hartinger" <[hidden email]>
Cc: [hidden email]
Sent: Wednesday, January 27, 2016 2:42:00 PM
Subject: Re: [cdi-dev] Getting injection point from Bean#create

Hi

On Thu, Nov 20, 2014 at 9:42 PM, Jozef Hartinger < [hidden email] > 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));

I just tried this again, and unfortunately it now fails on Weld.

I quickly tried a series of Weld versions and this is the result:

Weld
2.2.6 works
2.2.8 works
2.2.9 fails
2.2.13 fails
2.2.14 fails
2.2.16 fails
2.3.0 fails
2.3.2 fails

So apparently it broke between 2.2.8 and 2.2.9.

Would it be possible to add a TCK test for this? This would be really helpful as we'd like to use this for the JSF 2.3 RI.

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
[hidden email]
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.



_______________________________________________
cdi-dev mailing list
[hidden email]
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.


_______________________________________________
cdi-dev mailing list
[hidden email]
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.


--
Martin Kouba
Software Engineer
Red Hat, Czech Republic


_______________________________________________
cdi-dev mailing list
[hidden email]
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.
12