Program: 66007
Project: 66007
Buggy source:
public void addGlobalElements(HashMap<QName,ElementDeclaration> additionalElements){
  for (  Entry<QName,ElementDeclaration> entry : additionalElements.entrySet()) {
    QName next=entry.getKey();
    if (next != null) {
      ElementDeclaration nextElement=entry.getValue();
      if (nextElement.getScopeClass() == GLOBAL.class) {
        String namespaceURI=next.getNamespaceURI();
        Schema targetSchema=getSchemaForNamespace(namespaceURI);
        if (targetSchema == null) {
          break;
        }
        if (targetSchema.getTopLevelElements().get(next.getLocalPart()) == null) {
          Element element=new Element();
          element.setName(next.getLocalPart());
          JavaClass javaClass=nextElement.getJavaType();
          QName schemaType=(QName)helper.getXMLToJavaTypeMap().get(javaClass.getRawName());
          if (schemaType != null) {
            element.setType(XMLConstants.SCHEMA_PREFIX + COLON + schemaType.getLocalPart());
          }
 else           if (areEquals(javaClass,JAVAX_ACTIVATION_DATAHANDLER) || areEquals(javaClass,byte[].class) || areEquals(javaClass,Byte[].class)|| areEquals(javaClass,Image.class)|| areEquals(javaClass,Source.class)|| areEquals(javaClass,JAVAX_MAIL_INTERNET_MIMEMULTIPART)) {
            schemaType=XMLConstants.BASE_64_BINARY_QNAME;
            if (nextElement.getTypeMappingInfo() != null) {
              if (nextElement.isXmlAttachmentRef()) {
                schemaType=XMLConstants.SWA_REF_QNAME;
              }
              if (nextElement.getXmlMimeType() != null) {
                element.getAttributesMap().put(XMLConstants.EXPECTED_CONTENT_TYPES_QNAME,nextElement.getXmlMimeType());
              }
            }
            String prefix=getOrGeneratePrefixForNamespace(schemaType.getNamespaceURI(),targetSchema);
            element.setType(prefix + COLON + schemaType.getLocalPart());
          }
 else           if (areEquals(javaClass,ClassConstants.XML_GREGORIAN_CALENDAR)) {
            schemaType=XMLConstants.ANY_SIMPLE_TYPE_QNAME;
            element.setType(XMLConstants.SCHEMA_PREFIX + COLON + schemaType.getLocalPart());
          }
 else {
            TypeInfo type=(TypeInfo)this.typeInfo.get(javaClass.getQualifiedName());
            if (type != null) {
              String typeName=null;
              if (type.isComplexType()) {
                typeName=type.getComplexType().getName();
              }
 else {
                typeName=type.getSimpleType().getName();
              }
              if (typeName == null) {
                Schema schema=getSchemaForNamespace(next.getNamespaceURI());
                ComplexType cType=new ComplexType();
                TypeDefParticle particle=new Sequence();
                cType.setTypeDefParticle(particle);
                element.setComplexType(cType);
                addToSchemaType(type,type.getPropertyList(),particle,cType,schema);
                targetSchema=schema;
              }
 else {
                if (type.getClassNamespace().equals(namespaceURI)) {
                  String prefix=targetSchema.getNamespaceResolver().resolveNamespaceURI(namespaceURI);
                  if (prefix != null && !(prefix.equals(EMPTY_STRING))) {
                    element.setType(prefix + COLON + typeName);
                  }
 else {
                    element.setType(typeName);
                  }
                }
 else {
                  Schema complexTypeSchema=getSchemaForNamespace(type.getClassNamespace());
                  String complexTypeSchemaNS=type.getClassNamespace();
                  if (complexTypeSchemaNS == null) {
                    complexTypeSchemaNS=EMPTY_STRING;
                  }
                  addImportIfRequired(targetSchema,complexTypeSchema,type.getClassNamespace());
                  String prefix=targetSchema.getNamespaceResolver().resolveNamespaceURI(complexTypeSchemaNS);
                  if (prefix != null) {
                    element.setType(prefix + COLON + typeName);
                  }
 else {
                    element.setType(typeName);
                  }
                }
              }
            }
          }
          if (nextElement.getSubstitutionHead() != null) {
            String subLocal=nextElement.getSubstitutionHead().getLocalPart();
            String subNamespace=nextElement.getSubstitutionHead().getNamespaceURI();
            String prefix=getPrefixForNamespace(subNamespace,targetSchema.getNamespaceResolver());
            if (prefix == null || prefix.equals(EMPTY_STRING)) {
              element.setSubstitutionGroup(subLocal);
            }
 else {
              element.setSubstitutionGroup(prefix + COLON + subLocal);
            }
          }
          targetSchema.addTopLevelElement(element);
          SchemaTypeInfo info=this.schemaTypeInfo.get(javaClass.getQualifiedName());
          if (info == null) {
            info=new SchemaTypeInfo();
            info.setSchemaTypeName(schemaType);
            schemaTypeInfo.put(javaClass.getQualifiedName(),info);
          }
          info.getGlobalElementDeclarations().add(next);
        }
      }
    }
  }
}

Fixed version (ground truth):
public void addGlobalElements(HashMap<QName,ElementDeclaration> additionalElements){
  for (  Entry<QName,ElementDeclaration> entry : additionalElements.entrySet()) {
    QName next=entry.getKey();
    if (next != null) {
      ElementDeclaration nextElement=entry.getValue();
      if (nextElement.getScopeClass() == GLOBAL.class) {
        String namespaceURI=next.getNamespaceURI();
        Schema targetSchema=getSchemaForNamespace(namespaceURI);
        if (targetSchema == null) {
          continue;
        }
        if (targetSchema.getTopLevelElements().get(next.getLocalPart()) == null) {
          Element element=new Element();
          element.setName(next.getLocalPart());
          JavaClass javaClass=nextElement.getJavaType();
          QName schemaType=(QName)helper.getXMLToJavaTypeMap().get(javaClass.getRawName());
          if (schemaType != null) {
            element.setType(XMLConstants.SCHEMA_PREFIX + COLON + schemaType.getLocalPart());
          }
 else           if (areEquals(javaClass,JAVAX_ACTIVATION_DATAHANDLER) || areEquals(javaClass,byte[].class) || areEquals(javaClass,Byte[].class)|| areEquals(javaClass,Image.class)|| areEquals(javaClass,Source.class)|| areEquals(javaClass,JAVAX_MAIL_INTERNET_MIMEMULTIPART)) {
            schemaType=XMLConstants.BASE_64_BINARY_QNAME;
            if (nextElement.getTypeMappingInfo() != null) {
              if (nextElement.isXmlAttachmentRef()) {
                schemaType=XMLConstants.SWA_REF_QNAME;
              }
              if (nextElement.getXmlMimeType() != null) {
                element.getAttributesMap().put(XMLConstants.EXPECTED_CONTENT_TYPES_QNAME,nextElement.getXmlMimeType());
              }
            }
            String prefix=getOrGeneratePrefixForNamespace(schemaType.getNamespaceURI(),targetSchema);
            element.setType(prefix + COLON + schemaType.getLocalPart());
          }
 else           if (areEquals(javaClass,ClassConstants.XML_GREGORIAN_CALENDAR)) {
            schemaType=XMLConstants.ANY_SIMPLE_TYPE_QNAME;
            element.setType(XMLConstants.SCHEMA_PREFIX + COLON + schemaType.getLocalPart());
          }
 else {
            TypeInfo type=(TypeInfo)this.typeInfo.get(javaClass.getQualifiedName());
            if (type != null) {
              String typeName=null;
              if (type.isComplexType()) {
                typeName=type.getComplexType().getName();
              }
 else {
                typeName=type.getSimpleType().getName();
              }
              if (typeName == null) {
                Schema schema=getSchemaForNamespace(next.getNamespaceURI());
                ComplexType cType=new ComplexType();
                TypeDefParticle particle=new Sequence();
                cType.setTypeDefParticle(particle);
                element.setComplexType(cType);
                addToSchemaType(type,type.getPropertyList(),particle,cType,schema);
                targetSchema=schema;
              }
 else {
                if (type.getClassNamespace().equals(namespaceURI)) {
                  String prefix=targetSchema.getNamespaceResolver().resolveNamespaceURI(namespaceURI);
                  if (prefix != null && !(prefix.equals(EMPTY_STRING))) {
                    element.setType(prefix + COLON + typeName);
                  }
 else {
                    element.setType(typeName);
                  }
                }
 else {
                  Schema complexTypeSchema=getSchemaForNamespace(type.getClassNamespace());
                  String complexTypeSchemaNS=type.getClassNamespace();
                  if (complexTypeSchemaNS == null) {
                    complexTypeSchemaNS=EMPTY_STRING;
                  }
                  addImportIfRequired(targetSchema,complexTypeSchema,type.getClassNamespace());
                  String prefix=targetSchema.getNamespaceResolver().resolveNamespaceURI(complexTypeSchemaNS);
                  if (prefix != null) {
                    element.setType(prefix + COLON + typeName);
                  }
 else {
                    element.setType(typeName);
                  }
                }
              }
            }
          }
          if (nextElement.getSubstitutionHead() != null) {
            String subLocal=nextElement.getSubstitutionHead().getLocalPart();
            String subNamespace=nextElement.getSubstitutionHead().getNamespaceURI();
            String prefix=getPrefixForNamespace(subNamespace,targetSchema.getNamespaceResolver());
            if (prefix == null || prefix.equals(EMPTY_STRING)) {
              element.setSubstitutionGroup(subLocal);
            }
 else {
              element.setSubstitutionGroup(prefix + COLON + subLocal);
            }
          }
          targetSchema.addTopLevelElement(element);
          SchemaTypeInfo info=this.schemaTypeInfo.get(javaClass.getQualifiedName());
          if (info == null) {
            info=new SchemaTypeInfo();
            info.setSchemaTypeName(schemaType);
            schemaTypeInfo.put(javaClass.getQualifiedName(),info);
          }
          info.getGlobalElementDeclarations().add(next);
        }
      }
    }
  }
}