Sonarqube issue – Change this "try" to a try-with-resources. How to handle conditional resources?

huangapple go评论112阅读模式
英文:

Sonarqube issue - Change this "try" to a try-with-resources. How to handle conditional resources?

问题

我在类似的问题中检查过关于这个主题的内容,但是我没有找到适用于我的用例的解决方法。
以下方法显示了Sonar问题,显示为主要代码异味,内容是 - 将此“try”更改为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无法读/写CSV。因此使用Java IO。
                br = new BufferedReader(new FileReader(file));
                readExcel(br);
            }
        } catch (IOException | InvalidFormatException ex) {
                // 设置无效标志并调用清理操作
        } finally {
            try {
                if (workbook != null) {
                    workbook.close();
                }
                if (br != null) {
                    br.close();
                }
            } catch (IOException ex) {
                // 设置无效标志并调用清理操作
            }
        } // try-catch-finally 结束
    }

这是误报的 Sonar 问题吗?

英文:

Checked similar questions regarding this topic but I did not find solution to my use-case.
<br>Following method shows Sonar issue with Major code smell as - Change this &quot;try&quot; 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) &amp;&amp; (file.length() / (1024 * 1024)) &lt; 25) {
                workbook = new HSSFWorkbook(new POIFSFileSystem(file));
                Sheet sheet = workbook.getSheetAt(0);
                readExcel(sheet);
            } else if ((file.getName().endsWith(FILE_TYPES.XLSX.value)) &amp;&amp; (file.length() / (1024 * 1024)) &lt; 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
    }

Is this false positive sonar issue?

答案1

得分: 2

HSSFWorkbookAutoCloseable
XSSFWorkbookAutoCloseable
BufferedReaderAutoCloseable

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

去掉 Workbook workbook = null;BufferedReader br = null;,以及 finally 块中的代码,因为那都是旧式的 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 不能读/写 CSV。因此使用 Java IO。
			try (BufferedReader br = new BufferedReader(new FileReader(file))) {
				readExcel(br);
			}
		}
	} catch (IOException | InvalidFormatException ex) {
		// 设置无效标志并调用清理
	}
}
英文:

HSSFWorkbook is an AutoCloseable.
XSSFWorkbook is an AutoCloseable.
BufferedReader is an AutoCloseable.

They all need their own try-with-resources.

Get rid of Workbook workbook = null; and BufferedReader br = null;, and the code in the finally block, because that's all old-style pre-try-with-resources.

private void readExcel() {
	try {
		File file = path.toFile();
		if (file.getName().endsWith(FILE_TYPES.XLS.value) &amp;&amp; (file.length() / (1024 * 1024)) &lt; 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)) &amp;&amp; (file.length() / (1024 * 1024)) &lt; 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
	}
}

huangapple
  • 本文由 发表于 2020年9月20日 02:38:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/63972170.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定