Program: 65539
Project: 65539
Buggy source:
public boolean addXsiTypeAndClassIndicatorIfRequired(XMLRecord record,XMLDescriptor xmlDescriptor,XMLDescriptor referenceDescriptor,XMLField xmlField,boolean addToNamespaceResolver){
  boolean xsiTypeIndicatorField=false;
  if (xmlDescriptor.hasInheritance()) {
    XMLField indicatorField=(XMLField)xmlDescriptor.getInheritancePolicy().getClassIndicatorField();
    if (indicatorField != null) {
      if (indicatorField.getLastXPathFragment().getNamespaceURI() != null && indicatorField.getLastXPathFragment().getNamespaceURI().equals(XMLConstants.SCHEMA_INSTANCE_URL) && indicatorField.getLastXPathFragment().getLocalName().equals(XMLConstants.SCHEMA_TYPE_ATTRIBUTE)) {
        xsiTypeIndicatorField=true;
      }
      if (!xsiTypeIndicatorField) {
        xmlDescriptor.getInheritancePolicy().addClassIndicatorFieldToRow(record);
      }
    }
  }
  QName leafType=null;
  if (xmlField != null) {
    leafType=xmlField.getLeafElementType();
    XMLSchemaReference xmlRef=xmlDescriptor.getSchemaReference();
    if (xmlRef != null) {
      if (leafType == null && xmlRef.getType() == XMLSchemaReference.ELEMENT) {
        return false;
      }
      if (leafType == null && referenceDescriptor == null) {
        QName typeValueQName=getTypeValueToWriteAsQName(record,xmlDescriptor,xmlRef,addToNamespaceResolver);
        writeXsiTypeAttribute(xmlDescriptor,record,typeValueQName,addToNamespaceResolver);
        return true;
      }
      if (((xmlRef.getType() == XMLSchemaReference.COMPLEX_TYPE) || (xmlRef.getType() == XMLSchemaReference.SIMPLE_TYPE)) && xmlRef.getSchemaContext() != null && xmlRef.isGlobalDefinition()) {
        QName ctxQName=xmlRef.getSchemaContextAsQName(xmlDescriptor.getNamespaceResolver());
        if (leafType != null) {
          if (!ctxQName.equals(leafType)) {
            QName typeValueQName=getTypeValueToWriteAsQName(record,xmlDescriptor,xmlRef,addToNamespaceResolver);
            writeXsiTypeAttribute(xmlDescriptor,record,typeValueQName,addToNamespaceResolver);
            return true;
          }
        }
      }
    }
  }
  if (referenceDescriptor != null && referenceDescriptor == xmlDescriptor) {
    return false;
  }
  if (xmlDescriptor.hasInheritance() && !xmlDescriptor.getInheritancePolicy().isRootParentDescriptor()) {
    XMLField indicatorField=(XMLField)xmlDescriptor.getInheritancePolicy().getClassIndicatorField();
    if (indicatorField != null && xsiTypeIndicatorField) {
      Object classIndicatorValueObject=xmlDescriptor.getInheritancePolicy().getClassIndicatorMapping().get(xmlDescriptor.getJavaClass());
      QName classIndicatorQName=null;
      if (classIndicatorValueObject instanceof QName) {
        classIndicatorQName=(QName)classIndicatorValueObject;
      }
 else {
        String classIndicatorValue=(String)xmlDescriptor.getInheritancePolicy().getClassIndicatorMapping().get(xmlDescriptor.getJavaClass());
        int nsindex=classIndicatorValue.indexOf(XMLConstants.COLON);
        String localName=null;
        String prefix=null;
        if (nsindex != -1) {
          localName=classIndicatorValue.substring(nsindex + 1);
          prefix=classIndicatorValue.substring(0,nsindex);
        }
 else {
          localName=classIndicatorValue;
        }
        String namespaceURI=xmlDescriptor.getNonNullNamespaceResolver().resolveNamespacePrefix(prefix);
        classIndicatorQName=new QName(namespaceURI,localName);
      }
      if (leafType == null || !classIndicatorQName.equals(leafType)) {
        if (xmlDescriptor.getInheritancePolicy().hasClassExtractor()) {
          xmlDescriptor.getInheritancePolicy().addClassIndicatorFieldToRow(record);
        }
 else {
          writeXsiTypeAttribute(xmlDescriptor,record,classIndicatorQName,addToNamespaceResolver);
        }
        return true;
      }
      return false;
    }
  }
  return false;
}

Fixed version (ground truth):
public boolean addXsiTypeAndClassIndicatorIfRequired(XMLRecord record,XMLDescriptor xmlDescriptor,XMLDescriptor referenceDescriptor,XMLField xmlField,boolean addToNamespaceResolver){
  if (descriptor.hasInheritance() && !xsiTypeIndicatorField) {
    xmlDescriptor.getInheritancePolicy().addClassIndicatorFieldToRow(record);
  }
  QName leafType=null;
  if (xmlField != null) {
    leafType=xmlField.getLeafElementType();
    XMLSchemaReference xmlRef=xmlDescriptor.getSchemaReference();
    if (xmlRef != null) {
      if (leafType == null && xmlRef.getType() == XMLSchemaReference.ELEMENT) {
        return false;
      }
      if (leafType == null && referenceDescriptor == null) {
        QName typeValueQName=getTypeValueToWriteAsQName(record,xmlDescriptor,xmlRef,addToNamespaceResolver);
        writeXsiTypeAttribute(xmlDescriptor,record,typeValueQName,addToNamespaceResolver);
        return true;
      }
      if (((xmlRef.getType() == XMLSchemaReference.COMPLEX_TYPE) || (xmlRef.getType() == XMLSchemaReference.SIMPLE_TYPE)) && xmlRef.getSchemaContext() != null && xmlRef.isGlobalDefinition()) {
        if (leafType != null) {
          QName ctxQName=xmlRef.getSchemaContextAsQName(xmlDescriptor.getNamespaceResolver());
          if (!ctxQName.equals(leafType)) {
            QName typeValueQName=getTypeValueToWriteAsQName(record,xmlDescriptor,xmlRef,addToNamespaceResolver);
            writeXsiTypeAttribute(xmlDescriptor,record,typeValueQName,addToNamespaceResolver);
            return true;
          }
        }
      }
    }
  }
  if (referenceDescriptor != null && referenceDescriptor == xmlDescriptor) {
    return false;
  }
  if (xmlDescriptor.hasInheritance() && !xmlDescriptor.getInheritancePolicy().isRootParentDescriptor()) {
    XMLField indicatorField=(XMLField)xmlDescriptor.getInheritancePolicy().getClassIndicatorField();
    if (indicatorField != null && xsiTypeIndicatorField) {
      Object classIndicatorValueObject=xmlDescriptor.getInheritancePolicy().getClassIndicatorMapping().get(xmlDescriptor.getJavaClass());
      QName classIndicatorQName=null;
      if (classIndicatorValueObject instanceof QName) {
        classIndicatorQName=(QName)classIndicatorValueObject;
      }
 else {
        String classIndicatorValue=(String)xmlDescriptor.getInheritancePolicy().getClassIndicatorMapping().get(xmlDescriptor.getJavaClass());
        int nsindex=classIndicatorValue.indexOf(XMLConstants.COLON);
        String localName=null;
        String prefix=null;
        if (nsindex != -1) {
          localName=classIndicatorValue.substring(nsindex + 1);
          prefix=classIndicatorValue.substring(0,nsindex);
        }
 else {
          localName=classIndicatorValue;
        }
        String namespaceURI=xmlDescriptor.getNonNullNamespaceResolver().resolveNamespacePrefix(prefix);
        classIndicatorQName=new QName(namespaceURI,localName);
      }
      if (leafType == null || !classIndicatorQName.equals(leafType)) {
        if (xmlDescriptor.getInheritancePolicy().hasClassExtractor()) {
          xmlDescriptor.getInheritancePolicy().addClassIndicatorFieldToRow(record);
        }
 else {
          writeXsiTypeAttribute(xmlDescriptor,record,classIndicatorQName,addToNamespaceResolver);
        }
        return true;
      }
      return false;
    }
  }
  return false;
}