[cdi-dev] [JBoss JIRA] (CDI-723) Obtaining 'original' type/instance of alternatives

Martin Kouba (JIRA) issues at jboss.org
Tue Feb 27 08:53:00 EST 2018

    [ https://issues.jboss.org/browse/CDI-723?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13538869#comment-13538869 ] 

Martin Kouba commented on CDI-723:

Just to ilustrate the proposed solution based on {{WeldInstance}}:
FooFace produce(WeldInstance<FooFace> instance) {
  // ambiguousHandlersStream() method does not exist yet - note that by default, Instance<> resolves ambiguities automatically
  // Filters out alternative beans and returns FooImpl instance
  return instance.ambiguousHandlersStream().filter(h -> !h.getBean().isAlternative()).map(h -> h.get()).findAny().get();

> Obtaining 'original' type/instance of alternatives
> --------------------------------------------------
>                 Key: CDI-723
>                 URL: https://issues.jboss.org/browse/CDI-723
>             Project: CDI Specification Issues
>          Issue Type: Feature Request
>    Affects Versions: 2.0 .Final
>            Reporter: Matej Novotny
> This issue is based on [CDI mailing list discussion|http://lists.jboss.org/pipermail/cdi-dev/2018-February/010088.html].
> The request is, given that there is one (or more) alternative(s) in place, to be able to obtain an instance of the original bean.
> E.g. given that I have:
> {code}
> public interface FooFace{...} // interface
> @ApplicationScoped
> public class FooImpl{...} // 'original' impl
> @ApplicationScoped
> @Alternative
> @Priority(1)
> public class AltFooImpl{...} // alternative impl
> {code}
> I want to be able to put my hands on {{FooImpl}} instance.
> The use case is to be able to obtain certain data from it, or simply build on top of it and it concerns both, producers and "classic" beans defined as classes and overriden via alternatives.
> As for implementation, this could be done on the {{Instance}} level, given an additional structure is added. What would be needed is a way to say that you want to resolve all beans of given type, but without throwing away disabled alternatives. Then you want to allow user to browse through those beans (with access to metadata - {{Bean<?>}}. With that in place, user could then browse all the beans and choose which one to instantiate.
> Following is an idea of how this could be done using structures have in Weld:
> For instance, in Weld this would be possible using our internal structure, called {{Handler}}, which allows to browse bean metadata. From {{WeldInstance}} (enriched version of {{Instance}}), you can call {{getHandler()}} to obtain the object. With this in place we could pretty easily allows user to get all the beans with given type, inspect them, and then instantiate whichever is needed.

This message was sent by Atlassian JIRA

More information about the cdi-dev mailing list