Program: 29915
Project: 29915
Buggy source:
private static ArrayList<MetricExpression> BuildMetricsExpression(Context context,TableDefinition definition){
  if (context == null)   throw new IllegalArgumentException("String_Node_Str");
  if (context.items.isEmpty())   return null;
  AggregationMetric metric=new AggregationMetric(definition);
  ArrayList<Item> items=extractMetricTokens(context);
  TableDefinition tableDef=definition;
  boolean fieldDetected=false;
  int ptr=0;
  int endMetricPtr=0;
  ArrayList<MetricExpression> resultList=new ArrayList<MetricExpression>();
  Stack<MetricExpression> expressions=new Stack<MetricExpression>();
  Stack<String> operations=new Stack<String>();
  for (int i=0; i < items.size(); i++) {
    Item item=items.get(i);
    if (item.item.getType().equals("String_Node_Str")) {
      pushOp(item.item.getValue(),expressions,operations);
      continue;
    }
    if (item.item.getType().equals("String_Node_Str")) {
      operations.push(item.item.getValue());
      continue;
    }
    if (item.item.getType().equals("String_Node_Str") && item.item.getValue().equals("String_Node_Str")) {
      String unit2=operations.pop();
      String unit1=operations.pop();
      String unit=operations.pop();
      GregorianCalendar c1, c2;
      try {
        c1=Utils.parseDate(unit1);
      }
 catch (      Exception e) {
        throw new IllegalArgumentException("String_Node_Str" + unit1);
      }
      try {
        c2=Utils.parseDate(unit2);
      }
 catch (      Exception e) {
        throw new IllegalArgumentException("String_Node_Str" + unit2);
      }
      LongIntegerExpression le=new LongIntegerExpression();
      le.value=TimeUtils.getTimeDifference(unit,c1,c2);
      expressions.push(le);
      metric=null;
      continue;
    }
    if (item.item.getType().equals("String_Node_Str")) {
      endMetricPtr=item.item.getPtr();
      continue;
    }
    if (item.item.getType().equals("String_Node_Str")) {
      NumberExpression ne=new NumberExpression();
      try {
        ne.value=Double.parseDouble(item.item.getValue());
      }
 catch (      Exception e) {
        throw new IllegalArgumentException("String_Node_Str" + item.item.getValue() + "String_Node_Str");
      }
      expressions.push(ne);
      metric=null;
      continue;
    }
    if (item.item.getType().equals(SemanticNames.AGGREGATION_METRIC_FUNCTION_NAME)) {
      ptr=item.item.getPtr();
      metric.function=item.item.getValue();
      if (item.queryItems != null) {
        for (int j=0; j < item.queryItems.size(); j++) {
          ArrayList<GrammarItem> filterItems=item.queryItems.get(j);
          metric.filter=CompileQuery(tableDef,metric.filter,filterItems);
        }
      }
      continue;
    }
    if (item.item.getType().equals("String_Node_Str") && item.item.getValue().equals("String_Node_Str")) {
      if (metric != null) {
        expressions.push(metric);
        metric.sourceText=context.inputString.substring(ptr,endMetricPtr);
      }
      tableDef=definition;
      fieldDetected=false;
      metric=new AggregationMetric(definition);
      continue;
    }
    if (item.item.getType().equals(SemanticNames.TRUNCATE_SUBFIELD_VALUE)) {
      metric.subField=AggregationGroup.SubField.valueOf(item.item.getValue());
      continue;
    }
    if (item.item.getType().equals("String_Node_Str") && item.item.getValue().equals("String_Node_Str")) {
      while (!operations.isEmpty()) {
        doOperation(operations.pop(),expressions,operations);
      }
      MetricExpression me=expressions.pop();
      if (!expressions.isEmpty())       throw new IllegalArgumentException("String_Node_Str");
      resultList.add(me);
      tableDef=definition;
      fieldDetected=false;
      metric=new AggregationMetric(definition);
      continue;
    }
    if (fieldDetected)     throw new IllegalArgumentException("String_Node_Str" + QueryUtils.FullLinkName(metric.items));
    AggregationGroupItem ai=new AggregationGroupItem();
    if (metric.items == null)     metric.items=new ArrayList<AggregationGroupItem>();
    metric.items.add(ai);
    ai.name=item.item.getValue();
    if (tableDef != null) {
      FieldDefinition fd=tableDef.getFieldDef(ai.name);
      ai.fieldDef=fd;
      if (fd == null) {
        if (i != (items.size() - 1)) {
          throw new IllegalArgumentException("String_Node_Str" + QueryUtils.FullLinkName(metric.items));
        }
        if (!QueryUtils.isSystemField(ai.name)) {
          throw new IllegalArgumentException("String_Node_Str" + ai.name);
        }
      }
      if (tableDef.isLinkField(ai.name) || fd.isXLinkField()) {
        ai.isLink=true;
        if (fieldDetected)         throw new IllegalArgumentException("String_Node_Str" + QueryUtils.FullLinkName(metric.items));
        tableDef=tableDef.getLinkExtentTableDef(fd);
        if (tableDef == null) {
          throw new IllegalArgumentException("String_Node_Str" + QueryUtils.FullLinkName(metric.items));
        }
      }
 else {
        if (fd != null && fd.getType() == FieldType.GROUP)         throw new IllegalArgumentException("String_Node_Str");
        fieldDetected=true;
      }
      ai.tableDef=tableDef;
    }
    if (item.queryItems != null) {
      if (ai.isLink) {
        for (int j=0; j < item.queryItems.size(); j++) {
          ArrayList<GrammarItem> filterItems=item.queryItems.get(j);
          ai.query=CompileQuery(tableDef,ai.query,filterItems);
        }
      }
 else {
        throw new IllegalArgumentException(ai.name + "String_Node_Str");
      }
    }
  }
  while (!operations.isEmpty()) {
    doOperation(operations.pop(),expressions,operations);
  }
  MetricExpression me=expressions.pop();
  if (!expressions.isEmpty())   throw new IllegalArgumentException("String_Node_Str");
  resultList.add(me);
  return resultList;
}

Fixed version (ground truth):
private static ArrayList<MetricExpression> BuildMetricsExpression(Context context,TableDefinition definition){
  if (context == null)   throw new IllegalArgumentException("String_Node_Str");
  if (context.items.isEmpty())   return null;
  AggregationMetric metric=new AggregationMetric(definition);
  ArrayList<Item> items=extractMetricTokens(context);
  TableDefinition tableDef=definition;
  boolean fieldDetected=false;
  int ptr=0;
  int endMetricPtr=0;
  ArrayList<MetricExpression> resultList=new ArrayList<MetricExpression>();
  Stack<MetricExpression> expressions=new Stack<MetricExpression>();
  Stack<String> operations=new Stack<String>();
  for (int i=0; i < items.size(); i++) {
    Item item=items.get(i);
    if (item.item.getType().equals("String_Node_Str")) {
      pushOp(item.item.getValue(),expressions,operations);
      continue;
    }
    if (item.item.getType().equals("String_Node_Str")) {
      operations.push(item.item.getValue());
      continue;
    }
    if (item.item.getType().equals("String_Node_Str") && item.item.getValue().equals("String_Node_Str")) {
      String unit2=operations.pop();
      String unit1=operations.pop();
      String unit=operations.pop();
      GregorianCalendar c1, c2;
      try {
        c1=Utils.parseDate(unit1);
      }
 catch (      Exception e) {
        throw new IllegalArgumentException("String_Node_Str" + unit1);
      }
      try {
        c2=Utils.parseDate(unit2);
      }
 catch (      Exception e) {
        throw new IllegalArgumentException("String_Node_Str" + unit2);
      }
      LongIntegerExpression le=new LongIntegerExpression();
      le.value=TimeUtils.getTimeDifference(unit,c1,c2);
      expressions.push(le);
      metric=null;
      continue;
    }
    if (item.item.getType().equals("String_Node_Str")) {
      endMetricPtr=item.item.getPtr();
      continue;
    }
    if (item.item.getType().equals("String_Node_Str")) {
      NumberExpression ne=new NumberExpression();
      try {
        ne.value=Double.parseDouble(item.item.getValue());
      }
 catch (      Exception e) {
        throw new IllegalArgumentException("String_Node_Str" + item.item.getValue() + "String_Node_Str");
      }
      expressions.push(ne);
      metric=null;
      continue;
    }
    if (item.item.getType().equals(SemanticNames.AGGREGATION_METRIC_FUNCTION_NAME)) {
      ptr=item.item.getPtr();
      metric.function=item.item.getValue();
      if (item.queryItems != null) {
        for (int j=0; j < item.queryItems.size(); j++) {
          ArrayList<GrammarItem> filterItems=item.queryItems.get(j);
          metric.filter=CompileQuery(tableDef,metric.filter,filterItems);
        }
      }
      continue;
    }
    if (item.item.getType().equals("String_Node_Str") && item.item.getValue().equals("String_Node_Str")) {
      if (metric != null) {
        expressions.push(metric);
        metric.sourceText=context.inputString.substring(ptr,endMetricPtr);
      }
      tableDef=definition;
      fieldDetected=false;
      metric=new AggregationMetric(definition);
      continue;
    }
    if (item.item.getType().equals(SemanticNames.TRUNCATE_SUBFIELD_VALUE)) {
      metric.subField=AggregationGroup.SubField.valueOf(item.item.getValue());
      continue;
    }
    if (item.item.getType().equals("String_Node_Str") && item.item.getValue().equals("String_Node_Str")) {
      while (!operations.isEmpty()) {
        doOperation(operations.pop(),expressions,operations);
      }
      MetricExpression me=expressions.pop();
      if (!expressions.isEmpty())       throw new IllegalArgumentException("String_Node_Str");
      resultList.add(me);
      tableDef=definition;
      fieldDetected=false;
      metric=new AggregationMetric(definition);
      continue;
    }
    if (fieldDetected)     throw new IllegalArgumentException("String_Node_Str" + QueryUtils.FullLinkName(metric.items));
    AggregationGroupItem ai=new AggregationGroupItem();
    if (metric.items == null)     metric.items=new ArrayList<AggregationGroupItem>();
    metric.items.add(ai);
    ai.name=item.item.getValue();
    if (tableDef != null) {
      FieldDefinition fd=tableDef.getFieldDef(ai.name);
      ai.fieldDef=fd;
      if (fd == null) {
        if (i != (items.size() - 1)) {
          throw new IllegalArgumentException("String_Node_Str" + QueryUtils.FullLinkName(metric.items));
        }
        if (!QueryUtils.isSystemField(ai.name)) {
          throw new IllegalArgumentException("String_Node_Str" + ai.name);
        }
      }
      if (tableDef.isLinkField(ai.name) || (fd != null && fd.isXLinkField())) {
        ai.isLink=true;
        if (fieldDetected)         throw new IllegalArgumentException("String_Node_Str" + QueryUtils.FullLinkName(metric.items));
        tableDef=tableDef.getLinkExtentTableDef(fd);
        if (tableDef == null) {
          throw new IllegalArgumentException("String_Node_Str" + QueryUtils.FullLinkName(metric.items));
        }
      }
 else {
        if (fd != null && fd.getType() == FieldType.GROUP)         throw new IllegalArgumentException("String_Node_Str");
        fieldDetected=true;
      }
      ai.tableDef=tableDef;
    }
    if (item.queryItems != null) {
      if (ai.isLink) {
        for (int j=0; j < item.queryItems.size(); j++) {
          ArrayList<GrammarItem> filterItems=item.queryItems.get(j);
          ai.query=CompileQuery(tableDef,ai.query,filterItems);
        }
      }
 else {
        throw new IllegalArgumentException(ai.name + "String_Node_Str");
      }
    }
  }
  while (!operations.isEmpty()) {
    doOperation(operations.pop(),expressions,operations);
  }
  MetricExpression me=expressions.pop();
  if (!expressions.isEmpty())   throw new IllegalArgumentException("String_Node_Str");
  resultList.add(me);
  return resultList;
}