-
Notifications
You must be signed in to change notification settings - Fork 78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add API to obtain current injection point from Bean#create #637
Comments
p.s. a workaround is now: @Dependent
public class InjectionPointGenerator {
// TODO: this is a workaround originally for older OWB versions, but while OWB is fixed, newer Weld
// versions are now broken. It seems this needs to be fixed in CDI 2.1, or perhaps CDI 3.
// See https://issues.jboss.org/browse/CDI-610
@Inject
private InjectionPoint injectionPoint;
public InjectionPoint getInjectionPoint() {
return injectionPoint;
}
} public static InjectionPoint getCurrentInjectionPoint(BeanManager beanManager, CreationalContext<?> creationalContext) {
Bean<InjectionPointGenerator> bean = resolve(beanManager, InjectionPointGenerator.class);
return bean != null
? (InjectionPoint) beanManager.getInjectableReference(bean.getInjectionPoints().iterator().next(), creationalContext)
: null;
}
@SuppressWarnings("unchecked")
public static <T> Bean<T> resolve(BeanManager beanManager, Class<T> beanClass, Annotation... qualifiers) {
Set<Bean<?>> beans = beanManager.getBeans(beanClass, qualifiers);
for (Bean<?> bean : beans) {
if (bean.getBeanClass() == beanClass) {
return (Bean<T>) beanManager.resolve(Collections.<Bean<?>>singleton(bean));
}
}
Bean<T> bean = (Bean<T>) beanManager.resolve(beans);
if (bean == null && beanClass.getSuperclass() != Object.class) {
return (Bean<T>) resolve(beanManager, beanClass.getSuperclass(), qualifiers);
} else {
return bean;
}
} |
I agree with @mkouba's comments in the original issue -- the existing CDI API for programmatically looking up
I'd say that's probably a bug, but I'll let @manovotn comment on that :-) |
That sounds like a bug and I am not aware of any issue tracking that. As for this issue, I am also of the opinion that you should be able to use programmatic lookup to obtain |
As per the existing issue https://issues.redhat.com/browse/CDI-610
There's currently not a clear way on how to obtain the current injection point (if any) from
Bean<T>#create
.A previously somewhat accepted way (though not specified) was:
This however broke in some version of Weld.
Since getting the injection point is an often used feature in producers, I'd like to propose to introduce an easy API for this, so
Bean<T>
implementations can use this just as easily. E.g. something like:BeanManager#getCurrentInjectionPoint()
.Also see: http://cdi-development-mailing-list.1064426.n5.nabble.com/Getting-injection-point-from-Bean-create-td5710505i20.html
The text was updated successfully, but these errors were encountered: