Program: 65569
Project: 65569
Buggy source:
@Override public void writeFromObjectIntoRow(Object object,AbstractRecord row,AbstractSession session,WriteType writeType) throws DescriptorException {
  if (this.isReadOnly()) {
    return;
  }
  DOMRecord record=(DOMRecord)row;
  Object attributeValue=this.getAttributeValueFromObject(object);
  Node root=record.getDOM();
  org.w3c.dom.Document doc=record.getDocument();
  XMLField xmlRootField=null;
  if (attributeValue == null) {
    return;
  }
  if (field != null) {
    root=XPathEngine.getInstance().create((XMLField)getField(),record.getDOM(),session);
    DOMRecord rootRecord=new DOMRecord(root);
    rootRecord.setSession(session);
    rootRecord.setDocPresPolicy(record.getDocPresPolicy());
    rootRecord.setNamespaceResolver(record.getNamespaceResolver());
    rootRecord.setMarshaller(record.getMarshaller());
    record=rootRecord;
  }
  ContainerPolicy cp=this.getContainerPolicy();
  ArrayList childNodes=getUnmappedChildNodes(record.getDOM().getChildNodes());
  Object iter=cp.iteratorFor(attributeValue);
  int childNodeCount=0;
  boolean wasXMLRoot=false;
  if (null != iter) {
    while (cp.hasNext(iter) && (childNodeCount < childNodes.size())) {
      Object element=cp.next(iter,session);
      if (this.getConverter() != null) {
        element=getConverter().convertObjectValueToDataValue(element,session,record.getMarshaller());
      }
      Object originalObject=element;
      Node nextChild=null;
      while (childNodeCount < childNodes.size()) {
        Node nextPossible=(Node)childNodes.get(childNodeCount);
        if ((nextPossible.getNodeType() == Node.ELEMENT_NODE) || (nextPossible.getNodeType() == Node.TEXT_NODE) || (nextPossible.getNodeType() == Node.CDATA_SECTION_NODE)) {
          nextChild=nextPossible;
          childNodeCount++;
          break;
        }
        childNodeCount++;
      }
      if (nextChild == null) {
        break;
      }
      if (usesXMLRoot() && (element instanceof XMLRoot)) {
        xmlRootField=new XMLField();
        wasXMLRoot=true;
        XPathFragment frag=new XPathFragment();
        if ((((XMLRoot)element)).getRootFragment().getNamespaceURI() != null) {
          frag.setNamespaceURI(((XMLRoot)element).getNamespaceURI());
        }
 else {
          frag.setXPath(((XMLRoot)element).getLocalName());
        }
        xmlRootField.setXPathFragment(frag);
        xmlRootField.setNamespaceResolver(record.getNamespaceResolver());
        element=((XMLRoot)element).getObject();
      }
      if (element instanceof String) {
        if (wasXMLRoot) {
          if (((XMLRoot)originalObject).getRootFragment().getNamespaceURI() != null) {
            String prefix=record.getNamespaceResolver().resolveNamespaceURI(((XMLRoot)originalObject).getRootFragment().getNamespaceURI());
            if ((prefix == null) || prefix.length() == 0) {
              xmlRootField.getXPathFragment().setGeneratedPrefix(true);
              prefix=record.getNamespaceResolver().generatePrefix();
            }
            xmlRootField.getXPathFragment().setXPath(prefix + XMLConstants.COLON + ((XMLRoot)originalObject).getLocalName());
          }
        }
        if (xmlRootField != null) {
          XPathEngine.getInstance().create(xmlRootField,root,element,session);
        }
 else {
          Text textNode=doc.createTextNode((String)element);
          root.replaceChild(textNode,nextChild);
        }
      }
 else       if (element instanceof org.w3c.dom.Node) {
        Node importedCopy=doc.importNode((Node)element,true);
        root.replaceChild(importedCopy,nextChild);
      }
 else {
        XMLDescriptor referenceDescriptor=(XMLDescriptor)session.getDescriptor(element.getClass());
        if (wasXMLRoot) {
          if (((XMLRoot)originalObject).getRootFragment().getNamespaceURI() != null) {
            String prefix=referenceDescriptor.getNonNullNamespaceResolver().resolveNamespaceURI(((XMLRoot)originalObject).getNamespaceURI());
            if ((prefix == null) || prefix.length() == 0) {
              prefix=record.getNamespaceResolver().resolveNamespaceURI(((XMLRoot)originalObject).getRootFragment().getNamespaceURI());
            }
            if ((prefix == null) || prefix.length() == 0) {
              xmlRootField.getXPathFragment().setGeneratedPrefix(true);
              prefix=record.getNamespaceResolver().generatePrefix();
            }
            xmlRootField.getXPathFragment().setXPath(prefix + XMLConstants.COLON + ((XMLRoot)originalObject).getLocalName());
          }
        }
        DOMRecord nestedRecord=(DOMRecord)buildCompositeRow(element,session,referenceDescriptor,row,xmlRootField,element,wasXMLRoot);
        if (nestedRecord != null) {
          if (nestedRecord.getDOM() != nextChild) {
            root.replaceChild(nestedRecord.getDOM(),nextChild);
          }
        }
      }
    }
  }
  if (childNodeCount < childNodes.size()) {
    for (int i=childNodeCount; i < childNodes.size(); i++) {
      Node child=(Node)childNodes.get(i);
      if ((child.getNodeType() == Node.ELEMENT_NODE) || (child.getNodeType() == Node.TEXT_NODE) || (child.getNodeType() == Node.CDATA_SECTION_NODE)) {
        root.removeChild(child);
      }
    }
  }
  if (null != iter && cp.hasNext(iter)) {
    while (cp.hasNext(iter)) {
      Object element=cp.next(iter,session);
      writeSingleValue(element,object,record,session);
    }
  }
}

Fixed version (ground truth):
@Override public void writeFromObjectIntoRow(Object object,AbstractRecord row,AbstractSession session,WriteType writeType) throws DescriptorException {
  if (this.isReadOnly()) {
    return;
  }
  DOMRecord record=(DOMRecord)row;
  Object attributeValue=this.getAttributeValueFromObject(object);
  Node root=record.getDOM();
  org.w3c.dom.Document doc=record.getDocument();
  XMLField xmlRootField=null;
  if (attributeValue == null) {
    return;
  }
  if (field != null) {
    root=XPathEngine.getInstance().create((XMLField)getField(),record.getDOM(),session);
    DOMRecord rootRecord=new DOMRecord(root);
    rootRecord.setSession(session);
    rootRecord.setDocPresPolicy(record.getDocPresPolicy());
    rootRecord.setNamespaceResolver(record.getNamespaceResolver());
    rootRecord.setMarshaller(record.getMarshaller());
    record=rootRecord;
  }
  ContainerPolicy cp=this.getContainerPolicy();
  ArrayList childNodes=getUnmappedChildNodes(record.getDOM().getChildNodes());
  Object iter=cp.iteratorFor(attributeValue);
  int childNodeCount=0;
  boolean wasXMLRoot=false;
  if (null != iter) {
    while (cp.hasNext(iter) && (childNodeCount < childNodes.size())) {
      Object element=cp.next(iter,session);
      if (this.getConverter() != null) {
        element=getConverter().convertObjectValueToDataValue(element,session,record.getMarshaller());
      }
      Object originalObject=element;
      Node nextChild=null;
      while (childNodeCount < childNodes.size()) {
        Node nextPossible=(Node)childNodes.get(childNodeCount);
        if ((nextPossible.getNodeType() == Node.ELEMENT_NODE) || (nextPossible.getNodeType() == Node.TEXT_NODE) || (nextPossible.getNodeType() == Node.CDATA_SECTION_NODE)) {
          nextChild=nextPossible;
          childNodeCount++;
          break;
        }
        childNodeCount++;
      }
      if (nextChild == null) {
        break;
      }
      if (usesXMLRoot() && (element instanceof XMLRoot)) {
        xmlRootField=new XMLField();
        wasXMLRoot=true;
        XPathFragment frag=new XPathFragment();
        if ((((XMLRoot)element)).getNamespaceURI() != null) {
          frag.setNamespaceURI(((XMLRoot)element).getNamespaceURI());
        }
        frag.setXPath(((XMLRoot)element).getLocalName());
        xmlRootField.setXPathFragment(frag);
        xmlRootField.setNamespaceResolver(record.getNamespaceResolver());
        element=((XMLRoot)element).getObject();
      }
      if (element instanceof String) {
        if (wasXMLRoot) {
          if (((XMLRoot)originalObject).getNamespaceURI() != null) {
            String prefix=record.getNamespaceResolver().resolveNamespaceURI(((XMLRoot)originalObject).getNamespaceURI());
            if ((prefix == null) || prefix.length() == 0) {
              xmlRootField.getXPathFragment().setGeneratedPrefix(true);
              prefix=record.getNamespaceResolver().generatePrefix();
            }
            xmlRootField.getXPathFragment().setXPath(prefix + XMLConstants.COLON + ((XMLRoot)originalObject).getLocalName());
          }
        }
        if (xmlRootField != null) {
          XPathEngine.getInstance().create(xmlRootField,root,element,session);
        }
 else {
          Text textNode=doc.createTextNode((String)element);
          root.replaceChild(textNode,nextChild);
        }
      }
 else       if (element instanceof org.w3c.dom.Node) {
        Node importedCopy=doc.importNode((Node)element,true);
        root.replaceChild(importedCopy,nextChild);
      }
 else {
        XMLDescriptor referenceDescriptor=(XMLDescriptor)session.getDescriptor(element.getClass());
        if (wasXMLRoot) {
          if (((XMLRoot)originalObject).getNamespaceURI() != null) {
            String prefix=referenceDescriptor.getNonNullNamespaceResolver().resolveNamespaceURI(((XMLRoot)originalObject).getNamespaceURI());
            if ((prefix == null) || prefix.length() == 0) {
              prefix=record.getNamespaceResolver().resolveNamespaceURI(((XMLRoot)originalObject).getNamespaceURI());
            }
            if ((prefix == null) || prefix.length() == 0) {
              xmlRootField.getXPathFragment().setGeneratedPrefix(true);
              prefix=record.getNamespaceResolver().generatePrefix();
            }
            xmlRootField.getXPathFragment().setXPath(prefix + XMLConstants.COLON + ((XMLRoot)originalObject).getLocalName());
          }
        }
        DOMRecord nestedRecord=(DOMRecord)buildCompositeRow(element,session,referenceDescriptor,row,xmlRootField,element,wasXMLRoot);
        if (nestedRecord != null) {
          if (nestedRecord.getDOM() != nextChild) {
            root.replaceChild(nestedRecord.getDOM(),nextChild);
          }
        }
      }
    }
  }
  if (childNodeCount < childNodes.size()) {
    for (int i=childNodeCount; i < childNodes.size(); i++) {
      Node child=(Node)childNodes.get(i);
      if ((child.getNodeType() == Node.ELEMENT_NODE) || (child.getNodeType() == Node.TEXT_NODE) || (child.getNodeType() == Node.CDATA_SECTION_NODE)) {
        root.removeChild(child);
      }
    }
  }
  if (null != iter && cp.hasNext(iter)) {
    while (cp.hasNext(iter)) {
      Object element=cp.next(iter,session);
      writeSingleValue(element,object,record,session);
    }
  }
}