NanoContainer
  1. NanoContainer
  2. NANO-180

Can't remove a container built with makeChildContainer()

    Details

    • Type: Bug Bug
    • Status: Closed Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 1.1
    • Component/s: core
    • Labels:
      None
    • Testcase included:
      yes
    • Number of attachments :
      2

      Description

      Here's the test case that fails:

      (Specific test run is: DefaultNanoPicoContainerTestCase)

      org.picocontainer.tck.AbstractPicoContainerTestCase:

      public void testMakeRemoveChildContainer()

      { final NanoPicoContainer parent = (NanoPicoContainer) createPicoContainer(null); parent.registerComponentInstance("java.lang.String", "This is a test"); MutablePicoContainer pico = parent.makeChildContainer(); //Verify they are indeed wired together. assertNotNull(pico.getComponentInstance("java.lang.String")); boolean result = parent.removeChildContainer(pico); assertTrue(result); }

      I'm posting the concept here because after wracking my brains out, the BEST solution I could come up with was modify a few methods in AbstractNanoPicoContainer to have if/thens like this:

      public boolean removeChildContainer(PicoContainer child) {
      boolean result;
      if (child instanceof AbstractNanoPicoContainer)

      { result = getDelegate().removeChildContainer(((AbstractNanoPicoContainer)child).getDelegate()); }

      else

      { result = getDelegate().removeChildContainer(child); }

      //.....
      }

      (Full details in the patch).

      To me, this fix is u g l y. But I couldn't think of any other way to pull it off without changing the API.

      Anybody have any thoughts?

      Thanks!

      -Mike (R)

      1. nano-mkchildcontainer.patch
        2 kB
        Michael Rimov
      2. removeChildContainer.patch
        7 kB
        Jörg Schaible

        Activity

        Hide
        Jörg Schaible added a comment -

        Can you try the following patch? Basic idea is, that the registered child container in the MPC is replaced with the DNPC itself.

        Index: C:/Work/Apps/Codehaus/pico/java/nanocontainer/container/src/java/org/nanocontainer/reflection/DefaultNanoPicoContainer.java
        ===================================================================
        --- C:/Work/Apps/Codehaus/pico/java/nanocontainer/container/src/java/org/nanocontainer/reflection/DefaultNanoPicoContainer.java	(revision 2961)
        +++ C:/Work/Apps/Codehaus/pico/java/nanocontainer/container/src/java/org/nanocontainer/reflection/DefaultNanoPicoContainer.java	(working copy)
        @@ -94,6 +94,9 @@
              */
             protected DefaultNanoPicoContainer(final DefaultNanoPicoContainer parent) {
                 super(parent.getDelegate().makeChildContainer(),  parent.getComponentClassLoader());
        +        MutablePicoContainer parentDelegate = parent.getDelegate();
        +        parentDelegate.removeChildContainer(getDelegate());
        +        parentDelegate.addChildContainer(this);
             }
         
             /**
        
        Show
        Jörg Schaible added a comment - Can you try the following patch? Basic idea is, that the registered child container in the MPC is replaced with the DNPC itself. Index: C:/Work/Apps/Codehaus/pico/java/nanocontainer/container/src/java/org/nanocontainer/reflection/DefaultNanoPicoContainer.java =================================================================== --- C:/Work/Apps/Codehaus/pico/java/nanocontainer/container/src/java/org/nanocontainer/reflection/DefaultNanoPicoContainer.java (revision 2961) +++ C:/Work/Apps/Codehaus/pico/java/nanocontainer/container/src/java/org/nanocontainer/reflection/DefaultNanoPicoContainer.java (working copy) @@ -94,6 +94,9 @@ */ protected DefaultNanoPicoContainer(final DefaultNanoPicoContainer parent) { super(parent.getDelegate().makeChildContainer(), parent.getComponentClassLoader()); + MutablePicoContainer parentDelegate = parent.getDelegate(); + parentDelegate.removeChildContainer(getDelegate()); + parentDelegate.addChildContainer(this); } /**
        Hide
        Michael Rimov added a comment -

        Jörg,

        It does pass the test cases, but I haven't replied up until now because so far I haven't figured out WHY your patch doesn't also see the original test case fail for anything deriving from AbstractNanoPicoContainer. (ex: ImplementationHidingNanoPicoContainer).

        I haven't stepped through it enough to understand, but I really appreciate your feedback. I'll continue to chew on it this week.

        -Mike (R)

        Show
        Michael Rimov added a comment - Jörg, It does pass the test cases, but I haven't replied up until now because so far I haven't figured out WHY your patch doesn't also see the original test case fail for anything deriving from AbstractNanoPicoContainer. (ex: ImplementationHidingNanoPicoContainer). I haven't stepped through it enough to understand, but I really appreciate your feedback. I'll continue to chew on it this week. -Mike (R)
        Hide
        Jörg Schaible added a comment -

        You're right, the IHNPC has the same problem. Try this patch, I've refactored the necessary functionality ...

        Show
        Jörg Schaible added a comment - You're right, the IHNPC has the same problem. Try this patch, I've refactored the necessary functionality ...
        Jörg Schaible made changes -
        Field Original Value New Value
        Attachment removeChildContainer.patch [ 20949 ]
        Hide
        Michael Rimov added a comment -

        Thanks Jörg,

        Very clever and clean. Huge improvement over my first patch!

        Show
        Michael Rimov added a comment - Thanks Jörg, Very clever and clean. Huge improvement over my first patch!
        Michael Rimov made changes -
        Resolution Fixed [ 1 ]
        Fix Version/s 1.1 [ 12307 ]
        Status Open [ 1 ] Closed [ 6 ]
        Assignee Michael Rimov [ rimovm ]

          People

          • Assignee:
            Michael Rimov
            Reporter:
            Michael Rimov
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved: