Program: 29913
Project: 29913
Buggy source:
private static ArrayList<AggregationGroup> processItems(Context context,TableDefinition definition,ArrayList<Item> items){
  AggregationGroup aggregationGroup=new AggregationGroup(definition);
  TableDefinition tableDef=definition;
  boolean fieldDetected=false;
  ArrayList<AggregationGroup> result=new ArrayList<AggregationGroup>();
  int startPos=-1;
  int lastPos=-1;
  boolean includeList=false;
  for (int i=0; i < items.size(); i++) {
    Item item=items.get(i);
    if (item.item.getPtr() != -1) {
      lastPos=item.item.getPtr() + item.item.getValue().length();
    }
    if (startPos == -1)     startPos=item.item.getPtr();
    if (item.item.getType().equals("String_Node_Str")) {
      SetFilter(aggregationGroup,tableDef,item);
      continue;
    }
    if (item.item.getType().equals("String_Node_Str")) {
      SetFilter(aggregationGroup,tableDef,item);
      continue;
    }
    if (item.item.getType().equals("String_Node_Str")) {
      lastPos=item.item.getPtr();
      if (aggregationGroup.items != null) {
        result.add(aggregationGroup);
        aggregationGroup.text=context.inputString.substring(startPos,item.item.getPtr());
        startPos=item.item.getPtr() + item.item.getValue().length();
        aggregationGroup=new AggregationGroup(definition);
        tableDef=definition;
        fieldDetected=false;
      }
    }
 else     if (item.item.getValue().equals("String_Node_Str")) {
      result.add(aggregationGroup);
      aggregationGroup.text=context.inputString.substring(startPos,item.item.getPtr());
      startPos=item.item.getPtr() + item.item.getValue().length();
      aggregationGroup=new AggregationGroup(definition);
      SetFilter(aggregationGroup,tableDef,item);
      tableDef=definition;
      fieldDetected=false;
    }
 else {
      String type=item.item.getType();
      if (type.equals(SemanticNames.TRUNCATE_VALUE)) {
        aggregationGroup.truncate=item.item.getValue();
        continue;
      }
      if (type.equals(SemanticNames.TRUNCATE_SUBFIELD_VALUE)) {
        aggregationGroup.subField=AggregationGroup.SubField.valueOf(item.item.getValue());
        continue;
      }
      if (type.equals(SemanticNames.TIMEZONEVALUE)) {
        aggregationGroup.timeZone=item.item.getValue().trim();
        char ch=aggregationGroup.timeZone.charAt(0);
        if (ch == '+' || ch == '-') {
          aggregationGroup.timeZone="String_Node_Str" + aggregationGroup.timeZone;
        }
 else         if (Character.getType(ch) == Character.DECIMAL_DIGIT_NUMBER) {
          aggregationGroup.timeZone="String_Node_Str" + aggregationGroup.timeZone;
        }
        String prefix=aggregationGroup.timeZone.substring(0,4);
        String val=aggregationGroup.timeZone.substring(4);
switch (val.length()) {
case 1:
          aggregationGroup.timeZone=prefix + "String_Node_Str" + val+ "String_Node_Str";
        break;
case 2:
      aggregationGroup.timeZone=prefix + val + "String_Node_Str";
    break;
case 3:
  aggregationGroup.timeZone=prefix + "String_Node_Str" + val.substring(0,1)+ "String_Node_Str"+ val.substring(1);
break;
case 4:
if (val.charAt(1) == ':') aggregationGroup.timeZone=prefix + "String_Node_Str" + val;
 else {
aggregationGroup.timeZone=prefix + val.substring(0,2) + "String_Node_Str"+ val.substring(2);
}
break;
case 5:
break;
default :
throw new IllegalArgumentException("String_Node_Str");
}
TimeZone zone=TimeZone.getTimeZone(aggregationGroup.timeZone);
String id=zone.getID();
if (id.compareTo(aggregationGroup.timeZone) != 0) {
throw new IllegalArgumentException("String_Node_Str" + aggregationGroup.timeZone + "String_Node_Str");
}
continue;
}
if (type.equals(SemanticNames.TIMEZONEDISPLAYNAME)) {
aggregationGroup.timeZone=item.item.getValue();
if (!isCorrectTimeZone(aggregationGroup.timeZone)) throw new IllegalArgumentException("String_Node_Str" + aggregationGroup.timeZone + "String_Node_Str");
continue;
}
if (type.equals(SemanticNames.GROUP)) {
continue;
}
if (type.equals(SemanticNames.BATCH_VALUE)) {
if (aggregationGroup.batch == null) aggregationGroup.batch=new ArrayList<Object>();
aggregationGroup.batch.add(item.item.getValue());
continue;
}
if (type.equals(SemanticNames.TOPBOTTOMVALUE)) {
aggregationGroup.selectionValue=Integer.parseInt(item.item.getValue());
continue;
}
if (type.equals(SemanticNames.UPPER)) {
aggregationGroup.tocase=SemanticNames.UPPER;
continue;
}
if (type.equals(SemanticNames.LOWER)) {
aggregationGroup.tocase=SemanticNames.LOWER;
continue;
}
if (type.equals(SemanticNames.TRUNCATE) || type.equals(SemanticNames.BATCH)) {
SetFilter(aggregationGroup,tableDef,item);
continue;
}
if (type.equals(SemanticNames.TERMS)) continue;
if (type.equals(SemanticNames.EXCLUDELIST)) {
includeList=false;
continue;
}
if (type.equals(SemanticNames.INCLUDELIST)) {
includeList=true;
continue;
}
if (type.equals(SemanticNames.EXCLUDE)) {
continue;
}
if (type.equals(SemanticNames.STOPVALUEANY)) {
if (aggregationGroup.stopWords == null) {
aggregationGroup.stopWords=new ArrayList<String>();
}
 else {
throw new IllegalArgumentException("String_Node_Str");
}
continue;
}
if (type.equals(SemanticNames.STOPVALUE)) {
if (aggregationGroup.stopWords == null) {
aggregationGroup.stopWords=new ArrayList<String>();
}
aggregationGroup.stopWords.add(item.item.getValue());
continue;
}
if (type.equals(SemanticNames.EXCLUDEVALUE)) {
String value=item.item.getValue();
if ("String_Node_Str".equals(value)) value=null;
if (includeList) {
if (aggregationGroup.include == null) {
aggregationGroup.include=new ArrayList<String>();
}
aggregationGroup.include.add(value);
continue;
}
 else {
if (aggregationGroup.exclude == null) {
aggregationGroup.exclude=new ArrayList<String>();
}
aggregationGroup.exclude.add(value);
continue;
}
}
if (type.equals(SemanticNames.ALIAS)) {
aggregationGroup.name=item.item.getValue();
continue;
}
if (item.item.getValue().equals("String_Node_Str")) {
continue;
}
if (type.equals(SemanticNames.TOPBOTTOM)) {
if (item.item.getValue().equals("String_Node_Str")) aggregationGroup.selection=AggregationGroup.Selection.Top;
 else aggregationGroup.selection=AggregationGroup.Selection.Bottom;
SetFilter(aggregationGroup,tableDef,item);
continue;
}
AggregationGroupItem ai=new AggregationGroupItem();
if (aggregationGroup.items == null) aggregationGroup.items=new ArrayList<AggregationGroupItem>();
aggregationGroup.items.add(ai);
ai.name=item.item.getValue();
if (tableDef != null) {
FieldDefinition fd=tableDef.getFieldDef(ai.name);
ai.fieldDef=fd;
if (fd == null) throw new IllegalArgumentException("String_Node_Str" + QueryUtils.FullLinkName(aggregationGroup.items));
if (fd.isGroupField()) {
ai.nestedLinks=GetNestedFieldsInfo(fd);
ai.isLink=true;
for (FieldDefinition nestedFieldDef : fd.getNestedFields()) {
if (nestedFieldDef.isGroupField()) {
List<LinkInfo> info=GetNestedFieldsInfo(nestedFieldDef);
if (info.size() == 0) throw new IllegalArgumentException("String_Node_Str" + nestedFieldDef.getName());
nestedFieldDef=info.get(0).fieldDef;
}
if (tableDef.isLinkField(nestedFieldDef.getName())) {
TableDefinition td=tableDef.getLinkExtentTableDef(nestedFieldDef);
if (td == null) {
throw new IllegalArgumentException("String_Node_Str" + QueryUtils.FullLinkName(aggregationGroup.items));
}
tableDef=td;
}
ai.tableDef=tableDef;
break;
}
}
 else if (fd.isLinkField() || fd.isXLinkField()) {
ai.isLink=true;
if (fieldDetected) throw new IllegalArgumentException("String_Node_Str" + QueryUtils.FullLinkName(aggregationGroup.items));
tableDef=tableDef.getLinkExtentTableDef(fd);
if (tableDef == null) {
throw new IllegalArgumentException("String_Node_Str" + QueryUtils.FullLinkName(aggregationGroup.items));
}
}
 else {
if (fieldDetected) throw new IllegalArgumentException("String_Node_Str" + QueryUtils.FullLinkName(aggregationGroup.items));
fieldDetected=true;
}
ai.tableDef=tableDef;
}
if (item.queryItems != null) {
for (int j=0; j < item.queryItems.size(); j++) {
ArrayList<GrammarItem> filterItems=item.queryItems.get(j);
ai.query=CompileQuery(tableDef,ai.query,filterItems);
GrammarItem last=filterItems.get(filterItems.size() - 2);
lastPos=last.getPtr() + last.getValue().length();
}
}
}
}
if (aggregationGroup != null && aggregationGroup.items != null) {
aggregationGroup.text=context.inputString.substring(startPos,lastPos);
result.add(aggregationGroup);
}
for (int i=0; i < result.size(); i++) {
AggregationGroup group=result.get(i);
if (group.batch != null && group.items != null) {
AggregationGroupItem item=group.items.get(group.items.size() - 1);
if (item.fieldDef == null) throw new IllegalArgumentException("String_Node_Str" + item.name);
FieldType itemType=item.fieldDef.getType();
if (itemType == FieldType.GROUP || itemType == FieldType.LINK || itemType == FieldType.XLINK || itemType == FieldType.BINARY || itemType == FieldType.BOOLEAN) throw new IllegalArgumentException("String_Node_Str" + itemType.toString() + "String_Node_Str");
for (int j=0; j < group.batch.size(); j++) {
try {
group.batch.set(j,convert(itemType,(String)group.batch.get(j)));
}
 catch (Exception e) {
throw new IllegalArgumentException("String_Node_Str" + group.batch.get(j) + "String_Node_Str"+ item.fieldDef.getName());
}
}
if (group.batch.size() > 1) {
Object first=group.batch.get(0);
for (int j=1; j < group.batch.size(); j++) {
switch (compareBatchValues(itemType,first,group.batch.get(j))) {
case 0:
throw new IllegalArgumentException("String_Node_Str");
case 1:
throw new IllegalArgumentException("String_Node_Str");
}
first=group.batch.get(j);
}
}
}
if (group.truncate != null && group.items != null) {
AggregationGroupItem item=group.items.get(group.items.size() - 1);
if (item.fieldDef == null) throw new IllegalArgumentException("String_Node_Str" + item.name);
if (!(item.fieldDef.getType() == FieldType.TIMESTAMP)) throw new IllegalArgumentException("String_Node_Str");
}
}
for (int i=0; i < result.size(); i++) {
AggregationGroup group=result.get(i);
group.whereFilter=getWhereQuery(group);
}
return result;
}

Fixed version (ground truth):
private static ArrayList<AggregationGroup> processItems(Context context,TableDefinition definition,ArrayList<Item> items){
  AggregationGroup aggregationGroup=new AggregationGroup(definition);
  TableDefinition tableDef=definition;
  boolean fieldDetected=false;
  ArrayList<AggregationGroup> result=new ArrayList<AggregationGroup>();
  int startPos=-1;
  int lastPos=-1;
  boolean includeList=false;
  for (int i=0; i < items.size(); i++) {
    Item item=items.get(i);
    if (item.item.getPtr() != -1) {
      lastPos=item.item.getPtr() + item.item.getValue().length();
    }
    if (startPos == -1)     startPos=item.item.getPtr();
    if (item.item.getType().equals("String_Node_Str")) {
      SetFilter(aggregationGroup,tableDef,item);
      continue;
    }
    if (item.item.getType().equals("String_Node_Str")) {
      SetFilter(aggregationGroup,tableDef,item);
      continue;
    }
    if (item.item.getType().equals("String_Node_Str")) {
      lastPos=item.item.getPtr();
      if (aggregationGroup.items != null) {
        result.add(aggregationGroup);
        aggregationGroup.text=context.inputString.substring(startPos,item.item.getPtr());
        startPos=item.item.getPtr() + item.item.getValue().length();
        aggregationGroup=new AggregationGroup(definition);
        tableDef=definition;
        fieldDetected=false;
      }
    }
 else     if (item.item.getValue().equals("String_Node_Str")) {
      result.add(aggregationGroup);
      aggregationGroup.text=context.inputString.substring(startPos,item.item.getPtr());
      startPos=item.item.getPtr() + item.item.getValue().length();
      aggregationGroup=new AggregationGroup(definition);
      SetFilter(aggregationGroup,tableDef,item);
      tableDef=definition;
      fieldDetected=false;
    }
 else {
      String type=item.item.getType();
      if (type.equals(SemanticNames.TRUNCATE_VALUE)) {
        aggregationGroup.truncate=item.item.getValue();
        continue;
      }
      if (type.equals(SemanticNames.TRUNCATE_SUBFIELD_VALUE)) {
        aggregationGroup.subField=AggregationGroup.SubField.valueOf(item.item.getValue());
        continue;
      }
      if (type.equals(SemanticNames.TIMEZONEVALUE)) {
        aggregationGroup.timeZone=item.item.getValue().trim();
        char ch=aggregationGroup.timeZone.charAt(0);
        if (ch == '+' || ch == '-') {
          aggregationGroup.timeZone="String_Node_Str" + aggregationGroup.timeZone;
        }
 else         if (Character.getType(ch) == Character.DECIMAL_DIGIT_NUMBER) {
          aggregationGroup.timeZone="String_Node_Str" + aggregationGroup.timeZone;
        }
        String prefix=aggregationGroup.timeZone.substring(0,4);
        String val=aggregationGroup.timeZone.substring(4);
switch (val.length()) {
case 1:
          aggregationGroup.timeZone=prefix + "String_Node_Str" + val+ "String_Node_Str";
        break;
case 2:
      aggregationGroup.timeZone=prefix + val + "String_Node_Str";
    break;
case 3:
  aggregationGroup.timeZone=prefix + "String_Node_Str" + val.substring(0,1)+ "String_Node_Str"+ val.substring(1);
break;
case 4:
if (val.charAt(1) == ':') aggregationGroup.timeZone=prefix + "String_Node_Str" + val;
 else {
aggregationGroup.timeZone=prefix + val.substring(0,2) + "String_Node_Str"+ val.substring(2);
}
break;
case 5:
break;
default :
throw new IllegalArgumentException("String_Node_Str");
}
TimeZone zone=TimeZone.getTimeZone(aggregationGroup.timeZone);
String id=zone.getID();
if (id.compareTo(aggregationGroup.timeZone) != 0) {
throw new IllegalArgumentException("String_Node_Str" + aggregationGroup.timeZone + "String_Node_Str");
}
continue;
}
if (type.equals(SemanticNames.TIMEZONEDISPLAYNAME)) {
aggregationGroup.timeZone=item.item.getValue();
if (!isCorrectTimeZone(aggregationGroup.timeZone)) throw new IllegalArgumentException("String_Node_Str" + aggregationGroup.timeZone + "String_Node_Str");
continue;
}
if (type.equals(SemanticNames.GROUP)) {
continue;
}
if (type.equals(SemanticNames.BATCH_VALUE)) {
if (aggregationGroup.batch == null) aggregationGroup.batch=new ArrayList<Object>();
aggregationGroup.batch.add(item.item.getValue());
continue;
}
if (type.equals(SemanticNames.TOPBOTTOMVALUE)) {
aggregationGroup.selectionValue=Integer.parseInt(item.item.getValue());
continue;
}
if (type.equals(SemanticNames.UPPER)) {
aggregationGroup.tocase=SemanticNames.UPPER;
continue;
}
if (type.equals(SemanticNames.LOWER)) {
aggregationGroup.tocase=SemanticNames.LOWER;
continue;
}
if (type.equals(SemanticNames.TRUNCATE) || type.equals(SemanticNames.BATCH)) {
SetFilter(aggregationGroup,tableDef,item);
continue;
}
if (type.equals(SemanticNames.TERMS)) continue;
if (type.equals(SemanticNames.EXCLUDELIST)) {
includeList=false;
continue;
}
if (type.equals(SemanticNames.INCLUDELIST)) {
includeList=true;
continue;
}
if (type.equals(SemanticNames.EXCLUDE)) {
continue;
}
if (type.equals(SemanticNames.STOPVALUEANY)) {
if (aggregationGroup.stopWords == null) {
aggregationGroup.stopWords=new ArrayList<String>();
}
 else {
throw new IllegalArgumentException("String_Node_Str");
}
continue;
}
if (type.equals(SemanticNames.STOPVALUE)) {
if (aggregationGroup.stopWords == null) {
aggregationGroup.stopWords=new ArrayList<String>();
}
aggregationGroup.stopWords.add(item.item.getValue());
continue;
}
if (type.equals(SemanticNames.EXCLUDEVALUE)) {
String value=item.item.getValue();
if ("String_Node_Str".equals(value)) value=null;
if (includeList) {
if (aggregationGroup.include == null) {
aggregationGroup.include=new ArrayList<String>();
}
aggregationGroup.include.add(value);
continue;
}
 else {
if (aggregationGroup.exclude == null) {
aggregationGroup.exclude=new ArrayList<String>();
}
aggregationGroup.exclude.add(value);
continue;
}
}
if (type.equals(SemanticNames.ALIAS)) {
aggregationGroup.name=item.item.getValue();
continue;
}
if (item.item.getValue().equals("String_Node_Str")) {
continue;
}
if (type.equals(SemanticNames.TOPBOTTOM)) {
if (item.item.getValue().equals("String_Node_Str")) aggregationGroup.selection=AggregationGroup.Selection.Top;
 else aggregationGroup.selection=AggregationGroup.Selection.Bottom;
SetFilter(aggregationGroup,tableDef,item);
continue;
}
AggregationGroupItem ai=new AggregationGroupItem();
if (aggregationGroup.items == null) aggregationGroup.items=new ArrayList<AggregationGroupItem>();
aggregationGroup.items.add(ai);
ai.name=item.item.getValue();
if (tableDef != null) {
FieldDefinition fd=tableDef.getFieldDef(ai.name);
ai.fieldDef=fd;
if (fd == null) throw new IllegalArgumentException("String_Node_Str" + QueryUtils.FullLinkName(aggregationGroup.items));
if (fd.isGroupField()) {
ai.nestedLinks=GetNestedFieldsInfo(fd);
ai.isLink=true;
for (FieldDefinition nestedFieldDef : fd.getNestedFields()) {
if (nestedFieldDef.isGroupField()) {
List<LinkInfo> info=GetNestedFieldsInfo(nestedFieldDef);
if (info.size() == 0) throw new IllegalArgumentException("String_Node_Str" + nestedFieldDef.getName());
nestedFieldDef=info.get(0).fieldDef;
}
if (tableDef.isLinkField(nestedFieldDef.getName()) || nestedFieldDef.isXLinkField()) {
TableDefinition td=tableDef.getLinkExtentTableDef(nestedFieldDef);
if (td == null) {
throw new IllegalArgumentException("String_Node_Str" + QueryUtils.FullLinkName(aggregationGroup.items));
}
tableDef=td;
}
ai.tableDef=tableDef;
break;
}
}
 else if (fd.isLinkField() || fd.isXLinkField()) {
ai.isLink=true;
if (fieldDetected) throw new IllegalArgumentException("String_Node_Str" + QueryUtils.FullLinkName(aggregationGroup.items));
tableDef=tableDef.getLinkExtentTableDef(fd);
if (tableDef == null) {
throw new IllegalArgumentException("String_Node_Str" + QueryUtils.FullLinkName(aggregationGroup.items));
}
}
 else {
if (fieldDetected) throw new IllegalArgumentException("String_Node_Str" + QueryUtils.FullLinkName(aggregationGroup.items));
fieldDetected=true;
}
ai.tableDef=tableDef;
}
if (item.queryItems != null) {
for (int j=0; j < item.queryItems.size(); j++) {
ArrayList<GrammarItem> filterItems=item.queryItems.get(j);
ai.query=CompileQuery(tableDef,ai.query,filterItems);
GrammarItem last=filterItems.get(filterItems.size() - 2);
lastPos=last.getPtr() + last.getValue().length();
}
}
}
}
if (aggregationGroup != null && aggregationGroup.items != null) {
aggregationGroup.text=context.inputString.substring(startPos,lastPos);
result.add(aggregationGroup);
}
for (int i=0; i < result.size(); i++) {
AggregationGroup group=result.get(i);
if (group.batch != null && group.items != null) {
AggregationGroupItem item=group.items.get(group.items.size() - 1);
if (item.fieldDef == null) throw new IllegalArgumentException("String_Node_Str" + item.name);
FieldType itemType=item.fieldDef.getType();
if (itemType == FieldType.GROUP || itemType == FieldType.LINK || itemType == FieldType.XLINK || itemType == FieldType.BINARY || itemType == FieldType.BOOLEAN) throw new IllegalArgumentException("String_Node_Str" + itemType.toString() + "String_Node_Str");
for (int j=0; j < group.batch.size(); j++) {
try {
group.batch.set(j,convert(itemType,(String)group.batch.get(j)));
}
 catch (Exception e) {
throw new IllegalArgumentException("String_Node_Str" + group.batch.get(j) + "String_Node_Str"+ item.fieldDef.getName());
}
}
if (group.batch.size() > 1) {
Object first=group.batch.get(0);
for (int j=1; j < group.batch.size(); j++) {
switch (compareBatchValues(itemType,first,group.batch.get(j))) {
case 0:
throw new IllegalArgumentException("String_Node_Str");
case 1:
throw new IllegalArgumentException("String_Node_Str");
}
first=group.batch.get(j);
}
}
}
if (group.truncate != null && group.items != null) {
AggregationGroupItem item=group.items.get(group.items.size() - 1);
if (item.fieldDef == null) throw new IllegalArgumentException("String_Node_Str" + item.name);
if (!(item.fieldDef.getType() == FieldType.TIMESTAMP)) throw new IllegalArgumentException("String_Node_Str");
}
}
for (int i=0; i < result.size(); i++) {
AggregationGroup group=result.get(i);
group.whereFilter=getWhereQuery(group);
}
return result;
}