Program: 129927
Project: 129927
Buggy source:
/** 
 * DOC cantoine. Method to return a Collection of Tables for a DB connection.
 * @param DatabaseMetaData dbMetaData
 * @return Collection of MetadataTable
 */
public static List<IMetadataTable> extractTablesFromDB(DatabaseMetaData dbMetaData,IMetadataConnection iMetadataConnection,int... limit){
  String schema=iMetadataConnection.getSchema();
  final boolean as400=EDatabaseTypeName.AS400.getProduct().equals(iMetadataConnection.getProduct());
  if (schema == null || "String_Node_Str".equals(schema)) {
    if (EDatabaseTypeName.TERADATA.getProduct().equals(iMetadataConnection.getProduct())) {
      schema=iMetadataConnection.getDatabase();
    }
 else     if (EDatabaseTypeName.IBMDB2.getProduct().equals(iMetadataConnection.getProduct())) {
      String username=iMetadataConnection.getUsername();
      if (username != null) {
        schema=username.toUpperCase();
        iMetadataConnection.setSchema(schema);
      }
    }
 else     if (as400) {
      schema=ExtractMetaDataUtils.retrieveSchemaPatternForAS400(iMetadataConnection.getUrl());
    }
  }
  if (dbMetaData.equals(oldMetadata) && schema.equals(oldSchema) && limit.equals(oldLimit)&& (oldUseAllSynonyms == ExtractMetaDataUtils.isUseAllSynonyms())) {
    return oldMetadataRetrieved;
  }
  List<String> tablesToFilter=new ArrayList<String>();
  if (EDatabaseTypeName.ORACLEFORSID.getProduct().equals(iMetadataConnection.getProduct())) {
    Statement stmt;
    try {
      stmt=ExtractMetaDataUtils.conn.createStatement();
      ExtractMetaDataUtils.setQueryStatementTimeout(stmt);
      if (iMetadataConnection.getDbVersionString() != null && !iMetadataConnection.getDbVersionString().equals("String_Node_Str")) {
        ResultSet rsTables=stmt.executeQuery(TableInfoParameters.ORACLE_10G_RECBIN_SQL);
        tablesToFilter=getTableNamesFromQuery(rsTables);
        rsTables.close();
      }
      stmt.close();
    }
 catch (    SQLException e) {
      ExceptionHandler.process(e);
    }
  }
 else {
    ExtractMetaDataUtils.setUseAllSynonyms(false);
  }
  List<IMetadataTable> medataTables=new ArrayList<IMetadataTable>();
  try {
    ResultSet rsTables=null, rsTableTypes=null;
    if (!ExtractMetaDataUtils.isUseAllSynonyms()) {
      rsTableTypes=dbMetaData.getTableTypes();
      Set<String> availableTableTypes=new HashSet<String>();
      String[] neededTableTypes={ETableTypes.TABLETYPE_TABLE.getName(),ETableTypes.TABLETYPE_VIEW.getName(),ETableTypes.TABLETYPE_SYNONYM.getName()};
      while (rsTableTypes.next()) {
        String currentTableType=StringUtils.trimToEmpty(rsTableTypes.getString("String_Node_Str"));
        if ("String_Node_Str".equalsIgnoreCase(currentTableType)) {
          currentTableType=ETableTypes.TABLETYPE_TABLE.getName();
        }
        if (ArrayUtils.contains(neededTableTypes,currentTableType)) {
          availableTableTypes.add(currentTableType);
        }
      }
      rsTableTypes.close();
      if (dbMetaData.supportsSchemasInTableDefinitions() && schema != null && !schema.equals("String_Node_Str")) {
        if (as400) {
          String[] multiSchems=ExtractMetaDataUtils.getMultiSchems(schema);
          if (multiSchems != null) {
            for (            String s : multiSchems) {
              rsTables=dbMetaData.getTables(null,s.trim(),null,availableTableTypes.toArray(new String[]{}));
              getMetadataTables(medataTables,rsTables,dbMetaData.supportsSchemasInTableDefinitions(),tablesToFilter,limit);
              rsTables.close();
            }
            rsTables=null;
          }
        }
 else {
          rsTables=dbMetaData.getTables(null,schema,null,availableTableTypes.toArray(new String[]{}));
        }
      }
 else       if (schema != null && !schema.equals("String_Node_Str")) {
        rsTables=dbMetaData.getTables(null,schema,null,availableTableTypes.toArray(new String[]{}));
      }
 else {
        rsTables=dbMetaData.getTables(null,null,null,availableTableTypes.toArray(new String[]{}));
      }
    }
 else {
      String sql=GET_ALL_SYNONYMS;
      Statement stmt=ExtractMetaDataUtils.conn.createStatement();
      ExtractMetaDataUtils.setQueryStatementTimeout(stmt);
      rsTables=stmt.executeQuery(sql);
    }
    if (rsTables != null) {
      getMetadataTables(medataTables,rsTables,dbMetaData.supportsSchemasInTableDefinitions(),tablesToFilter,limit);
      rsTables.close();
    }
  }
 catch (  SQLException e) {
    ExceptionHandler.process(e);
    log.error(e.toString());
    throw new RuntimeException(e);
  }
catch (  Exception e) {
    ExceptionHandler.process(e);
    log.error(e.toString());
    throw new RuntimeException(e);
  }
  oldMetadata=dbMetaData;
  oldSchema=schema;
  oldLimit=limit;
  oldUseAllSynonyms=ExtractMetaDataUtils.isUseAllSynonyms();
  oldMetadataRetrieved=medataTables;
  return medataTables;
}

Fixed version (ground truth):
/** 
 * DOC cantoine. Method to return a Collection of Tables for a DB connection.
 * @param DatabaseMetaData dbMetaData
 * @return Collection of MetadataTable
 */
public static List<IMetadataTable> extractTablesFromDB(DatabaseMetaData dbMetaData,IMetadataConnection iMetadataConnection,int... limit){
  String schema=iMetadataConnection.getSchema();
  final boolean as400=EDatabaseTypeName.AS400.getProduct().equals(iMetadataConnection.getProduct());
  if (schema == null || "String_Node_Str".equals(schema)) {
    if (EDatabaseTypeName.TERADATA.getProduct().equals(iMetadataConnection.getProduct())) {
      schema=iMetadataConnection.getDatabase();
    }
 else     if (EDatabaseTypeName.IBMDB2.getProduct().equals(iMetadataConnection.getProduct())) {
      String username=iMetadataConnection.getUsername();
      if (username != null) {
        schema=username.toUpperCase();
        iMetadataConnection.setSchema(schema);
      }
    }
 else     if (as400) {
      schema=ExtractMetaDataUtils.retrieveSchemaPatternForAS400(iMetadataConnection.getUrl());
    }
  }
  if (dbMetaData.equals(oldMetadata) && schema.equals(oldSchema) && limit.equals(oldLimit)&& (oldUseAllSynonyms == ExtractMetaDataUtils.isUseAllSynonyms())) {
    return oldMetadataRetrieved;
  }
  List<String> tablesToFilter=new ArrayList<String>();
  if (EDatabaseTypeName.ORACLEFORSID.getDisplayName().equals(iMetadataConnection.getDbType()) || EDatabaseTypeName.ORACLESN.getDisplayName().equals(iMetadataConnection.getDbType()) || EDatabaseTypeName.ORACLE_RAC.getDisplayName().equals(iMetadataConnection.getDbType())|| EDatabaseTypeName.ORACLE_OCI.getDisplayName().equals(iMetadataConnection.getDbType())) {
    Statement stmt;
    try {
      stmt=ExtractMetaDataUtils.conn.createStatement();
      ExtractMetaDataUtils.setQueryStatementTimeout(stmt);
      if (iMetadataConnection.getDbVersionString() != null && !iMetadataConnection.getDbVersionString().equals("String_Node_Str")) {
        ResultSet rsTables=stmt.executeQuery(TableInfoParameters.ORACLE_10G_RECBIN_SQL);
        tablesToFilter=getTableNamesFromQuery(rsTables);
        rsTables.close();
      }
      stmt.close();
    }
 catch (    SQLException e) {
      ExceptionHandler.process(e);
    }
  }
 else {
    ExtractMetaDataUtils.setUseAllSynonyms(false);
  }
  List<IMetadataTable> medataTables=new ArrayList<IMetadataTable>();
  try {
    ResultSet rsTables=null, rsTableTypes=null;
    if (!ExtractMetaDataUtils.isUseAllSynonyms()) {
      rsTableTypes=dbMetaData.getTableTypes();
      Set<String> availableTableTypes=new HashSet<String>();
      String[] neededTableTypes={ETableTypes.TABLETYPE_TABLE.getName(),ETableTypes.TABLETYPE_VIEW.getName(),ETableTypes.TABLETYPE_SYNONYM.getName()};
      while (rsTableTypes.next()) {
        String currentTableType=StringUtils.trimToEmpty(rsTableTypes.getString("String_Node_Str"));
        if ("String_Node_Str".equalsIgnoreCase(currentTableType)) {
          currentTableType=ETableTypes.TABLETYPE_TABLE.getName();
        }
        if (ArrayUtils.contains(neededTableTypes,currentTableType)) {
          availableTableTypes.add(currentTableType);
        }
      }
      rsTableTypes.close();
      if (dbMetaData.supportsSchemasInTableDefinitions() && schema != null && !schema.equals("String_Node_Str")) {
        if (as400) {
          String[] multiSchems=ExtractMetaDataUtils.getMultiSchems(schema);
          if (multiSchems != null) {
            for (            String s : multiSchems) {
              rsTables=dbMetaData.getTables(null,s.trim(),null,availableTableTypes.toArray(new String[]{}));
              getMetadataTables(medataTables,rsTables,dbMetaData.supportsSchemasInTableDefinitions(),tablesToFilter,limit);
              rsTables.close();
            }
            rsTables=null;
          }
        }
 else {
          rsTables=dbMetaData.getTables(null,schema,null,availableTableTypes.toArray(new String[]{}));
        }
      }
 else       if (schema != null && !schema.equals("String_Node_Str")) {
        rsTables=dbMetaData.getTables(null,schema,null,availableTableTypes.toArray(new String[]{}));
      }
 else {
        rsTables=dbMetaData.getTables(null,null,null,availableTableTypes.toArray(new String[]{}));
      }
    }
 else {
      String sql=GET_ALL_SYNONYMS;
      Statement stmt=ExtractMetaDataUtils.conn.createStatement();
      ExtractMetaDataUtils.setQueryStatementTimeout(stmt);
      rsTables=stmt.executeQuery(sql);
    }
    if (rsTables != null) {
      getMetadataTables(medataTables,rsTables,dbMetaData.supportsSchemasInTableDefinitions(),tablesToFilter,limit);
      rsTables.close();
    }
  }
 catch (  SQLException e) {
    ExceptionHandler.process(e);
    log.error(e.toString());
    throw new RuntimeException(e);
  }
catch (  Exception e) {
    ExceptionHandler.process(e);
    log.error(e.toString());
    throw new RuntimeException(e);
  }
  oldMetadata=dbMetaData;
  oldSchema=schema;
  oldLimit=limit;
  oldUseAllSynonyms=ExtractMetaDataUtils.isUseAllSynonyms();
  oldMetadataRetrieved=medataTables;
  return medataTables;
}