Program: 66013
Project: 66013
Buggy source:
private void populateMeasureDefinitionForCalculateMeasures(ICubeQueryDefinition query) throws DataException, AdapterException {
  List calculatedMeasures=query.getDerivedMeasures();
  if (calculatedMeasures == null || calculatedMeasures.size() == 0)   return;
  List measures=query.getMeasures();
  List measureNameList=new ArrayList();
  for (int i=0; i < measures.size(); i++) {
    measureNameList.add(((IMeasureDefinition)measures.get(i)).getName());
  }
  List derivedMeasureNameList=new ArrayList();
  for (int i=0; i < calculatedMeasures.size(); i++) {
    derivedMeasureNameList.add(((IDerivedMeasureDefinition)calculatedMeasures.get(i)).getName());
  }
  if (this.cubeMetaDataHandleMap != null && this.cubeMetaDataHandleMap.containsKey(query.getName())) {
    CubeHandle cubeHandle=(CubeHandle)this.cubeMetaDataHandleMap.get(query.getName());
    List measureGroups=cubeHandle.getContents(CubeHandle.MEASURE_GROUPS_PROP);
    for (int i=0; i < measureGroups.size(); i++) {
      MeasureGroupHandle mgh=(MeasureGroupHandle)measureGroups.get(i);
      List measureGroup=mgh.getContents(MeasureGroupHandle.MEASURES_PROP);
      for (int j=0; j < measureGroup.size(); j++) {
        MeasureHandle measure=(MeasureHandle)measureGroup.get(j);
        if (measure.isCalculated() && !derivedMeasureNameList.contains(measure.getName())) {
          derivedMeasureNameList.add(measure.getName());
          query.createDerivedMeasure(measure.getName(),DataAdapterUtil.adaptModelDataType(measure.getDataType()),modelAdaptor.adaptExpression((Expression)measure.getExpressionProperty(IMeasureModel.MEASURE_EXPRESSION_PROP).getValue(),ExpressionLocation.CUBE));
        }
      }
    }
  }
  for (int i=0; i < calculatedMeasures.size(); i++) {
    IDerivedMeasureDefinition dmd=(IDerivedMeasureDefinition)calculatedMeasures.get(i);
    List measureNames=ExpressionCompilerUtil.extractColumnExpression(dmd.getExpression(),ExpressionUtil.MEASURE_INDICATOR);
    for (int j=0; j < measureNames.size(); j++) {
      if (!measureNameList.contains(measureNames.get(j).toString()) && !derivedMeasureNameList.contains(measureNames.get(j).toString())) {
        IMeasureDefinition md=query.createMeasure(measureNames.get(j).toString());
        if (this.cubeMetaDataHandleMap != null && this.cubeMetaDataHandleMap.containsKey(query.getName())) {
          CubeHandle cubeHandle=(CubeHandle)this.cubeMetaDataHandleMap.get(query.getName());
          MeasureHandle measureHandle=cubeHandle.getMeasure(measureNames.get(j).toString());
          md.setAggrFunction(DataAdapterUtil.adaptModelAggregationType(measureHandle.getFunction()));
        }
      }
    }
  }
}

Fixed version (ground truth):
private void populateMeasureDefinitionForCalculateMeasures(ICubeQueryDefinition query) throws DataException, AdapterException {
  List calculatedMeasures=query.getDerivedMeasures();
  List measures=query.getMeasures();
  List measureNameList=new ArrayList();
  for (int i=0; i < measures.size(); i++) {
    measureNameList.add(((IMeasureDefinition)measures.get(i)).getName());
  }
  List derivedMeasureNameList=new ArrayList();
  for (int i=0; i < calculatedMeasures.size(); i++) {
    derivedMeasureNameList.add(((IDerivedMeasureDefinition)calculatedMeasures.get(i)).getName());
  }
  if (this.cubeMetaDataHandleMap != null && this.cubeMetaDataHandleMap.containsKey(query.getName())) {
    CubeHandle cubeHandle=(CubeHandle)this.cubeMetaDataHandleMap.get(query.getName());
    List measureGroups=cubeHandle.getContents(CubeHandle.MEASURE_GROUPS_PROP);
    for (int i=0; i < measureGroups.size(); i++) {
      MeasureGroupHandle mgh=(MeasureGroupHandle)measureGroups.get(i);
      List measureGroup=mgh.getContents(MeasureGroupHandle.MEASURES_PROP);
      for (int j=0; j < measureGroup.size(); j++) {
        MeasureHandle measure=(MeasureHandle)measureGroup.get(j);
        if (measure.isCalculated() && !derivedMeasureNameList.contains(measure.getName())) {
          derivedMeasureNameList.add(measure.getName());
          query.createDerivedMeasure(measure.getName(),DataAdapterUtil.adaptModelDataType(measure.getDataType()),modelAdaptor.adaptExpression((Expression)measure.getExpressionProperty(IMeasureModel.MEASURE_EXPRESSION_PROP).getValue(),ExpressionLocation.CUBE));
        }
      }
    }
  }
  calculatedMeasures=query.getDerivedMeasures();
  if (calculatedMeasures == null || calculatedMeasures.size() == 0)   return;
  for (int i=0; i < calculatedMeasures.size(); i++) {
    IDerivedMeasureDefinition dmd=(IDerivedMeasureDefinition)calculatedMeasures.get(i);
    List measureNames=ExpressionCompilerUtil.extractColumnExpression(dmd.getExpression(),ExpressionUtil.MEASURE_INDICATOR);
    for (int j=0; j < measureNames.size(); j++) {
      if (!measureNameList.contains(measureNames.get(j).toString()) && !derivedMeasureNameList.contains(measureNames.get(j).toString())) {
        IMeasureDefinition md=query.createMeasure(measureNames.get(j).toString());
        if (this.cubeMetaDataHandleMap != null && this.cubeMetaDataHandleMap.containsKey(query.getName())) {
          CubeHandle cubeHandle=(CubeHandle)this.cubeMetaDataHandleMap.get(query.getName());
          MeasureHandle measureHandle=cubeHandle.getMeasure(measureNames.get(j).toString());
          md.setAggrFunction(DataAdapterUtil.adaptModelAggregationType(measureHandle.getFunction()));
        }
      }
    }
  }
}