diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/migration/TryFinallyCanBeTryWithResourcesInspection.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/migration/TryFinallyCanBeTryWithResourcesInspection.java index df277a49c8a1..83b2d0b3f646 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/migration/TryFinallyCanBeTryWithResourcesInspection.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/migration/TryFinallyCanBeTryWithResourcesInspection.java @@ -107,8 +107,14 @@ public class TryFinallyCanBeTryWithResourcesInspection extends BaseInspection { } PsiCodeBlock finallyBlock = tryStatement.getFinallyBlock(); if(finallyBlock == null) return; - if(finallyNonEmpty(finallyBlock)) { + if(!ControlFlowUtils.isEmptyCodeBlock(finallyBlock)) { sb.append("finally").append(finallyBlock.getText()); + } else { + PsiElement[] finallyBlockChildren = finallyBlock.getChildren(); + if(!StreamEx.of(finallyBlockChildren).skip(1).limit(finallyBlockChildren.length - 2).allMatch(el -> el instanceof PsiWhiteSpace)) { + PsiElement tryParent = tryStatement.getParent(); + tryParent.addRangeAfter(finallyBlockChildren[1], finallyBlockChildren[finallyBlockChildren.length - 2], tryStatement); + } } tryStatement.replace(JavaPsiFacade.getElementFactory(project).createStatementFromText(sb.toString(), tryStatement)); } @@ -139,16 +145,6 @@ public class TryFinallyCanBeTryWithResourcesInspection extends BaseInspection { statement.delete(); } } - - private static boolean finallyNonEmpty(PsiCodeBlock finallyBlock) { - PsiElement[] children = finallyBlock.getChildren(); - for (int i = 1; i < children.length - 1; i++) { - if (!(children[i] instanceof PsiWhiteSpace)) { - return true; - } - } - return false; - } } @Override diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/migration/try_finally_can_be_try_with_resources/Comments.after.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/migration/try_finally_can_be_try_with_resources/Comments.after.java index 213c58e71b39..0523dd66e470 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igfixes/migration/try_finally_can_be_try_with_resources/Comments.after.java +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/migration/try_finally_can_be_try_with_resources/Comments.after.java @@ -5,11 +5,9 @@ import java.io.*; class Comments { void m(OutputStream out) throws IOException { - try (InputStream in = new FileInputStream("filename")) { - } finally { - // stop - // now - out.close(); + try (out; InputStream in = new FileInputStream("filename")) { } + // stop + // now } } \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/migration/try_finally_can_be_try_with_resources/Java9WithParameter.after.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/migration/try_finally_can_be_try_with_resources/Java9WithParameter.after.java index e05b107508f2..5d6f83361f33 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igfixes/migration/try_finally_can_be_try_with_resources/Java9WithParameter.after.java +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/migration/try_finally_can_be_try_with_resources/Java9WithParameter.after.java @@ -1,6 +1,3 @@ -/* - * Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. - */ package com.siyeh.igfixes.migration.try_finally_can_be_try_with_resources; import java.io.*; diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/migration/try_finally_can_be_try_with_resources/TryInCatchBlock.after.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/migration/try_finally_can_be_try_with_resources/TryInCatchBlock.after.java index a75ec72bbe5f..ac0248c66610 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igfixes/migration/try_finally_can_be_try_with_resources/TryInCatchBlock.after.java +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/migration/try_finally_can_be_try_with_resources/TryInCatchBlock.after.java @@ -8,9 +8,8 @@ class X { try (Reader r = new FileReader("")) { r.read(); - } finally { - // 1 - /*2*/ } + // 1 + /*2*/ } } \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igtest/migration/try_finally_can_be_try_with_resources/TryFinallyCanBeTryWithResources.java b/plugins/InspectionGadgets/test/com/siyeh/igtest/migration/try_finally_can_be_try_with_resources/TryFinallyCanBeTryWithResources.java index 5472cd3d9406..20bc9be2770a 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igtest/migration/try_finally_can_be_try_with_resources/TryFinallyCanBeTryWithResources.java +++ b/plugins/InspectionGadgets/test/com/siyeh/igtest/migration/try_finally_can_be_try_with_resources/TryFinallyCanBeTryWithResources.java @@ -67,8 +67,8 @@ class TryFinallyCanBeTryWithResources { class Java9 { void test() throws FileNotFoundException { - PrintStream printStream; - printStream = new PrintStream(""); + PrintStream printStream = new PrintStream(""); + printStream.print(false); try { printStream.print(true); } finally {