Program: 24086
Project: 24086
Buggy source:
/** 
 * note: snapshot implies that we are generating a resource or a data type; for other profiles, the snapshot is generated elsewhere
 */
private ElementDefinition defineElement(Profile ap,StructureDefinition p,List<ElementDefinition> elements,ElementDefn e,String path,Set<String> slices,List<SliceHandle> parentSlices,SnapShotMode snapshot,boolean root) throws Exception {
  ElementDefinition ce=new ElementDefinition();
  elements.add(ce);
  ce.setPath(path);
  if (e.isXmlAttribute())   ce.addRepresentation(PropertyRepresentation.XMLATTR);
  List<SliceHandle> myParents=new ArrayList<ProfileGenerator.SliceHandle>();
  myParents.addAll(parentSlices);
  if (!Utilities.noString(e.getProfileName())) {
    if (e.getDiscriminator().size() > 0 && !slices.contains(path)) {
      ce.setSlicing(new ElementDefinitionSlicingComponent());
      ce.getSlicing().setDescription(e.getSliceDescription());
      String[] d=e.getDiscriminator().get(0).split("String_Node_Str");
      if (d.length >= 1)       ce.getSlicing().addDiscriminator(d[0].trim());
      if (d.length >= 2)       ce.getSlicing().setOrdered(Boolean.parseBoolean(d[1].trim()));
 else       ce.getSlicing().setOrdered(false);
      if (d.length >= 3)       ce.getSlicing().setRules(SlicingRules.fromCode(d[2].trim()));
 else       ce.getSlicing().setRules(SlicingRules.OPEN);
      for (int i=1; i < e.getDiscriminator().size(); i++) {
        ce.getSlicing().addDiscriminator(e.getDiscriminator().get(i).trim());
      }
      ce=new ElementDefinition();
      elements.add(ce);
      ce.setPath(path);
      slices.add(path);
    }
    SliceHandle hnd=new SliceHandle();
    hnd.name=path;
    myParents.add(hnd);
    ce.setName(e.getProfileName());
  }
  addToPaths(myParents,path,ce,p.getName());
  if (!"String_Node_Str".equals(e.getComments()))   ce.setComments(e.getComments());
  if (!"String_Node_Str".equals(e.getShortDefn()))   ce.setShort(e.getShortDefn());
  if (!"String_Node_Str".equals(e.getDefinition())) {
    ce.setDefinition(e.getDefinition());
    if ("String_Node_Str".equals(e.getShortDefn()))     ce.setShort(e.getDefinition());
  }
  if (!Utilities.noString(e.getRequirements()))   ce.setRequirements(e.getRequirements());
  if (e.hasMustSupport())   ce.setMustSupport(e.isMustSupport());
  if (e.getMaxLength() != null)   ce.setMax(e.getMaxLength());
  if (e.getMinCardinality() != null)   ce.setMin(e.getMinCardinality());
  if (e.getMaxCardinality() != null)   ce.setMax(e.getMaxCardinality() == Integer.MAX_VALUE ? "String_Node_Str" : e.getMaxCardinality().toString());
  if (!root) {
    if (e.typeCode().startsWith("String_Node_Str")) {
      ce.setNameReference(getNameForPath(myParents,e.typeCode().substring(1)));
    }
 else {
      List<TypeRef> expandedTypes=new ArrayList<TypeRef>();
      for (      TypeRef t : e.getTypes()) {
        if (t.hasParams() && !"String_Node_Str".equals(t.getName())) {
          throw new Exception("String_Node_Str" + path + "String_Node_Str"+ p.getName());
        }
        if (t.getParams().size() > 1) {
          if (t.getProfile() != null && t.getParams().size() != 1) {
            throw new Exception("String_Node_Str" + path + "String_Node_Str"+ p.getName());
          }
          for (          String param : t.getParams()) {
            TypeRef childType=new TypeRef(t.getName());
            childType.getParams().add(param);
            childType.getAggregations().addAll(t.getAggregations());
            expandedTypes.add(childType);
          }
        }
 else         if (!t.getName().startsWith("String_Node_Str")) {
          expandedTypes.add(t);
        }
      }
      if (expandedTypes.isEmpty()) {
        if (snapshot != SnapShotMode.None)         ce.addType().setCode(snapshot == SnapShotMode.DataType ? "String_Node_Str" : "String_Node_Str");
      }
 else       for (      TypeRef t : expandedTypes) {
        TypeRefComponent type=new TypeRefComponent();
        type.setCode(t.getName());
        String profile=t.getProfile();
        if (profile == null && t.hasParams()) {
          profile=t.getParams().get(0);
        }
        if (profile != null) {
          if (profile.startsWith("String_Node_Str") || profile.startsWith("String_Node_Str")) {
            type.addProfile(profile);
          }
 else {
            type.addProfile("String_Node_Str" + (profile.equals("String_Node_Str") ? "String_Node_Str" : profile));
          }
        }
        for (        String aggregation : t.getAggregations()) {
          type.addAggregation(AggregationMode.fromCode(aggregation));
        }
        ce.getType().add(type);
      }
    }
  }
  for (  Invariant id : e.getStatedInvariants())   ce.addCondition(id.getId());
  ce.setFixed(e.getFixed());
  ce.setPattern(e.getPattern());
  ce.setDefaultValue(e.getDefaultValue());
  ce.setMeaningWhenMissing(e.getMeaningWhenMissing());
  ce.setExample(e.getExample());
  ce.setRequirements(e.getRequirements());
  for (  String s : e.getAliases())   ce.addAlias(s);
  if (e.hasModifier())   ce.setIsModifier(e.isModifier());
  if (e.hasSummaryItem())   ce.setIsSummaryElement(Factory.newBoolean(e.isSummary()));
  for (  String n : definitions.getMapTypes().keySet()) {
    addMapping(p,ce,n,e.getMapping(n),null);
  }
  if (ap != null) {
    for (    String n : ap.getMappingSpaces().keySet()) {
      addMapping(p,ce,n,e.getMapping(n),ap);
    }
  }
  ToolingExtensions.addDisplayHint(ce,e.getDisplayHint());
  for (  String in : e.getInvariants().keySet()) {
    ElementDefinitionConstraintComponent con=new ElementDefinitionConstraintComponent();
    Invariant inv=e.getInvariants().get(in);
    con.setKey(inv.getId());
    con.setRequirements(inv.getRequirements());
    if (Utilities.noString(inv.getSeverity()))     con.setSeverity(ConstraintSeverity.ERROR);
 else     con.setSeverity(ConstraintSeverity.fromCode(inv.getSeverity()));
    con.setHuman(inv.getEnglish());
    con.setXpath(inv.getXpath());
    ce.getConstraint().add(con);
  }
  if (e.hasBinding()) {
    ce.setBinding(generateBinding(e.getBinding()));
  }
  if (snapshot != SnapShotMode.None && !e.getElements().isEmpty()) {
  }
  Set<String> containedSlices=new HashSet<String>();
  if (snapshot != SnapShotMode.None) {
    if (!root && Utilities.noString(e.typeCode())) {
      if (snapshot == SnapShotMode.Resource)       defineAncestorElements("String_Node_Str",path,snapshot,containedSlices,p,elements);
 else       defineAncestorElements("String_Node_Str",path,snapshot,containedSlices,p,elements);
    }
 else     if (root && !Utilities.noString(e.typeCode()))     defineAncestorElements(e.typeCode(),path,snapshot,containedSlices,p,elements);
  }
  for (  ElementDefn child : e.getElements())   defineElement(ap,p,elements,child,path + "String_Node_Str" + child.getName(),containedSlices,myParents,snapshot,false);
  return ce;
}

Fixed version (ground truth):
/** 
 * note: snapshot implies that we are generating a resource or a data type; for other profiles, the snapshot is generated elsewhere
 */
private ElementDefinition defineElement(Profile ap,StructureDefinition p,List<ElementDefinition> elements,ElementDefn e,String path,Set<String> slices,List<SliceHandle> parentSlices,SnapShotMode snapshot,boolean root) throws Exception {
  ElementDefinition ce=new ElementDefinition();
  elements.add(ce);
  ce.setPath(path);
  if (e.isXmlAttribute())   ce.addRepresentation(PropertyRepresentation.XMLATTR);
  List<SliceHandle> myParents=new ArrayList<ProfileGenerator.SliceHandle>();
  myParents.addAll(parentSlices);
  if (!Utilities.noString(e.getProfileName())) {
    if (e.getDiscriminator().size() > 0 && !slices.contains(path)) {
      ce.setSlicing(new ElementDefinitionSlicingComponent());
      ce.getSlicing().setDescription(e.getSliceDescription());
      String[] d=e.getDiscriminator().get(0).split("String_Node_Str");
      if (d.length >= 1)       ce.getSlicing().addDiscriminator(d[0].trim());
      if (d.length >= 2)       ce.getSlicing().setOrdered(Boolean.parseBoolean(d[1].trim()));
 else       ce.getSlicing().setOrdered(false);
      if (d.length >= 3)       ce.getSlicing().setRules(SlicingRules.fromCode(d[2].trim()));
 else       ce.getSlicing().setRules(SlicingRules.OPEN);
      for (int i=1; i < e.getDiscriminator().size(); i++) {
        ce.getSlicing().addDiscriminator(e.getDiscriminator().get(i).trim());
      }
      ce=new ElementDefinition();
      elements.add(ce);
      ce.setPath(path);
      slices.add(path);
    }
    SliceHandle hnd=new SliceHandle();
    hnd.name=path;
    myParents.add(hnd);
    ce.setName(e.getProfileName());
  }
  addToPaths(myParents,path,ce,p.getName());
  if (!!Utilities.noString(e.getComments()))   ce.setComments(e.getComments());
  if (!Utilities.noString(e.getShortDefn()))   ce.setShort(e.getShortDefn());
  if (!Utilities.noString(e.getDefinition())) {
    ce.setDefinition(e.getDefinition());
    if (!Utilities.noString(e.getShortDefn()))     ce.setShort(e.getDefinition());
  }
  if (!Utilities.noString(e.getRequirements()))   ce.setRequirements(e.getRequirements());
  if (e.hasMustSupport())   ce.setMustSupport(e.isMustSupport());
  if (e.getMaxLength() != null)   ce.setMax(e.getMaxLength());
  if (e.getMinCardinality() != null)   ce.setMin(e.getMinCardinality());
  if (e.getMaxCardinality() != null)   ce.setMax(e.getMaxCardinality() == Integer.MAX_VALUE ? "String_Node_Str" : e.getMaxCardinality().toString());
  if (!root) {
    if (e.typeCode().startsWith("String_Node_Str")) {
      ce.setNameReference(getNameForPath(myParents,e.typeCode().substring(1)));
    }
 else {
      List<TypeRef> expandedTypes=new ArrayList<TypeRef>();
      for (      TypeRef t : e.getTypes()) {
        if (t.hasParams() && !"String_Node_Str".equals(t.getName())) {
          throw new Exception("String_Node_Str" + path + "String_Node_Str"+ p.getName());
        }
        if (t.getParams().size() > 1) {
          if (t.getProfile() != null && t.getParams().size() != 1) {
            throw new Exception("String_Node_Str" + path + "String_Node_Str"+ p.getName());
          }
          for (          String param : t.getParams()) {
            TypeRef childType=new TypeRef(t.getName());
            childType.getParams().add(param);
            childType.getAggregations().addAll(t.getAggregations());
            expandedTypes.add(childType);
          }
        }
 else         if (!t.getName().startsWith("String_Node_Str")) {
          expandedTypes.add(t);
        }
      }
      if (expandedTypes.isEmpty()) {
        if (snapshot != SnapShotMode.None)         ce.addType().setCode(snapshot == SnapShotMode.DataType ? "String_Node_Str" : "String_Node_Str");
      }
 else       for (      TypeRef t : expandedTypes) {
        TypeRefComponent type=new TypeRefComponent();
        type.setCode(t.getName());
        String profile=t.getProfile();
        if (profile == null && t.hasParams()) {
          profile=t.getParams().get(0);
        }
        if (profile != null) {
          if (profile.startsWith("String_Node_Str") || profile.startsWith("String_Node_Str")) {
            type.addProfile(profile);
          }
 else {
            type.addProfile("String_Node_Str" + (profile.equals("String_Node_Str") ? "String_Node_Str" : profile));
          }
        }
        for (        String aggregation : t.getAggregations()) {
          type.addAggregation(AggregationMode.fromCode(aggregation));
        }
        ce.getType().add(type);
      }
    }
  }
  for (  Invariant id : e.getStatedInvariants())   ce.addCondition(id.getId());
  ce.setFixed(e.getFixed());
  ce.setPattern(e.getPattern());
  ce.setDefaultValue(e.getDefaultValue());
  ce.setMeaningWhenMissing(e.getMeaningWhenMissing());
  ce.setExample(e.getExample());
  ce.setRequirements(e.getRequirements());
  for (  String s : e.getAliases())   ce.addAlias(s);
  if (e.hasModifier())   ce.setIsModifier(e.isModifier());
  if (e.hasSummaryItem())   ce.setIsSummaryElement(Factory.newBoolean(e.isSummary()));
  for (  String n : definitions.getMapTypes().keySet()) {
    addMapping(p,ce,n,e.getMapping(n),null);
  }
  if (ap != null) {
    for (    String n : ap.getMappingSpaces().keySet()) {
      addMapping(p,ce,n,e.getMapping(n),ap);
    }
  }
  ToolingExtensions.addDisplayHint(ce,e.getDisplayHint());
  for (  String in : e.getInvariants().keySet()) {
    ElementDefinitionConstraintComponent con=new ElementDefinitionConstraintComponent();
    Invariant inv=e.getInvariants().get(in);
    con.setKey(inv.getId());
    con.setRequirements(inv.getRequirements());
    if (Utilities.noString(inv.getSeverity()))     con.setSeverity(ConstraintSeverity.ERROR);
 else     con.setSeverity(ConstraintSeverity.fromCode(inv.getSeverity()));
    con.setHuman(inv.getEnglish());
    con.setXpath(inv.getXpath());
    ce.getConstraint().add(con);
  }
  if (e.hasBinding()) {
    ce.setBinding(generateBinding(e.getBinding()));
  }
  if (snapshot != SnapShotMode.None && !e.getElements().isEmpty()) {
  }
  Set<String> containedSlices=new HashSet<String>();
  if (snapshot != SnapShotMode.None) {
    if (!root && Utilities.noString(e.typeCode())) {
      if (snapshot == SnapShotMode.Resource)       defineAncestorElements("String_Node_Str",path,snapshot,containedSlices,p,elements);
 else       defineAncestorElements("String_Node_Str",path,snapshot,containedSlices,p,elements);
    }
 else     if (root && !Utilities.noString(e.typeCode()))     defineAncestorElements(e.typeCode(),path,snapshot,containedSlices,p,elements);
  }
  for (  ElementDefn child : e.getElements())   defineElement(ap,p,elements,child,path + "String_Node_Str" + child.getName(),containedSlices,myParents,snapshot,false);
  return ce;
}