[cdi-dev] Easy way to get 'original' type from alternative producers

arjan tijms arjan.tijms at gmail.com
Tue Feb 13 07:13:54 EST 2018


Hi,

When writing an alternative producer, e.g.

@Alternative
@Priority(500)
@ApplicationScoped
public class ApplicationInit {

    @Produces
    public HttpAuthenticationMechanism produce(BeanManager beanManager) {
        return ...
    }
}

You not rarely need the bean the producer is going to be an alternative
for. For instance to wrap it, or otherwise augment it, or perhaps to take a
few values from.

In order to get that bean, a bunch of quite verbose code is needed. I.e I
came up with:

HttpAuthenticationMechanism mechanism =
    createRef(
        beanManager.resolve(
            beanManager.getBeans(HttpAuthenticationMechanism.class)
                       .stream()
                       .filter(e ->
!e.getBeanClass().equals(ApplicationInit.class))
                       .collect(toSet())),
            beanManager);


And:

HttpAuthenticationMechanism createRef(Bean<?> bean, BeanManager
beanManager) {
    return (HttpAuthenticationMechanism)
        beanManager.getReference(
            bean,
            HttpAuthenticationMechanism.class,
            beanManager.createCreationalContext(bean));
}

I wonder if it would not be a good idea to introduce something to get that
original bean more easily, i.e. just like a decorator and @Delegate.

E.g.

@Alternative
@Priority(500)
@ApplicationScoped
public class ApplicationInit {

    @Produces
    public HttpAuthenticationMechanism produce(BeanManager beanManager,
HttpAuthenticationMechanism original) {
        return ...
    }
}

Or reuse the @Delegate (perhaps with the @Alternative annotation)

@Alternative
@Priority(500)
@ApplicationScoped
public class ApplicationInit {

    @Inject
    @Delegate
    @Alternative (?)
    private HttpAuthenticationMechanism original;

    @Produces
    public HttpAuthenticationMechanism produce(BeanManager beanManager) {
        return ...
    }
}

Thoughts?

Kind regards,
Arjan Tijms
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/cdi-dev/attachments/20180213/4b217bfc/attachment.html 


More information about the cdi-dev mailing list