【问题标题】:Sonarqube issue - Change this "try" to a try-with-resources. How to handle conditional resources?Sonarqube 问题 - 将此“尝试”更改为资源尝试。如何处理条件资源?
【发布时间】:2020-09-19 18:38:26
【问题描述】:

检查了有关此主题的类似问题,但我没有找到我的用例的解决方案。
以下方法将Major code smell 的声纳问题显示为 - Change this "try" to a try-with-resources.

private void readExcel() {
        Workbook workbook = null;
        BufferedReader br = null;
        
        try {
            File file = path.toFile();
            if (file.getName().endsWith(FILE_TYPES.XLS.value) && (file.length() / (1024 * 1024)) < 25) {
                workbook = new HSSFWorkbook(new POIFSFileSystem(file));
                Sheet sheet = workbook.getSheetAt(0);
                readExcel(sheet);
            } else if ((file.getName().endsWith(FILE_TYPES.XLSX.value)) && (file.length() / (1024 * 1024)) < 25) {
                workbook = new XSSFWorkbook(file);
                Sheet sheet = workbook.getSheetAt(0);
                readExcel(sheet);
            } else if (file.getName().endsWith(FILE_TYPES.CSV.value)) {
                // Apache POI cant read/write CSV. Hence using Java IO.
                br = new BufferedReader(new FileReader(file));
                readExcel(br);
            }
        } catch (IOException | InvalidFormatException ex) {
                // set invalid flags and call clean-up
        } finally {
            try {
                if (workbook != null) {
                    workbook.close();
                }
                if (br != null) {
                    br.close();
                }
            } catch (IOException ex) {
                // set invalid flags and call clean-up
            }
        } // try-catch-finally closed
    }

这是误报声纳问题吗?

【问题讨论】:

  • 至少 BufferedReader 可以是 try-with-resources 块,所以这样做。

标签: java sonarqube try-with-resources


【解决方案1】:

HSSFWorkbookAutoCloseable
XSSFWorkbookAutoCloseable
BufferedReaderAutoCloseable

他们都需要自己的try-with-resources

去掉 Workbook workbook = null;BufferedReader br = null; 以及 finally 块中的代码,因为这都是旧式的 pre-try-with-resources。

private void readExcel() {
    try {
        File file = path.toFile();
        if (file.getName().endsWith(FILE_TYPES.XLS.value) && (file.length() / (1024 * 1024)) < 25) {
            try (Workbook workbook = new HSSFWorkbook(new POIFSFileSystem(file))) {
                Sheet sheet = workbook.getSheetAt(0);
                readExcel(sheet);
            }
        } else if ((file.getName().endsWith(FILE_TYPES.XLSX.value)) && (file.length() / (1024 * 1024)) < 25) {
            try (Workbook workbook = new XSSFWorkbook(file)) {
                Sheet sheet = workbook.getSheetAt(0);
                readExcel(sheet);
            }
        } else if (file.getName().endsWith(FILE_TYPES.CSV.value)) {
            // Apache POI cant read/write CSV. Hence using Java IO.
            try (BufferedReader br = new BufferedReader(new FileReader(file))) {
                readExcel(br);
            }
        }
    } catch (IOException | InvalidFormatException ex) {
        // set invalid flags and call clean-up
    }
}

【讨论】:

  • 考虑到前两个操作使用公共基本类型Workbook 并且在其他方​​面也相同,我不会说他们需要他们自己的try-with-resources .
  • @Holger 好吧,它们确实需要自己的构造函数调用,并且由于构造函数调用通常try 括号中,因此它们有自己的块。但你是对的,它们可以组合,但对于这个简单的代码来说几乎没有必要。
  • 当然,对于多少代码重复是可以接受的,人们有不同的看法。由于共享代码会稍微增加复杂性,因此五行代码是一种临界情况。对我来说,即使 file.getName() 重复三遍也感觉不对……
猜你喜欢
  • 1970-01-01
  • 2017-01-03
  • 1970-01-01
  • 2020-02-08
  • 1970-01-01
  • 1970-01-01
  • 2018-08-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多