PicoContainer
  1. PicoContainer
  2. PICO-343

CollectionComponentParameter works differently with generic Collection types

    Details

    • Type: Bug Bug
    • Status: Closed Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 2.6
    • Fix Version/s: 2.7
    • Component/s: PicoContainer (Java)
    • Labels:
      None
    • Number of attachments :
      0

      Description

      Has the behavior of CollectionComponentParameter changed now that Pico 2
      recognizes generic collection parameter types?

      In the example below, I expected that adding a CollectionComponentParameter
      with a componentValueType of Cod.class would only have Cod fish in the bowl,
      but I observe that it gets both Cod and Shark.

      If I repeat the experiment by making the Bowl have a type-erased List, the
      test works as I expect – I only get a Cod.

      Is this the expected behavior for typed collections?

      Test below:

        public void testRegisteringSubsetOfGenericCollectionParameters() {
           DefaultPicoContainer pico = new DefaultPicoContainer();
           pico.addComponent(Cod.class);
           pico.addComponent(Shark.class);
           pico.addComponent(GenericBowl.class, GenericBowl.class,
                   new CollectionComponentParameter(Cod.class, false));
      
      
           GenericBowl bowl = pico.getComponent(GenericBowl.class);
           // FAILS with PicoContainer 2.7-SNAPSHOT, returns 2
           assertEquals(1, bowl.fishes.size());
        }
      
        // Types
      
        public static interface Fish {
        }
      
        public static class Cod implements Fish {
        }
      
        public static class Shark implements Fish {
        }
      
        public static class GenericBowl {
           List<Fish> fishes;
      
           public GenericBowl(List<Fish> fishes) {
              this.fishes = fishes;
           }
        }
      
      

      ... but this one passes:

        public void testRegisteringSubsetOfTypeErasedCollectionParameters() {
           DefaultPicoContainer pico = new DefaultPicoContainer();
           pico.addComponent(Cod.class);
           pico.addComponent(Shark.class);
           pico.addComponent(GenericBowl.class, GenericBowl.class,
                   new CollectionComponentParameter(Fish.class, false));
      
      
           GenericBowl bowl = pico.getComponent(GenericBowl.class);
           assertEquals(2, bowl.fishes.size());
           assertTrue(CollectionUtilities.contains(bowl.fishes, new
      ContainsInstanceOfTypePredicate(Cod.class)));
           assertTrue(CollectionUtilities.contains(bowl.fishes, new
      ContainsInstanceOfTypePredicate(Shark.class)));
        }
      
      
        public static class TypeErasedListBowl {
           List fishes;
      
           public TypeErasedListBowl(List fishes) {
              this.fishes = fishes;
           }
        }
      

        People

        • Assignee:
          Paul Hammant
          Reporter:
          Paul Hammant
        • Votes:
          0 Vote for this issue
          Watchers:
          0 Start watching this issue

          Dates

          • Created:
            Updated:
            Resolved: