Skip to content

Define how InjectionPoint works when a bean is obtained via CDI.current() #779

@Azquelt

Description

@Azquelt

The InjectionPoint Javadoc says this:

Provides access to metadata about an injection point. May represent an injected field or a parameter of a bean constructor, initializer method, producer method, disposer method or observer method.

If the injection point is a dynamically selected reference obtained then the metadata obtain reflects the injection point of the Instance, with the required type and any additional required qualifiers defined by Instance.select().

This doesn't cover the case where a bean is looked up using CDI.current().select(...).get() because there is no injection point for the Instance.

For example if I have a qualifier:

@Qualifier
@Retention(RUNTIME)
@Target({ TYPE, FIELD, METHOD, PARAMETER, ANNOTATION_TYPE })
public interface @MyQualifier {
    @Nonbinding String value()
    public class MyQualifierLiteral ...
}

and a managed bean class:

@Dependent
@MyQualifier("")
public class MyBean {

    @Inject
    InjectionPoint ip;

    public String getQualifierValue() {
        return ip.getQualifiers().stream()
                        .filter(a -> a.annotationType() == MyQualifier.class)
                        .map(MyQualifier.class::cast)
                        .map(MyQualifier::value)
                        .findFirst()
                        .orElseThrow(() -> new IllegalStateException("No qualifier found"));
    }
}

I can inject this bean like this and get its qualifiers:

@Inject @MyQualifier("foo") private MyBean fooBean;
@Inject @MyQualifier("bar") private Instance<MyBean> barBeanInstance;
@Inject @Any private Instance<Object> objectInstance;

void test() {
    assert fooBean.getQualifierValue().equals("foo");
    assert barBeanInstance.get().getQualifierValue().equals("bar");
    assert objectInstance.select(MyBean.class, new MyQualifier.MyQualifierLiteral("baz")).get().getQualifierValue().equals("baz");
}

However, I don't think it's defined what happens when I do this:

void test() {
    MyBean bean = CDI.current().select(MyBean.class, new MyQualifier.MyQualifierLiteral("qux")).get();
    assert bean.getQualifierValue().equals("qux");
}

Would it make sense to define how the InjectionPoint should work here? I don't think there's any sensible value for getAnnotated(), getBean() or getMember(), but getQualifiers() and getType() would still be helpful in this scenario.

This came to my attention because the Jakarta Batch spec gave an example of looking up a bean like this but getting the injection point isn't supported on open web beans for a bean obtained via CDI.select(): jakartaee/batch-tck#69

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions