Program: 65174
Project: 65174
Buggy source:
public boolean marshal(XPathFragment xPathFragment,MarshalRecord marshalRecord,Object object,AbstractSession 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().equals(XMLNullRepresentationType.XSI_NIL)) {
      marshalRecord.nilSimple(namespaceResolver);
      return true;
    }
 else {
      return false;
    }
  }
  ContainerPolicy 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 {
    if (xmlChoiceCollectionMapping.getWrapperNullPolicy() != null) {
      XPathFragment groupingFragment=marshalRecord.openStartGroupingElements(namespaceResolver);
      marshalRecord.closeStartGroupingElements(groupingFragment);
    }
 else {
      return false;
    }
  }
  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=getConvertedValue(cp.next(iterator,session),marshalRecord,session);
      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++) {
          associatedNodeValue.marshalSingleValue(frag,marshalRecord,object,listValue.get(j),session,namespaceResolver,ObjectMarshalContext.getInstance());
        }
        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,AbstractSession 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().equals(XMLNullRepresentationType.XSI_NIL)) {
      marshalRecord.nilSimple(namespaceResolver);
      return true;
    }
 else {
      return false;
    }
  }
  ContainerPolicy 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 {
    if (xmlChoiceCollectionMapping.getWrapperNullPolicy() != null) {
      XPathFragment groupingFragment=marshalRecord.openStartGroupingElements(namespaceResolver);
      marshalRecord.closeStartGroupingElements(groupingFragment);
    }
 else {
      return false;
    }
  }
  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=getConvertedValue(cp.next(iterator,session),marshalRecord,session);
      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;
}