Uploaded image for project: 'Groovy'
  1. Groovy
  2. GROOVY-7044

Cloned NodeBuilder created nodes share values (child nodes) with original node.

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • 2.3.6
    • 2.4.8
    • groovy-jdk, XML Processing
    • None

    Description

      Cloning nodes created by NodeBuilder share child nodes with original node.
      If node is added to original node or child node value is changed, the cloned node will see those changes. Same applies to cloned node, adding node or changing child node value will be seen by original node.

      The problem seem to originate in NodeBuilder methods:

      protected Object createNode(Object name)
      protected Object createNode(Object name, Map attributes)
      

      which use ArrayList when creating new Node instead of NodeList.

      Can be easily reproduced by groovysh or use attached junit file.

      groovy:000> x1 = new NodeBuilder().a() { b() }
      ===> a[attributes={}; value=[b[attributes={}; value=[]]]]
      groovy:000> x2 = x1.clone()
      ===> a[attributes={}; value=[b[attributes={}; value=[]]]]
      groovy:000> x1.appendNode('c')
      ===> c[attributes={}; value=[]]
      groovy:000> x1
      ===> a[attributes={}; value=[b[attributes={}; value=[]], c[attributes={}; value=[]]]]
      groovy:000> x2
      ===> a[attributes={}; value=[b[attributes={}; value=[]], c[attributes={}; value=[]]]]
      groovy:000> x1.b[0].setValue(1)
      ===> null
      groovy:000> x1
      ===> a[attributes={}; value=[b[attributes={}; value=1], c[attributes={}; value=[]]]]
      groovy:000> x2
      ===> a[attributes={}; value=[b[attributes={}; value=1], c[attributes={}; value=[]]]]
      groovy:000> x2.c[0].setValue(2)
      ===> null
      groovy:000> x1
      ===> a[attributes={}; value=[b[attributes={}; value=1], c[attributes={}; value=2]]]
      

      Attachments

        1. NodeBuilderTest.groovy
          0.3 kB
          Damir Perovi?

        Issue Links

          Activity

            People

              shils Shil Sinha
              cvrcak Damir Perovi?
              Votes:
              1 Vote for this issue
              Watchers:
              6 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: