Program: 23953
Project: 23953
Buggy source:
private Cell genTypes(HierarchicalTableGenerator gen,Row r,ElementDefinition e,String profileBaseFileName,StructureDefinition profile,String corePath,String imagePath){
  Cell c=gen.new Cell();
  r.getCells().add(c);
  List<TypeRefComponent> types=e.getType();
  if (!e.hasType()) {
    if (e.hasContentReference()) {
      return c;
    }
 else {
      ElementDefinition d=(ElementDefinition)e.getUserData(DERIVATION_POINTER);
      if (d != null && d.hasType()) {
        types=new ArrayList<ElementDefinition.TypeRefComponent>();
        for (        TypeRefComponent tr : d.getType()) {
          TypeRefComponent tt=tr.copy();
          tt.setUserData(DERIVATION_EQUALS,true);
          types.add(tt);
        }
      }
 else       return c;
    }
  }
  boolean first=true;
  Element source=types.get(0);
  int aggMode=AGG_NONE;
  boolean allReference=!types.isEmpty();
  Set<AggregationMode> aggs=new HashSet<ElementDefinition.AggregationMode>();
  for (  TypeRefComponent t : types) {
    if (t.getCode().equals("String_Node_Str") && t.hasProfile()) {
      for (      Enumeration<AggregationMode> en : t.getAggregation())       aggs.add(en.getValue());
    }
 else     allReference=false;
  }
  if (allReference) {
    c.getPieces().add(gen.new Piece(corePath + "String_Node_Str","String_Node_Str",null));
    c.getPieces().add(gen.new Piece(null,"String_Node_Str",null));
    if (aggs.size() > 0) {
      boolean allSame=true;
      for (      TypeRefComponent t : types) {
        for (        AggregationMode agg : aggs) {
          boolean found=false;
          for (          Enumeration<AggregationMode> en : t.getAggregation())           if (en.getValue() == agg)           found=true;
          if (!found)           allSame=false;
        }
      }
      aggMode=allSame ? AGG_GR : AGG_IND;
      if (aggMode != AGG_GR)       allReference=false;
    }
  }
 else   aggMode=aggs.size() == 0 ? AGG_NONE : AGG_IND;
  if (allReference) {
    c.getPieces().add(gen.new Piece(corePath + "String_Node_Str","String_Node_Str",null));
    c.getPieces().add(gen.new Piece(null,"String_Node_Str",null));
  }
  TypeRefComponent tl=null;
  for (  TypeRefComponent t : types) {
    if (first)     first=false;
 else     if (allReference)     c.addPiece(checkForNoChange(tl,gen.new Piece(null,"String_Node_Str",null)));
 else     c.addPiece(checkForNoChange(tl,gen.new Piece(null,"String_Node_Str",null)));
    tl=t;
    if (t.getCode().equals("String_Node_Str") || (t.getCode().equals("String_Node_Str") && t.hasProfile())) {
      if (!allReference) {
        c.getPieces().add(gen.new Piece(corePath + "String_Node_Str","String_Node_Str",null));
        c.getPieces().add(gen.new Piece(null,"String_Node_Str",null));
      }
      if (t.hasProfile() && t.getProfile().startsWith("String_Node_Str")) {
        StructureDefinition sd=context.fetchResource(StructureDefinition.class,t.getProfile());
        if (sd != null) {
          String disp=sd.hasDisplay() ? sd.getDisplay() : sd.getName();
          c.addPiece(checkForNoChange(t,gen.new Piece(checkPrepend(corePath,sd.getUserString("String_Node_Str")),disp,null)));
        }
 else {
          String rn=t.getProfile().substring(40);
          c.addPiece(checkForNoChange(t,gen.new Piece(pkp.getLinkFor(corePath,rn),rn,null)));
        }
      }
 else       if (t.hasProfile() && Utilities.isAbsoluteUrl(t.getProfile())) {
        StructureDefinition sd=context.fetchResource(StructureDefinition.class,t.getProfile());
        if (sd != null) {
          String disp=sd.hasDisplay() ? sd.getDisplay() : sd.getName();
          String ref=pkp.getLinkForProfile(null,sd.getUrl());
          if (ref.contains("String_Node_Str"))           ref=ref.substring(0,ref.indexOf("String_Node_Str"));
          c.addPiece(checkForNoChange(t,gen.new Piece(ref,disp,null)));
        }
 else         c.addPiece(checkForNoChange(t,gen.new Piece(null,t.getProfile(),null)));
      }
 else       if (t.hasProfile() && t.getProfile().startsWith("String_Node_Str"))       c.addPiece(checkForNoChange(t,gen.new Piece(corePath + profileBaseFileName + "String_Node_Str"+ t.getProfile().substring(1).toLowerCase()+ "String_Node_Str",t.getProfile(),null)));
 else       c.addPiece(checkForNoChange(t,gen.new Piece(corePath + t.getProfile(),t.getProfile(),null)));
      if (!allReference) {
        c.getPieces().add(gen.new Piece(null,"String_Node_Str",null));
        if (t.getAggregation().size() > 0) {
          c.getPieces().add(gen.new Piece(corePath + "String_Node_Str","String_Node_Str",null));
          boolean firstA=true;
          for (          Enumeration<AggregationMode> a : t.getAggregation()) {
            if (firstA=true)             firstA=false;
 else             c.getPieces().add(gen.new Piece(corePath + "String_Node_Str","String_Node_Str",null));
            c.getPieces().add(gen.new Piece(corePath + "String_Node_Str",codeForAggregation(a.getValue()),null));
          }
          c.getPieces().add(gen.new Piece(corePath + "String_Node_Str","String_Node_Str",null));
        }
      }
    }
 else     if (t.hasProfile()) {
      String ref;
      ref=pkp.getLinkForProfile(profile,t.getProfile());
      if (ref != null) {
        String[] parts=ref.split("String_Node_Str");
        c.addPiece(checkForNoChange(t,gen.new Piece(corePath + parts[0],parts[1],t.getCode())));
      }
 else       c.addPiece(checkForNoChange(t,gen.new Piece(corePath + ref,t.getCode(),null)));
    }
 else     if (pkp.hasLinkFor(t.getCode())) {
      c.addPiece(checkForNoChange(t,gen.new Piece(pkp.getLinkFor(corePath,t.getCode()),t.getCode(),null)));
    }
 else     c.addPiece(checkForNoChange(t,gen.new Piece(null,t.getCode(),null)));
  }
  if (allReference) {
    c.getPieces().add(gen.new Piece(null,"String_Node_Str",null));
    if (aggs.size() > 0) {
      c.getPieces().add(gen.new Piece(corePath + "String_Node_Str","String_Node_Str",null));
      boolean firstA=true;
      for (      AggregationMode a : aggs) {
        if (firstA=true)         firstA=false;
 else         c.getPieces().add(gen.new Piece(corePath + "String_Node_Str","String_Node_Str",null));
        c.getPieces().add(gen.new Piece(corePath + "String_Node_Str",codeForAggregation(a),null));
      }
      c.getPieces().add(gen.new Piece(corePath + "String_Node_Str","String_Node_Str",null));
    }
  }
  return c;
}

Fixed version (ground truth):
private Cell genTypes(HierarchicalTableGenerator gen,Row r,ElementDefinition e,String profileBaseFileName,StructureDefinition profile,String corePath,String imagePath){
  Cell c=gen.new Cell();
  r.getCells().add(c);
  List<TypeRefComponent> types=e.getType();
  if (!e.hasType()) {
    if (e.hasContentReference()) {
      return c;
    }
 else {
      ElementDefinition d=(ElementDefinition)e.getUserData(DERIVATION_POINTER);
      if (d != null && d.hasType()) {
        types=new ArrayList<ElementDefinition.TypeRefComponent>();
        for (        TypeRefComponent tr : d.getType()) {
          TypeRefComponent tt=tr.copy();
          tt.setUserData(DERIVATION_EQUALS,true);
          types.add(tt);
        }
      }
 else       return c;
    }
  }
  boolean first=true;
  Element source=types.get(0);
  int aggMode=AGG_NONE;
  boolean allReference=!types.isEmpty();
  Set<AggregationMode> aggs=new HashSet<ElementDefinition.AggregationMode>();
  for (  TypeRefComponent t : types) {
    if (t.getCode().equals("String_Node_Str") && t.hasProfile()) {
      for (      Enumeration<AggregationMode> en : t.getAggregation())       aggs.add(en.getValue());
    }
 else     allReference=false;
  }
  if (allReference) {
    c.getPieces().add(gen.new Piece(corePath + "String_Node_Str","String_Node_Str",null));
    c.getPieces().add(gen.new Piece(null,"String_Node_Str",null));
    if (aggs.size() > 0) {
      boolean allSame=true;
      for (      TypeRefComponent t : types) {
        for (        AggregationMode agg : aggs) {
          boolean found=false;
          for (          Enumeration<AggregationMode> en : t.getAggregation())           if (en.getValue() == agg)           found=true;
          if (!found)           allSame=false;
        }
      }
      aggMode=allSame ? AGG_GR : AGG_IND;
      if (aggMode != AGG_GR)       allReference=false;
    }
  }
 else   aggMode=aggs.size() == 0 ? AGG_NONE : AGG_IND;
  if (allReference) {
    c.getPieces().add(gen.new Piece(corePath + "String_Node_Str","String_Node_Str",null));
    c.getPieces().add(gen.new Piece(null,"String_Node_Str",null));
  }
  TypeRefComponent tl=null;
  for (  TypeRefComponent t : types) {
    if (first)     first=false;
 else     if (allReference)     c.addPiece(checkForNoChange(tl,gen.new Piece(null,"String_Node_Str",null)));
 else     c.addPiece(checkForNoChange(tl,gen.new Piece(null,"String_Node_Str",null)));
    tl=t;
    if (t.getCode().equals("String_Node_Str") || (t.getCode().equals("String_Node_Str") && t.hasProfile())) {
      if (!allReference) {
        c.getPieces().add(gen.new Piece(corePath + "String_Node_Str","String_Node_Str",null));
        c.getPieces().add(gen.new Piece(null,"String_Node_Str",null));
      }
      if (t.hasProfile() && t.getProfile().startsWith("String_Node_Str")) {
        StructureDefinition sd=context.fetchResource(StructureDefinition.class,t.getProfile());
        if (sd != null) {
          String disp=sd.hasDisplay() ? sd.getDisplay() : sd.getName();
          c.addPiece(checkForNoChange(t,gen.new Piece(checkPrepend(corePath,sd.getUserString("String_Node_Str")),disp,null)));
        }
 else {
          String rn=t.getProfile().substring(40);
          c.addPiece(checkForNoChange(t,gen.new Piece(pkp.getLinkFor(corePath,rn),rn,null)));
        }
      }
 else       if (t.hasProfile() && Utilities.isAbsoluteUrl(t.getProfile())) {
        StructureDefinition sd=context.fetchResource(StructureDefinition.class,t.getProfile());
        if (sd != null) {
          String disp=sd.hasDisplay() ? sd.getDisplay() : sd.getName();
          String ref=pkp.getLinkForProfile(null,sd.getUrl());
          if (ref.contains("String_Node_Str"))           ref=ref.substring(0,ref.indexOf("String_Node_Str"));
          c.addPiece(checkForNoChange(t,gen.new Piece(ref,disp,null)));
        }
 else         c.addPiece(checkForNoChange(t,gen.new Piece(null,t.getProfile(),null)));
      }
 else       if (t.hasProfile() && t.getProfile().startsWith("String_Node_Str"))       c.addPiece(checkForNoChange(t,gen.new Piece(corePath + profileBaseFileName + "String_Node_Str"+ t.getProfile().substring(1).toLowerCase()+ "String_Node_Str",t.getProfile(),null)));
 else       if (t.hasProfile())       c.addPiece(checkForNoChange(t,gen.new Piece(corePath + t.getProfile(),t.getProfile(),null)));
      if (!allReference) {
        c.getPieces().add(gen.new Piece(null,"String_Node_Str",null));
        if (t.getAggregation().size() > 0) {
          c.getPieces().add(gen.new Piece(corePath + "String_Node_Str","String_Node_Str",null));
          boolean firstA=true;
          for (          Enumeration<AggregationMode> a : t.getAggregation()) {
            if (firstA=true)             firstA=false;
 else             c.getPieces().add(gen.new Piece(corePath + "String_Node_Str","String_Node_Str",null));
            c.getPieces().add(gen.new Piece(corePath + "String_Node_Str",codeForAggregation(a.getValue()),null));
          }
          c.getPieces().add(gen.new Piece(corePath + "String_Node_Str","String_Node_Str",null));
        }
      }
    }
 else     if (t.hasProfile()) {
      String ref;
      ref=pkp.getLinkForProfile(profile,t.getProfile());
      if (ref != null) {
        String[] parts=ref.split("String_Node_Str");
        if (parts[0].startsWith("String_Node_Str") || parts[0].startsWith("String_Node_Str"))         c.addPiece(checkForNoChange(t,gen.new Piece(parts[0],parts[1],t.getCode())));
 else         c.addPiece(checkForNoChange(t,gen.new Piece(corePath + parts[0],parts[1],t.getCode())));
      }
 else       c.addPiece(checkForNoChange(t,gen.new Piece(corePath + ref,t.getCode(),null)));
    }
 else     if (pkp.hasLinkFor(t.getCode())) {
      c.addPiece(checkForNoChange(t,gen.new Piece(pkp.getLinkFor(corePath,t.getCode()),t.getCode(),null)));
    }
 else     c.addPiece(checkForNoChange(t,gen.new Piece(null,t.getCode(),null)));
  }
  if (allReference) {
    c.getPieces().add(gen.new Piece(null,"String_Node_Str",null));
    if (aggs.size() > 0) {
      c.getPieces().add(gen.new Piece(corePath + "String_Node_Str","String_Node_Str",null));
      boolean firstA=true;
      for (      AggregationMode a : aggs) {
        if (firstA=true)         firstA=false;
 else         c.getPieces().add(gen.new Piece(corePath + "String_Node_Str","String_Node_Str",null));
        c.getPieces().add(gen.new Piece(corePath + "String_Node_Str",codeForAggregation(a),null));
      }
      c.getPieces().add(gen.new Piece(corePath + "String_Node_Str","String_Node_Str",null));
    }
  }
  return c;
}