Program: 146003
Project: 146003
Buggy source:
private static List<ComplexTypeMetadata> _sortTypes(MetadataRepository repository,List<ComplexTypeMetadata> typesSubSet,final SortType sortType){
  final List<ComplexTypeMetadata> types=new ArrayList<ComplexTypeMetadata>();
  for (  ComplexTypeMetadata currentType : typesSubSet) {
    if (currentType instanceof ContainedComplexTypeMetadata) {
      types.add(currentType.getEntity());
    }
 else     if (!types.contains(currentType)) {
      types.add(currentType);
    }
  }
  final int typeNumber=types.size();
  byte[][] dependencyGraph=new byte[typeNumber][typeNumber];
  for (  final ComplexTypeMetadata type : types) {
    dependencyGraph[getId(type,types)]=type.accept(new DefaultMetadataVisitor<byte[]>(){
      final Set<TypeMetadata> processedTypes=new HashSet<TypeMetadata>();
      final Set<TypeMetadata> processedReferences=new HashSet<TypeMetadata>();
      final byte[] lineContent=new byte[typeNumber];
      @Override public byte[] visit(      ComplexTypeMetadata complexType){
        if (processedTypes.contains(complexType)) {
          return lineContent;
        }
 else {
          processedTypes.add(complexType);
        }
        if (complexType.isInstantiable()) {
          Collection<TypeMetadata> superTypes=complexType.getSuperTypes();
          for (          TypeMetadata superType : superTypes) {
            if (superType instanceof ComplexTypeMetadata) {
              int id=types.indexOf(superType);
              if (id >= 0) {
                lineContent[id]++;
              }
            }
          }
          super.visit(complexType);
        }
        if (complexType.isInstantiable()) {
          processedTypes.clear();
        }
        return lineContent;
      }
      @Override public byte[] visit(      ContainedTypeFieldMetadata containedField){
        ComplexTypeMetadata containedType=containedField.getContainedType();
        if (processedTypes.contains(containedType)) {
          return lineContent;
        }
 else {
          processedTypes.add(containedType);
        }
        containedType.accept(this);
        for (        ComplexTypeMetadata subType : containedType.getSubTypes()) {
          if (processedTypes.contains(subType)) {
            return lineContent;
          }
 else {
            processedTypes.add(subType);
            subType.accept(this);
          }
        }
        return lineContent;
      }
      @Override public byte[] visit(      ReferenceFieldMetadata referenceField){
        boolean isInherited=!referenceField.getDeclaringType().equals(referenceField.getContainingType());
        if (isInherited) {
          return lineContent;
        }
        ComplexTypeMetadata referencedType=referenceField.getReferencedType();
        if (!processedReferences.add(referencedType)) {
          return lineContent;
        }
        if (include(referenceField) && referenceField.isFKIntegrity()) {
          if (referencedType.isInstantiable()) {
            if (types.contains(referencedType) && (!processedTypes.contains(referencedType) || isReferencedBySelf(referenceField))) {
              lineContent[getId(referencedType,types)]++;
              if (sortType == SortType.LENIENT) {
                for (                ComplexTypeMetadata subType : referencedType.getSubTypes()) {
                  lineContent[getId(subType,types)]++;
                }
              }
            }
          }
        }
        return lineContent;
      }
      private boolean include(      FieldMetadata field){
        if (field == null) {
          return false;
        }
        ComplexTypeMetadata containingType=field.getContainingType();
        FieldMetadata containerField=containingType.getContainer();
switch (sortType) {
case STRICT:
          if (containerField != null) {
            return include(containerField) && field.isMandatory();
          }
 else {
            return field.isMandatory();
          }
case LENIENT:
        return true;
default :
      throw new NotImplementedException("String_Node_Str" + sortType + "String_Node_Str");
  }
}
private boolean isReferencedBySelf(ReferenceFieldMetadata referenceField){
  ComplexTypeMetadata containingType=referenceField.getContainingType();
  if (containingType instanceof ContainedComplexTypeMetadata) {
    containingType=containingType.getContainer().getContainingType();
  }
  return containingType.equals(referenceField.getReferencedType());
}
}
);
}
if (LOGGER.isTraceEnabled()) {
StringBuilder builder=logDependencyMatrix(dependencyGraph);
LOGGER.trace("String_Node_Str" + builder.toString());
}
List<ComplexTypeMetadata> sortedTypes=new LinkedList<ComplexTypeMetadata>();
Set<ComplexTypeMetadata> noIncomingEdges=new HashSet<ComplexTypeMetadata>();
int lineNumber=0;
for (byte[] line : dependencyGraph) {
if (!hasIncomingEdges(line)) {
noIncomingEdges.add(getType(types,lineNumber));
}
lineNumber++;
}
while (!noIncomingEdges.isEmpty()) {
Iterator<ComplexTypeMetadata> iterator=noIncomingEdges.iterator();
ComplexTypeMetadata type=iterator.next();
iterator.remove();
sortedTypes.add(type);
int columnNumber=getId(type,types);
for (int i=0; i < typeNumber; i++) {
int edge=dependencyGraph[i][columnNumber];
if (edge > 0) {
  dependencyGraph[i][columnNumber]-=edge;
  if (!hasIncomingEdges(dependencyGraph[i])) {
    noIncomingEdges.add(getType(types,i));
  }
}
}
}
if (LOGGER.isTraceEnabled()) {
StringBuilder builder=logDependencyMatrix(dependencyGraph);
LOGGER.trace("String_Node_Str" + builder.toString());
}
if (sortedTypes.size() < dependencyGraph.length) {
lineNumber=0;
List<List<ComplexTypeMetadata>> cycles=new LinkedList<List<ComplexTypeMetadata>>();
for (byte[] line : dependencyGraph) {
if (hasIncomingEdges(line)) {
  List<ComplexTypeMetadata> dependencyPath=new LinkedList<ComplexTypeMetadata>();
  int currentLineNumber=lineNumber;
  do {
    ComplexTypeMetadata type=getType(types,currentLineNumber);
    dependencyPath.add(type);
    InboundReferences incomingReferences=new InboundReferences(type);
    Set<ReferenceFieldMetadata> incomingFields=repository.accept(incomingReferences);
    boolean hasMetDependency=false;
    for (    ReferenceFieldMetadata incomingField : incomingFields) {
      ComplexTypeMetadata containingType=repository.getComplexType(incomingField.getEntityTypeName());
      if (containingType != null) {
        int currentDependency=getId(containingType,types);
        if (hasIncomingEdges(dependencyGraph[currentDependency])) {
          dependencyGraph[currentLineNumber][currentDependency]--;
          currentLineNumber=currentDependency;
          hasMetDependency=true;
          break;
        }
      }
    }
    if (!hasMetDependency) {
      break;
    }
  }
 while (currentLineNumber != lineNumber);
  if (dependencyPath.size() >= 1) {
    dependencyPath.add(getType(types,lineNumber));
    cycles.add(dependencyPath);
  }
}
lineNumber++;
}
switch (sortType) {
case STRICT:
if (!cycles.isEmpty()) {
  Iterator<List<ComplexTypeMetadata>> cyclesIterator=cycles.iterator();
  Map<ComplexTypeMetadata,List<FieldMetadata>> cycleHints=new HashMap<ComplexTypeMetadata,List<FieldMetadata>>();
  while (cyclesIterator.hasNext()) {
    Iterator<ComplexTypeMetadata> dependencyPathIterator=cyclesIterator.next().iterator();
    ComplexTypeMetadata previous=null;
    while (dependencyPathIterator.hasNext()) {
      ComplexTypeMetadata currentType=dependencyPathIterator.next();
      ArrayList<FieldMetadata> fields=new ArrayList<FieldMetadata>();
      cycleHints.put(currentType,fields);
      if (previous != null) {
        Set<ReferenceFieldMetadata> inboundReferences=repository.accept(new InboundReferences(currentType));
        for (        ReferenceFieldMetadata inboundReference : inboundReferences) {
          ComplexTypeMetadata entity=repository.getComplexType(inboundReference.getEntityTypeName());
          if (entity != null) {
            fields.add(inboundReference);
          }
        }
      }
      previous=currentType;
    }
  }
  throw new CircularDependencyException(cycleHints);
}
case LENIENT:
for (List<ComplexTypeMetadata> cycle : cycles) {
cycle.remove(cycle.size() - 1);
for (ComplexTypeMetadata cycleElement : cycle) {
  if (!sortedTypes.contains(cycleElement)) {
    sortedTypes.add(cycleElement);
  }
}
}
break;
default :
throw new NotImplementedException("String_Node_Str" + sortType + "String_Node_Str");
}
}
return sortedTypes;
}

Fixed version (ground truth):
private static List<ComplexTypeMetadata> _sortTypes(MetadataRepository repository,List<ComplexTypeMetadata> typesSubSet,final SortType sortType){
  final List<ComplexTypeMetadata> types=new ArrayList<ComplexTypeMetadata>();
  for (  ComplexTypeMetadata currentType : typesSubSet) {
    if (currentType instanceof ContainedComplexTypeMetadata) {
      types.add(currentType.getEntity());
    }
 else     if (!types.contains(currentType)) {
      types.add(currentType);
    }
  }
  final int typeNumber=types.size();
  byte[][] dependencyGraph=new byte[typeNumber][typeNumber];
  for (  final ComplexTypeMetadata type : types) {
    dependencyGraph[getId(type,types)]=type.accept(new DefaultMetadataVisitor<byte[]>(){
      final Set<TypeMetadata> processedTypes=new HashSet<TypeMetadata>();
      final Set<TypeMetadata> processedReferences=new HashSet<TypeMetadata>();
      final byte[] lineContent=new byte[typeNumber];
      @Override public byte[] visit(      ComplexTypeMetadata complexType){
        if (processedTypes.contains(complexType)) {
          return lineContent;
        }
 else {
          processedTypes.add(complexType);
        }
        if (complexType.isInstantiable()) {
          Collection<TypeMetadata> superTypes=complexType.getSuperTypes();
          for (          TypeMetadata superType : superTypes) {
            if (superType instanceof ComplexTypeMetadata) {
              int id=types.indexOf(superType);
              if (id >= 0) {
                lineContent[id]++;
              }
            }
          }
          super.visit(complexType);
        }
        if (complexType.isInstantiable()) {
          processedTypes.clear();
        }
        return lineContent;
      }
      @Override public byte[] visit(      ContainedTypeFieldMetadata containedField){
        ComplexTypeMetadata containedType=containedField.getContainedType();
        if (processedTypes.contains(containedType)) {
          return lineContent;
        }
 else {
          processedTypes.add(containedType);
        }
        containedType.accept(this);
        for (        ComplexTypeMetadata subType : containedType.getSubTypes()) {
          if (processedTypes.contains(subType)) {
            return lineContent;
          }
 else {
            processedTypes.add(subType);
            subType.accept(this);
          }
        }
        return lineContent;
      }
      @Override public byte[] visit(      ReferenceFieldMetadata referenceField){
        boolean isInherited=!referenceField.getDeclaringType().equals(referenceField.getContainingType());
        if (isInherited) {
          return lineContent;
        }
        ComplexTypeMetadata referencedType=referenceField.getReferencedType();
        if (!processedReferences.add(referencedType)) {
          return lineContent;
        }
        if (include(referenceField) && referenceField.isFKIntegrity()) {
          if (referencedType.isInstantiable()) {
            if (types.contains(referencedType) && (!processedTypes.contains(referencedType) || isReferencedBySelf(referenceField))) {
              lineContent[getId(referencedType,types)]++;
              if (sortType == SortType.LENIENT) {
                for (                ComplexTypeMetadata subType : referencedType.getSubTypes()) {
                  lineContent[getId(subType,types)]++;
                }
              }
            }
          }
        }
        return lineContent;
      }
      private boolean include(      FieldMetadata field){
        if (field == null) {
          return false;
        }
        ComplexTypeMetadata containingType=field.getContainingType();
        FieldMetadata containerField=containingType.getContainer();
switch (sortType) {
case STRICT:
          if (containerField != null) {
            return include(containerField) && field.isMandatory();
          }
 else {
            return field.isMandatory();
          }
case LENIENT:
        return true;
default :
      throw new NotImplementedException("String_Node_Str" + sortType + "String_Node_Str");
  }
}
private boolean isReferencedBySelf(ReferenceFieldMetadata referenceField){
  ComplexTypeMetadata containingType=referenceField.getContainingType();
  if (containingType instanceof ContainedComplexTypeMetadata) {
    containingType=containingType.getContainer().getContainingType();
  }
  return containingType.equals(referenceField.getReferencedType());
}
}
);
}
if (LOGGER.isTraceEnabled()) {
StringBuilder builder=logDependencyMatrix(dependencyGraph);
LOGGER.trace("String_Node_Str" + builder.toString());
}
List<ComplexTypeMetadata> sortedTypes=new LinkedList<ComplexTypeMetadata>();
Set<ComplexTypeMetadata> noIncomingEdges=new HashSet<ComplexTypeMetadata>();
int lineNumber=0;
for (byte[] line : dependencyGraph) {
if (!hasIncomingEdges(line)) {
noIncomingEdges.add(getType(types,lineNumber));
}
lineNumber++;
}
while (!noIncomingEdges.isEmpty()) {
Iterator<ComplexTypeMetadata> iterator=noIncomingEdges.iterator();
ComplexTypeMetadata type=iterator.next();
iterator.remove();
sortedTypes.add(type);
int columnNumber=getId(type,types);
for (int i=0; i < typeNumber; i++) {
int edge=dependencyGraph[i][columnNumber];
if (edge > 0) {
  dependencyGraph[i][columnNumber]-=edge;
  if (!hasIncomingEdges(dependencyGraph[i])) {
    noIncomingEdges.add(getType(types,i));
  }
}
}
}
if (LOGGER.isTraceEnabled()) {
StringBuilder builder=logDependencyMatrix(dependencyGraph);
LOGGER.trace("String_Node_Str" + builder.toString());
}
if (sortedTypes.size() < dependencyGraph.length) {
lineNumber=0;
List<List<ComplexTypeMetadata>> cycles=new LinkedList<List<ComplexTypeMetadata>>();
for (byte[] line : dependencyGraph) {
if (hasIncomingEdges(line)) {
  List<ComplexTypeMetadata> dependencyPath=new LinkedList<ComplexTypeMetadata>();
  int currentLineNumber=lineNumber;
  do {
    ComplexTypeMetadata type=getType(types,currentLineNumber);
    dependencyPath.add(type);
    InboundReferences incomingReferences=new InboundReferences(type);
    Set<ReferenceFieldMetadata> incomingFields=repository.accept(incomingReferences);
    boolean hasMetDependency=false;
    for (    ReferenceFieldMetadata incomingField : incomingFields) {
      ComplexTypeMetadata containingType=repository.getComplexType(incomingField.getEntityTypeName());
      if (containingType != null && containingType != type) {
        int currentDependency=getId(containingType,types);
        if (hasIncomingEdges(dependencyGraph[currentDependency])) {
          dependencyGraph[currentLineNumber][currentDependency]--;
          currentLineNumber=currentDependency;
          hasMetDependency=true;
          break;
        }
      }
    }
    if (!hasMetDependency) {
      break;
    }
  }
 while (currentLineNumber != lineNumber);
  if (dependencyPath.size() >= 1) {
    dependencyPath.add(getType(types,lineNumber));
    cycles.add(dependencyPath);
  }
}
lineNumber++;
}
switch (sortType) {
case STRICT:
if (!cycles.isEmpty()) {
  Iterator<List<ComplexTypeMetadata>> cyclesIterator=cycles.iterator();
  Map<ComplexTypeMetadata,List<FieldMetadata>> cycleHints=new HashMap<ComplexTypeMetadata,List<FieldMetadata>>();
  while (cyclesIterator.hasNext()) {
    Iterator<ComplexTypeMetadata> dependencyPathIterator=cyclesIterator.next().iterator();
    ComplexTypeMetadata previous=null;
    while (dependencyPathIterator.hasNext()) {
      ComplexTypeMetadata currentType=dependencyPathIterator.next();
      ArrayList<FieldMetadata> fields=new ArrayList<FieldMetadata>();
      cycleHints.put(currentType,fields);
      if (previous != null) {
        Set<ReferenceFieldMetadata> inboundReferences=repository.accept(new InboundReferences(currentType));
        for (        ReferenceFieldMetadata inboundReference : inboundReferences) {
          ComplexTypeMetadata entity=repository.getComplexType(inboundReference.getEntityTypeName());
          if (entity != null) {
            fields.add(inboundReference);
          }
        }
      }
      previous=currentType;
    }
  }
  throw new CircularDependencyException(cycleHints);
}
case LENIENT:
for (List<ComplexTypeMetadata> cycle : cycles) {
cycle.remove(cycle.size() - 1);
for (ComplexTypeMetadata cycleElement : cycle) {
  if (!sortedTypes.contains(cycleElement)) {
    sortedTypes.add(cycleElement);
  }
}
}
break;
default :
throw new NotImplementedException("String_Node_Str" + sortType + "String_Node_Str");
}
}
return sortedTypes;
}