添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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

Ask Question

Checked similar questions regarding this topic but I did not find solution to my use-case.
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?

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
                Considering that the first two operations use the common base type Workbook and are otherwise identical too, I wouldn’t say that they need their own try-with-resources.
– Holger
                Oct 9, 2020 at 6:51
                @Holger Well, they do need their own constructor calls, and since the constructor call is usually in the try bracket, they get their own blocks. But you're right, they could be combined, but it's hardly necessary for this simple code.
– Andreas
                Oct 9, 2020 at 6:54
                Of course, people have different opinion about which amount of code duplication is acceptable. Since sharing the code would slightly raise the complexity, five lines of code are a borderline case. To me, even having file.getName() repeated three times feels wrong…
– Holger
                Oct 9, 2020 at 6:59
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.