Program: 23354
Project: 23354
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) {
    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")) {
      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.hasTargetProfile() && t.getTargetProfile().startsWith("String_Node_Str")) {
        StructureDefinition sd=context.fetchResource(StructureDefinition.class,t.getTargetProfile());
        if (sd != null) {
          String disp=sd.hasTitle() ? sd.getTitle() : sd.getName();
          c.addPiece(checkForNoChange(t,gen.new Piece(checkPrepend(corePath,sd.getUserString("String_Node_Str")),disp,null)));
        }
 else {
          String rn=t.getTargetProfile().substring(40);
          c.addPiece(checkForNoChange(t,gen.new Piece(pkp.getLinkFor(corePath,rn),rn,null)));
        }
      }
 else       if (t.hasTargetProfile() && Utilities.isAbsoluteUrl(t.getTargetProfile())) {
        StructureDefinition sd=context.fetchResource(StructureDefinition.class,t.getTargetProfile());
        if (sd != null) {
          String disp=sd.hasTitle() ? sd.getTitle() : 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.getTargetProfile(),null)));
      }
 else       if (t.hasProfile() && t.getTargetProfile().startsWith("String_Node_Str"))       c.addPiece(checkForNoChange(t,gen.new Piece(corePath + profileBaseFileName + "String_Node_Str"+ t.getTargetProfile().substring(1).toLowerCase()+ "String_Node_Str",t.getTargetProfile(),null)));
 else       if (t.hasTargetProfile())       c.addPiece(checkForNoChange(t,gen.new Piece(corePath + t.getTargetProfile(),t.getTargetProfile(),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((t.getProfile().startsWith(corePath) ? corePath : "String_Node_Str") + parts[0],parts[1],t.getCode())));
      }
 else       c.addPiece(checkForNoChange(t,gen.new Piece((t.getProfile().startsWith(corePath) ? corePath : "String_Node_Str") + 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) {
    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")) {
      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.hasTargetProfile() && t.getTargetProfile().startsWith("String_Node_Str")) {
        StructureDefinition sd=context.fetchResource(StructureDefinition.class,t.getTargetProfile());
        if (sd != null) {
          String disp=sd.hasTitle() ? sd.getTitle() : sd.getName();
          c.addPiece(checkForNoChange(t,gen.new Piece(checkPrepend(corePath,sd.getUserString("String_Node_Str")),disp,null)));
        }
 else {
          String rn=t.getTargetProfile().substring(40);
          c.addPiece(checkForNoChange(t,gen.new Piece(pkp.getLinkFor(corePath,rn),rn,null)));
        }
      }
 else       if (t.hasTargetProfile() && Utilities.isAbsoluteUrl(t.getTargetProfile())) {
        StructureDefinition sd=context.fetchResource(StructureDefinition.class,t.getTargetProfile());
        if (sd != null) {
          String disp=sd.hasTitle() ? sd.getTitle() : 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.getTargetProfile(),null)));
      }
 else       if (t.hasTargetProfile() && t.getTargetProfile().startsWith("String_Node_Str"))       c.addPiece(checkForNoChange(t,gen.new Piece(corePath + profileBaseFileName + "String_Node_Str"+ t.getTargetProfile().substring(1).toLowerCase()+ "String_Node_Str",t.getTargetProfile(),null)));
 else       if (t.hasTargetProfile())       c.addPiece(checkForNoChange(t,gen.new Piece(corePath + t.getTargetProfile(),t.getTargetProfile(),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((t.getProfile().startsWith(corePath) ? corePath : "String_Node_Str") + parts[0],parts[1],t.getCode())));
      }
 else       c.addPiece(checkForNoChange(t,gen.new Piece((t.getProfile().startsWith(corePath) ? corePath : "String_Node_Str") + 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;
}