Program: 64825
Project: 64825
Buggy source:
/** 
 * Add a new <code>element</code> to the <code>parent</code> element.  If an element with this name already exists, return it (unless <code>forceCreate</code> is <code>true</code>).
 * @param fragment Name of element to create
 * @param namespace namespace of element to create
 * @param parent Parent element
 * @param schemaType schemaType of element to create
 * @param value Value for the new node
 * @param forceCreate If true, create a new element even if one with the same name currently exists
 * @return The <code>NodeList</code> that was created/found
 */
private NodeList addElements(XPathFragment fragment,XMLField xmlField,Node parent,Object value,boolean forceCreate,Element sibling,DocumentPreservationPolicy docPresPolicy,AbstractSession session){
  if (!forceCreate) {
    NodeList nodes=unmarshalXPathEngine.selectElementNodes(parent,fragment,getNamespaceResolverForField(xmlField));
    if (nodes.getLength() > 0) {
      return nodes;
    }
  }
  XMLNodeList elementsToReturn=new XMLNodeList();
  if (value == this) {
    String namespace=resolveNamespacePrefix(fragment,getNamespaceResolverForField(xmlField));
    Element newElement=parent.getOwnerDocument().createElementNS(namespace,fragment.getShortName());
    XPathPredicate predicate=fragment.getPredicate();
    if (predicate != null) {
      XPathFragment predicateFragment=predicate.getXPathFragment();
      if (predicateFragment.isAttribute()) {
        if (predicateFragment.getNamespaceURI() == null || predicateFragment.getNamespaceURI().length() == 0) {
          newElement.setAttribute(predicateFragment.getLocalName(),fragment.getPredicate().getValue());
        }
 else {
          String name=predicateFragment.getLocalName();
          if (predicateFragment.getPrefix() != null && predicateFragment.getPrefix().length() != 0) {
            name=predicateFragment.getPrefix() + XMLConstants.COLON + name;
          }
          newElement.setAttributeNS(predicateFragment.getNamespaceURI(),name,fragment.getPredicate().getValue());
        }
      }
    }
    elementsToReturn.add(newElement);
    docPresPolicy.getNodeOrderingPolicy().appendNode(parent,newElement,sibling);
  }
 else   if (value == null) {
    elementsToReturn.add(parent);
  }
 else {
    if (value instanceof List) {
      List values=(List)value;
      for (int index=0; index < values.size(); index++) {
        Element newElement=null;
        if (values.get(index) != XMLRecord.NIL) {
          newElement=(Element)createElement(parent,fragment,xmlField,values.get(index),session);
        }
 else {
          newElement=(Element)createElement(parent,fragment,xmlField,XMLConstants.EMPTY_STRING,session);
          newElement.setAttributeNS(XMLConstants.SCHEMA_INSTANCE_URL,XMLConstants.SCHEMA_INSTANCE_PREFIX + XMLConstants.COLON + XMLConstants.SCHEMA_NIL_ATTRIBUTE,XMLConstants.BOOLEAN_STRING_TRUE);
        }
        XPathPredicate predicate=fragment.getPredicate();
        if (predicate != null) {
          XPathFragment predicateFragment=predicate.getXPathFragment();
          if (predicateFragment.isAttribute()) {
            if (predicateFragment.getNamespaceURI() == null || predicateFragment.getNamespaceURI().length() == 0) {
              newElement.setAttribute(predicateFragment.getLocalName(),fragment.getPredicate().getValue());
            }
 else {
              String name=predicateFragment.getLocalName();
              if (predicateFragment.getPrefix() != null && predicateFragment.getPrefix().length() != 0) {
                name=predicateFragment.getPrefix() + XMLConstants.COLON + name;
              }
              newElement.setAttributeNS(predicateFragment.getNamespaceURI(),name,fragment.getPredicate().getValue());
            }
          }
        }
        docPresPolicy.getNodeOrderingPolicy().appendNode(parent,newElement,sibling);
        elementsToReturn.add(newElement);
        sibling=newElement;
      }
    }
 else {
      Element newElement=null;
      if (value != XMLRecord.NIL) {
        newElement=(Element)createElement(parent,fragment,xmlField,value,session);
      }
 else {
        newElement=(Element)createElement(parent,fragment,xmlField,XMLConstants.EMPTY_STRING,session);
        newElement.setAttributeNS(XMLConstants.SCHEMA_INSTANCE_URL,XMLConstants.SCHEMA_INSTANCE_PREFIX + XMLConstants.COLON + XMLConstants.SCHEMA_NIL_ATTRIBUTE,XMLConstants.BOOLEAN_STRING_TRUE);
      }
      XPathPredicate predicate=fragment.getPredicate();
      if (predicate != null) {
        XPathFragment predicateFragment=predicate.getXPathFragment();
        if (predicateFragment.isAttribute()) {
          if (predicateFragment.getNamespaceURI() == null || predicateFragment.getNamespaceURI().length() == 0) {
            newElement.setAttribute(predicateFragment.getLocalName(),fragment.getPredicate().getValue());
          }
 else {
            String name=predicateFragment.getLocalName();
            if (predicateFragment.getPrefix() != null && predicateFragment.getPrefix().length() != 0) {
              name=predicateFragment.getPrefix() + XMLConstants.COLON + name;
            }
            newElement.setAttributeNS(predicateFragment.getNamespaceURI(),name,fragment.getPredicate().getValue());
          }
        }
      }
      docPresPolicy.getNodeOrderingPolicy().appendNode(parent,newElement,sibling);
      elementsToReturn.add(newElement);
    }
  }
  return elementsToReturn;
}

Fixed version (ground truth):
/** 
 * Add a new <code>element</code> to the <code>parent</code> element.  If an element with this name already exists, return it (unless <code>forceCreate</code> is <code>true</code>).
 * @param fragment Name of element to create
 * @param namespace namespace of element to create
 * @param parent Parent element
 * @param schemaType schemaType of element to create
 * @param value Value for the new node
 * @param forceCreate If true, create a new element even if one with the same name currently exists
 * @return The <code>NodeList</code> that was created/found
 */
private NodeList addElements(XPathFragment fragment,XMLField xmlField,Node parent,Object value,boolean forceCreate,Element sibling,DocumentPreservationPolicy docPresPolicy,AbstractSession session){
  if (!forceCreate) {
    NodeList nodes=unmarshalXPathEngine.selectElementNodes(parent,fragment,getNamespaceResolverForField(xmlField));
    if (nodes.getLength() > 0) {
      return nodes;
    }
  }
  XMLNodeList elementsToReturn=new XMLNodeList();
  if (value == this) {
    String namespace=resolveNamespacePrefix(fragment,getNamespaceResolverForField(xmlField));
    Element newElement=parent.getOwnerDocument().createElementNS(namespace,fragment.getShortName());
    XPathPredicate predicate=fragment.getPredicate();
    if (predicate != null) {
      XPathFragment predicateFragment=predicate.getXPathFragment();
      if (predicateFragment.isAttribute()) {
        if (predicateFragment.getNamespaceURI() == null || predicateFragment.getNamespaceURI().length() == 0) {
          newElement.setAttribute(predicateFragment.getLocalName(),fragment.getPredicate().getValue());
        }
 else {
          String name=predicateFragment.getLocalName();
          if (predicateFragment.getPrefix() != null && predicateFragment.getPrefix().length() != 0) {
            name=predicateFragment.getPrefix() + XMLConstants.COLON + name;
          }
          newElement.setAttributeNS(predicateFragment.getNamespaceURI(),name,fragment.getPredicate().getValue());
        }
      }
    }
    elementsToReturn.add(newElement);
    docPresPolicy.getNodeOrderingPolicy().appendNode(parent,newElement,sibling);
  }
 else   if (value == null) {
    elementsToReturn.add(parent);
  }
 else {
    if (value instanceof List) {
      List values=(List)value;
      for (int index=0; index < values.size(); index++) {
        Element newElement=null;
        if (values.get(index) != XMLRecord.NIL) {
          newElement=(Element)createElement(parent,fragment,xmlField,values.get(index),session);
        }
 else {
          newElement=(Element)createElement(parent,fragment,xmlField,XMLConstants.EMPTY_STRING,session);
          addXsiNilToElement(newElement,xmlField);
        }
        XPathPredicate predicate=fragment.getPredicate();
        if (predicate != null) {
          XPathFragment predicateFragment=predicate.getXPathFragment();
          if (predicateFragment.isAttribute()) {
            if (predicateFragment.getNamespaceURI() == null || predicateFragment.getNamespaceURI().length() == 0) {
              newElement.setAttribute(predicateFragment.getLocalName(),fragment.getPredicate().getValue());
            }
 else {
              String name=predicateFragment.getLocalName();
              if (predicateFragment.getPrefix() != null && predicateFragment.getPrefix().length() != 0) {
                name=predicateFragment.getPrefix() + XMLConstants.COLON + name;
              }
              newElement.setAttributeNS(predicateFragment.getNamespaceURI(),name,fragment.getPredicate().getValue());
            }
          }
        }
        docPresPolicy.getNodeOrderingPolicy().appendNode(parent,newElement,sibling);
        elementsToReturn.add(newElement);
        sibling=newElement;
      }
    }
 else {
      Element newElement=null;
      if (value != XMLRecord.NIL) {
        newElement=(Element)createElement(parent,fragment,xmlField,value,session);
      }
 else {
        newElement=(Element)createElement(parent,fragment,xmlField,XMLConstants.EMPTY_STRING,session);
        addXsiNilToElement(newElement,xmlField);
      }
      XPathPredicate predicate=fragment.getPredicate();
      if (predicate != null) {
        XPathFragment predicateFragment=predicate.getXPathFragment();
        if (predicateFragment.isAttribute()) {
          if (predicateFragment.getNamespaceURI() == null || predicateFragment.getNamespaceURI().length() == 0) {
            newElement.setAttribute(predicateFragment.getLocalName(),fragment.getPredicate().getValue());
          }
 else {
            String name=predicateFragment.getLocalName();
            if (predicateFragment.getPrefix() != null && predicateFragment.getPrefix().length() != 0) {
              name=predicateFragment.getPrefix() + XMLConstants.COLON + name;
            }
            newElement.setAttributeNS(predicateFragment.getNamespaceURI(),name,fragment.getPredicate().getValue());
          }
        }
      }
      docPresPolicy.getNodeOrderingPolicy().appendNode(parent,newElement,sibling);
      elementsToReturn.add(newElement);
    }
  }
  return elementsToReturn;
}