Uploaded image for project: 'CDI Specification Issues'
  1. CDI Specification Issues
  2. CDI-331

Instance.iterator() shouldn't include non-alternatives that haven't been replaced

      The specification currently says that:

      The iterator() method must:
      • Identify the set of beans that have the required type and required qualifiers and are eligible for injection into the class into
      which the parent Instance was injected, according to the rules of typesafe resolution, as defined in Section 5.2.1.

      The ambiguity resolution algorithm as defined in 5.2.2 should be applied.

      This is explicitly required for get() but is not required for iterator(). This causes the following inconsistency:

      Assume two classes Foo and Bar which both implement common interface Common. Bar is an enabled alternative.

      Assume further the following

      @Inject @Any Instance<Common> instance
      

      injection point.

      It is clear that instance.get() returns Bar.

      It is however not clear whether instance.iterator() iterates over:
      a) Bar
      b) Foo, Bar

      and whether instance.isAmbiguous() returns:
      a) true
      b) false

            [CDI-331] Instance.iterator() shouldn't include non-alternatives that haven't been replaced

            Closing all resolved issues in CDI 1.x

            Antoine Sabot-Durand (Inactive) added a comment - Closing all resolved issues in CDI 1.x

            This code snippet illustrates the problem:

            Instance<Foo> = ...
            
            if (!instance.isAmbiguous()) {
                instance.get(); // do something with it
            } else {
                // choose the appropriate instance
            }
            

            The intuitive code snippet above is wrong. You cannot effectively use isAmbiguous() to reflect whether there are multiple matching instances.

            For example if there were two matching @Alternative beans, each with a different priority, isAmbiguous() would still return true even though instance.get() is capable of returning the appropriate instance (the alternative with the highest priority).

            Jozef Hartinger added a comment - This code snippet illustrates the problem: Instance<Foo> = ... if (!instance.isAmbiguous()) { instance.get(); // do something with it } else { // choose the appropriate instance } The intuitive code snippet above is wrong. You cannot effectively use isAmbiguous() to reflect whether there are multiple matching instances. For example if there were two matching @Alternative beans, each with a different priority, isAmbiguous() would still return true even though instance.get() is capable of returning the appropriate instance (the alternative with the highest priority).

              asabotdu@redhat.com Antoine Sabot-Durand (Inactive)
              rhn-engineering-jharting Jozef Hartinger
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

                Created:
                Updated:
                Resolved: