英文:
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 "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
}
Is this false positive sonar issue?
答案1
得分: 2
HSSFWorkbook
是 AutoCloseable
。
XSSFWorkbook
是 AutoCloseable
。
BufferedReader
是 AutoCloseable
。
它们都需要自己的 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) && (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
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论