Program: 129782
Project: 129782
Buggy source:
private void addSubstitutionDetails(XSDSchema xsdSchema,ATreeNode parentNode,XSDElementDeclaration elementDeclaration,ATreeNode rootSubsNode) throws OdaException, IllegalAccessException, InvocationTargetException {
  boolean hasSubstitution=false;
  Set<ATreeNode> subsChildren=new HashSet<ATreeNode>();
  List<XSDElementDeclaration> directSubstitutionGroups=getDirectSubstitutionGroups(elementDeclaration);
  for (  XSDElementDeclaration xsdElementDeclaration : directSubstitutionGroups) {
    String elementName=xsdElementDeclaration.getName();
    hasSubstitution=true;
    if (rootSubsNode == null) {
      rootSubsNode=parentNode;
    }
    ATreeNode node=new ATreeNode();
    String prefix=null;
    String namespace=xsdElementDeclaration.getTargetNamespace();
    node.setCurrentNamespace(namespace);
    XSDTypeDefinition typeDef=xsdElementDeclaration.getTypeDefinition();
    if (namespace != null) {
      prefix=xsdElementDeclaration.getQName().contains("String_Node_Str") ? xsdElementDeclaration.getQName().split("String_Node_Str")[0] : "String_Node_Str";
      if (prefix == null || prefix.isEmpty()) {
        if (xsdSchema.getQNamePrefixToNamespaceMap().containsValue(typeDef.getTargetNamespace())) {
          for (          String key : xsdSchema.getQNamePrefixToNamespaceMap().keySet()) {
            if (xsdSchema.getQNamePrefixToNamespaceMap().get(key).equals(typeDef.getTargetNamespace())) {
              prefix=key;
            }
          }
        }
      }
      if (isEnableGeneratePrefix() && (prefix == null || prefix.isEmpty())) {
        prefix="String_Node_Str" + prefixNumberGenerated;
        prefixNumberGenerated++;
      }
      if (prefix != null && !prefix.isEmpty()) {
        elementName=prefix + "String_Node_Str" + xsdElementDeclaration.getName();
        namespaceToPrefix.put(namespace,prefix);
      }
 else {
        ATreeNode namespaceNode=new ATreeNode();
        namespaceNode.setDataType("String_Node_Str");
        namespaceNode.setType(ATreeNode.NAMESPACE_TYPE);
        namespaceNode.setValue(namespace);
        node.addChild(namespaceNode);
      }
    }
    node.setValue(elementName);
    node.setType(ATreeNode.ELEMENT_TYPE);
    node.setDataType(xsdElementDeclaration.getName());
    XSDTypeDefinition xsdTypeDefinition=xsdElementDeclaration.getTypeDefinition();
    XSDComplexTypeDefinition generalType=xsdSchema.resolveComplexTypeDefinitionURI(xsdElementDeclaration.getURI());
    if (generalType.getContainer() != null) {
      xsdTypeDefinition=generalType;
    }
    if (xsdTypeDefinition != null && xsdTypeDefinition.getName() != null) {
      node.setDataType(xsdTypeDefinition.getName());
    }
    if (xsdTypeDefinition instanceof XSDComplexTypeDefinition) {
      addComplexTypeDetails(xsdSchema,node,xsdTypeDefinition,prefix,namespace,"String_Node_Str" + elementName + "String_Node_Str");
    }
    List<String> namespaceList=new ArrayList(namespaceToPrefix.keySet());
    Collections.reverse(namespaceList);
    for (    String currentNamespace : namespaceList) {
      ATreeNode namespaceNode=null;
      if (currentNamespace != null) {
        prefix=namespaceToPrefix.get(currentNamespace);
        namespaceNode=new ATreeNode();
        namespaceNode.setDataType(prefix);
        namespaceNode.setType(ATreeNode.NAMESPACE_TYPE);
        namespaceNode.setValue(currentNamespace);
        node.addAsFirstChild(namespaceNode);
      }
    }
    addSubstitutionDetails(xsdSchema,node,xsdElementDeclaration,rootSubsNode);
    subsChildren.add(node);
  }
  if (hasSubstitution) {
    if (supportSubstitution) {
      Object oldValue=parentNode.getValue();
      parentNode.setSubstitution(true);
      parentNode.setValue(oldValue + SUBS);
      Object[] originalChildren=parentNode.getChildren();
      parentNode.removeAllChildren();
      if (!elementDeclaration.isAbstract()) {
        ATreeNode cloneNode=new ATreeNode();
        BeanUtils.copyProperties(cloneNode,parentNode);
        cloneNode.setSubstitution(false);
        cloneNode.setValue(oldValue);
        cloneNode.addChild(originalChildren);
        parentNode.addAsFirstChild(cloneNode);
      }
      parentNode.addChild(subsChildren.toArray());
    }
 else {
      if (rootSubsNode != null) {
        ATreeNode parent=rootSubsNode.getParent();
        if (parent != null) {
          List children=Arrays.asList(parent.getChildren());
          for (          ATreeNode child : subsChildren) {
            if (!children.contains(child)) {
              parent.addChild(child);
            }
          }
        }
      }
    }
  }
}

Fixed version (ground truth):
private void addSubstitutionDetails(XSDSchema xsdSchema,ATreeNode parentNode,XSDElementDeclaration elementDeclaration,ATreeNode rootSubsNode) throws OdaException, IllegalAccessException, InvocationTargetException {
  boolean hasSubstitution=false;
  Set<ATreeNode> subsChildren=new HashSet<ATreeNode>();
  List<XSDElementDeclaration> directSubstitutionGroups=getDirectSubstitutionGroups(elementDeclaration);
  for (  XSDElementDeclaration xsdElementDeclaration : directSubstitutionGroups) {
    String elementName=xsdElementDeclaration.getName();
    hasSubstitution=true;
    if (rootSubsNode == null) {
      rootSubsNode=parentNode;
    }
    ATreeNode node=new ATreeNode();
    String prefix=null;
    String namespace=xsdElementDeclaration.getTargetNamespace();
    node.setCurrentNamespace(namespace);
    XSDTypeDefinition typeDef=xsdElementDeclaration.getTypeDefinition();
    if (namespace != null) {
      prefix=xsdElementDeclaration.getQName().contains("String_Node_Str") ? xsdElementDeclaration.getQName().split("String_Node_Str")[0] : "String_Node_Str";
      if (prefix == null || prefix.isEmpty()) {
        if (xsdSchema.getQNamePrefixToNamespaceMap().containsValue(typeDef.getTargetNamespace())) {
          for (          String key : xsdSchema.getQNamePrefixToNamespaceMap().keySet()) {
            if (xsdSchema.getQNamePrefixToNamespaceMap().get(key).equals(typeDef.getTargetNamespace())) {
              prefix=key;
            }
          }
        }
      }
      if (isEnableGeneratePrefix() && (prefix == null || prefix.isEmpty())) {
        prefix="String_Node_Str" + prefixNumberGenerated;
        prefixNumberGenerated++;
      }
      if (prefix != null && !prefix.isEmpty()) {
        elementName=prefix + "String_Node_Str" + xsdElementDeclaration.getName();
        namespaceToPrefix.put(namespace,prefix);
      }
 else {
        ATreeNode namespaceNode=new ATreeNode();
        namespaceNode.setDataType("String_Node_Str");
        namespaceNode.setType(ATreeNode.NAMESPACE_TYPE);
        namespaceNode.setValue(namespace);
        node.addChild(namespaceNode);
      }
    }
    node.setValue(elementName);
    node.setType(ATreeNode.ELEMENT_TYPE);
    node.setDataType(xsdElementDeclaration.getName());
    XSDTypeDefinition xsdTypeDefinition=xsdElementDeclaration.getTypeDefinition();
    if (xsdTypeDefinition == null) {
      XSDComplexTypeDefinition generalType=xsdSchema.resolveComplexTypeDefinitionURI(xsdElementDeclaration.getURI());
      if (generalType.getContainer() != null) {
        xsdTypeDefinition=generalType;
      }
    }
    if (xsdTypeDefinition != null && xsdTypeDefinition.getName() != null) {
      node.setDataType(xsdTypeDefinition.getName());
    }
    if (xsdTypeDefinition instanceof XSDComplexTypeDefinition) {
      addComplexTypeDetails(xsdSchema,node,xsdTypeDefinition,prefix,namespace,"String_Node_Str" + elementName + "String_Node_Str");
    }
    List<String> namespaceList=new ArrayList(namespaceToPrefix.keySet());
    Collections.reverse(namespaceList);
    for (    String currentNamespace : namespaceList) {
      ATreeNode namespaceNode=null;
      if (currentNamespace != null) {
        prefix=namespaceToPrefix.get(currentNamespace);
        namespaceNode=new ATreeNode();
        namespaceNode.setDataType(prefix);
        namespaceNode.setType(ATreeNode.NAMESPACE_TYPE);
        namespaceNode.setValue(currentNamespace);
        node.addAsFirstChild(namespaceNode);
      }
    }
    addSubstitutionDetails(xsdSchema,node,xsdElementDeclaration,rootSubsNode);
    subsChildren.add(node);
  }
  if (hasSubstitution) {
    if (supportSubstitution) {
      Object oldValue=parentNode.getValue();
      parentNode.setSubstitution(true);
      parentNode.setValue(oldValue + SUBS);
      Object[] originalChildren=parentNode.getChildren();
      parentNode.removeAllChildren();
      if (!elementDeclaration.isAbstract()) {
        ATreeNode cloneNode=new ATreeNode();
        BeanUtils.copyProperties(cloneNode,parentNode);
        cloneNode.setSubstitution(false);
        cloneNode.setValue(oldValue);
        cloneNode.addChild(originalChildren);
        parentNode.addAsFirstChild(cloneNode);
      }
      parentNode.addChild(subsChildren.toArray());
    }
 else {
      if (rootSubsNode != null) {
        ATreeNode parent=rootSubsNode.getParent();
        if (parent != null) {
          List children=Arrays.asList(parent.getChildren());
          for (          ATreeNode child : subsChildren) {
            if (!children.contains(child)) {
              parent.addChild(child);
            }
          }
        }
      }
    }
  }
}