Program: 64377
Project: 64377
Buggy source:
public boolean marshal(XPathFragment xPathFragment,MarshalRecord marshalRecord,Object object,CoreAbstractSession session,NamespaceResolver namespaceResolver){
  if (xmlChoiceCollectionMapping.isReadOnly()) {
    return false;
  }
  Object value=xmlChoiceCollectionMapping.getAttributeValueFromObject(object);
  if (value == null) {
    AbstractNullPolicy wrapperNP=xmlChoiceCollectionMapping.getWrapperNullPolicy();
    if (wrapperNP != null && wrapperNP.getMarshalNullRepresentation() == XMLNullRepresentationType.XSI_NIL) {
      marshalRecord.nilSimple(namespaceResolver);
      return true;
    }
 else {
      return false;
    }
  }
  CoreContainerPolicy cp=getContainerPolicy();
  Object iterator=cp.iteratorFor(value);
  if (null != iterator && cp.hasNext(iterator)) {
    if (xPathFragment != null) {
      XPathFragment groupingFragment=marshalRecord.openStartGroupingElements(namespaceResolver);
      marshalRecord.closeStartGroupingElements(groupingFragment);
    }
  }
 else {
    return marshalRecord.emptyCollection(xPathFragment,namespaceResolver,xmlChoiceCollectionMapping.getWrapperNullPolicy() != null);
  }
  if (marshalRecord.getMarshaller().getMediaType() == MediaType.APPLICATION_JSON) {
    List<NodeValue> nodeValues=new ArrayList();
    List<List> values=new ArrayList<List>();
    NodeValue mixedNodeValue=null;
    List mixedValues=null;
    while (cp.hasNext(iterator)) {
      Object nextValue=xmlChoiceCollectionMapping.convertObjectValueToDataValue(cp.next(iterator,session),session,marshalRecord.getMarshaller());
      NodeValue nodeValue=getNodeValueForValue(nextValue);
      if (nodeValue != null) {
        if (nodeValue == this) {
          mixedNodeValue=this;
          if (mixedValues == null) {
            mixedValues=new ArrayList();
          }
          mixedValues.add(nextValue);
        }
 else {
          int index=nodeValues.indexOf(nodeValue);
          if (index > -1) {
            values.get(index).add(nextValue);
          }
 else {
            nodeValues.add(nodeValue);
            List valuesList=new ArrayList();
            valuesList.add(nextValue);
            values.add(valuesList);
          }
        }
      }
    }
    if (mixedNodeValue != null) {
      nodeValues.add(mixedNodeValue);
      values.add(mixedValues);
    }
    for (int i=0; i < nodeValues.size(); i++) {
      NodeValue associatedNodeValue=nodeValues.get(i);
      List listValue=values.get(i);
      XPathFragment frag=null;
      if (associatedNodeValue == this) {
        frag=marshalRecord.getTextWrapperFragment();
      }
 else {
        frag=associatedNodeValue.getXPathNode().getXPathFragment();
        if (frag != null) {
          frag=getOwningFragment(associatedNodeValue,frag);
          associatedNodeValue=((XMLChoiceCollectionMappingUnmarshalNodeValue)associatedNodeValue).getChoiceElementMarshalNodeValue();
        }
      }
      if (frag != null) {
        marshalRecord.startCollection();
        for (int j=0; j < listValue.size(); j++) {
          marshalSingleValueWithNodeValue(frag,marshalRecord,object,listValue.get(j),session,namespaceResolver,ObjectMarshalContext.getInstance(),associatedNodeValue);
        }
        marshalRecord.endCollection();
      }
    }
  }
 else {
    while (cp.hasNext(iterator)) {
      Object nextValue=cp.next(iterator,session);
      marshalSingleValue(xPathFragment,marshalRecord,object,nextValue,session,namespaceResolver,ObjectMarshalContext.getInstance());
    }
  }
  return true;
}

Fixed version (ground truth):
public boolean marshal(XPathFragment xPathFragment,MarshalRecord marshalRecord,Object object,CoreAbstractSession session,NamespaceResolver namespaceResolver){
  if (xmlChoiceCollectionMapping.isReadOnly()) {
    return false;
  }
  Object value=xmlChoiceCollectionMapping.getAttributeValueFromObject(object);
  if (value == null) {
    AbstractNullPolicy wrapperNP=xmlChoiceCollectionMapping.getWrapperNullPolicy();
    if (wrapperNP != null && wrapperNP.getMarshalNullRepresentation() == XMLNullRepresentationType.XSI_NIL) {
      marshalRecord.nilSimple(namespaceResolver);
      return true;
    }
 else {
      return false;
    }
  }
  CoreContainerPolicy cp=getContainerPolicy();
  Object iterator=cp.iteratorFor(value);
  if (null != iterator && cp.hasNext(iterator)) {
    if (xPathFragment != null) {
      XPathFragment groupingFragment=marshalRecord.openStartGroupingElements(namespaceResolver);
      marshalRecord.closeStartGroupingElements(groupingFragment);
    }
  }
 else {
    return marshalRecord.emptyCollection(xPathFragment,namespaceResolver,xmlChoiceCollectionMapping.getWrapperNullPolicy() != null);
  }
  if (marshalRecord.getMarshaller().getMediaType().isApplicationJSON()) {
    List<NodeValue> nodeValues=new ArrayList();
    List<List> values=new ArrayList<List>();
    NodeValue mixedNodeValue=null;
    List mixedValues=null;
    while (cp.hasNext(iterator)) {
      Object nextValue=xmlChoiceCollectionMapping.convertObjectValueToDataValue(cp.next(iterator,session),session,marshalRecord.getMarshaller());
      NodeValue nodeValue=getNodeValueForValue(nextValue);
      if (nodeValue != null) {
        if (nodeValue == this) {
          mixedNodeValue=this;
          if (mixedValues == null) {
            mixedValues=new ArrayList();
          }
          mixedValues.add(nextValue);
        }
 else {
          int index=nodeValues.indexOf(nodeValue);
          if (index > -1) {
            values.get(index).add(nextValue);
          }
 else {
            nodeValues.add(nodeValue);
            List valuesList=new ArrayList();
            valuesList.add(nextValue);
            values.add(valuesList);
          }
        }
      }
    }
    if (mixedNodeValue != null) {
      nodeValues.add(mixedNodeValue);
      values.add(mixedValues);
    }
    for (int i=0; i < nodeValues.size(); i++) {
      NodeValue associatedNodeValue=nodeValues.get(i);
      List listValue=values.get(i);
      XPathFragment frag=null;
      if (associatedNodeValue == this) {
        frag=marshalRecord.getTextWrapperFragment();
      }
 else {
        frag=associatedNodeValue.getXPathNode().getXPathFragment();
        if (frag != null) {
          frag=getOwningFragment(associatedNodeValue,frag);
          associatedNodeValue=((XMLChoiceCollectionMappingUnmarshalNodeValue)associatedNodeValue).getChoiceElementMarshalNodeValue();
        }
      }
      if (frag != null) {
        marshalRecord.startCollection();
        for (int j=0; j < listValue.size(); j++) {
          marshalSingleValueWithNodeValue(frag,marshalRecord,object,listValue.get(j),session,namespaceResolver,ObjectMarshalContext.getInstance(),associatedNodeValue);
        }
        marshalRecord.endCollection();
      }
    }
  }
 else {
    while (cp.hasNext(iterator)) {
      Object nextValue=cp.next(iterator,session);
      marshalSingleValue(xPathFragment,marshalRecord,object,nextValue,session,namespaceResolver,ObjectMarshalContext.getInstance());
    }
  }
  return true;
}