From f4e55195027e3bd63ddd359f171c401e813461ec Mon Sep 17 00:00:00 2001 From: Roman Shevchenko Date: Tue, 4 Sep 2012 21:06:35 +0400 Subject: [PATCH] IDEA-90736 (preserve resource declarations from deleted try statement) --- .../EmptyFinallyBlockInspection.java | 35 +++++++++++-------- .../tryCatch/DeleteEmptyFinally.after.java | 23 ++++++++++++ .../igfixes/tryCatch/DeleteEmptyFinally.java | 24 +++++++++++++ ...ava => DeleteEmptyTryStatement.after.java} | 2 +- ...ment.java => DeleteEmptyTryStatement.java} | 2 +- ...ter.java => DeleteTryStatement.after.java} | 2 +- ...Statement.java => DeleteTryStatement.java} | 2 +- .../DeleteTryWithEmptyFinally.after.java | 23 ++++++++++++ .../tryCatch/DeleteTryWithEmptyFinally.java | 25 +++++++++++++ .../com/siyeh/ig/IGQuickFixesTestCase.java | 20 +++++------ ...est.java => DeleteTrySectionsFixTest.java} | 24 +++++++++---- 11 files changed, 146 insertions(+), 36 deletions(-) create mode 100644 plugins/InspectionGadgets/test/com/siyeh/igfixes/tryCatch/DeleteEmptyFinally.after.java create mode 100644 plugins/InspectionGadgets/test/com/siyeh/igfixes/tryCatch/DeleteEmptyFinally.java rename plugins/InspectionGadgets/test/com/siyeh/igfixes/tryCatch/{deleteEmptyTryStatement.after.java => DeleteEmptyTryStatement.after.java} (93%) rename plugins/InspectionGadgets/test/com/siyeh/igfixes/tryCatch/{deleteEmptyTryStatement.java => DeleteEmptyTryStatement.java} (94%) rename plugins/InspectionGadgets/test/com/siyeh/igfixes/tryCatch/{deleteTryStatement.after.java => DeleteTryStatement.after.java} (94%) rename plugins/InspectionGadgets/test/com/siyeh/igfixes/tryCatch/{deleteTryStatement.java => DeleteTryStatement.java} (94%) create mode 100644 plugins/InspectionGadgets/test/com/siyeh/igfixes/tryCatch/DeleteTryWithEmptyFinally.after.java create mode 100644 plugins/InspectionGadgets/test/com/siyeh/igfixes/tryCatch/DeleteTryWithEmptyFinally.java rename plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/{DeleteCatchSectionFixTest.java => DeleteTrySectionsFixTest.java} (55%) diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/errorhandling/EmptyFinallyBlockInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/errorhandling/EmptyFinallyBlockInspection.java index 464989a521a5..81b9a007879e 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/ig/errorhandling/EmptyFinallyBlockInspection.java +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/errorhandling/EmptyFinallyBlockInspection.java @@ -28,12 +28,10 @@ import com.siyeh.ig.InspectionGadgetsFix; import org.jetbrains.annotations.NotNull; public class EmptyFinallyBlockInspection extends BaseInspection { - @Override @NotNull public String getDisplayName() { - return InspectionGadgetsBundle.message( - "empty.finally.block.display.name"); + return InspectionGadgetsBundle.message("empty.finally.block.display.name"); } @Override @@ -44,8 +42,7 @@ public class EmptyFinallyBlockInspection extends BaseInspection { @Override @NotNull protected String buildErrorString(Object... infos) { - return InspectionGadgetsBundle.message( - "empty.finally.block.problem.descriptor"); + return InspectionGadgetsBundle.message("empty.finally.block.problem.descriptor"); } @Override @@ -60,19 +57,15 @@ public class EmptyFinallyBlockInspection extends BaseInspection { } private static class RemoveTryFinallyBlockFix extends InspectionGadgetsFix { - @NotNull public String getName() { - return InspectionGadgetsBundle.message( - "remove.try.finally.block.quickfix"); + return InspectionGadgetsBundle.message("remove.try.finally.block.quickfix"); } @Override - protected void doFix(Project project, ProblemDescriptor descriptor) - throws IncorrectOperationException { + protected void doFix(Project project, ProblemDescriptor descriptor) throws IncorrectOperationException { final PsiElement element = descriptor.getPsiElement(); - final PsiTryStatement tryStatement = - PsiTreeUtil.getParentOfType(element, PsiTryStatement.class); + final PsiTryStatement tryStatement = PsiTreeUtil.getParentOfType(element, PsiTryStatement.class); if (tryStatement == null) { return; } @@ -81,21 +74,33 @@ public class EmptyFinallyBlockInspection extends BaseInspection { return; } final PsiElement parent = tryStatement.getParent(); + if (parent == null) { + return; + } + + final PsiResourceList resources = tryStatement.getResourceList(); + if (resources != null) { + final PsiElementFactory factory = JavaPsiFacade.getElementFactory(project); + for (PsiResourceVariable resource : resources.getResourceVariables()) { + final PsiStatement statement = factory.createStatementFromText(resource.getText() + ";", parent); + parent.addBefore(statement, tryStatement); + } + } + final PsiElement first = tryBlock.getFirstBodyElement(); final PsiElement last = tryBlock.getLastBodyElement(); if (first != null && last != null) { parent.addRangeAfter(first, last, tryStatement); } + tryStatement.delete(); } } private static class RemoveFinallyBlockFix extends InspectionGadgetsFix { - @NotNull public String getName() { - return InspectionGadgetsBundle.message( - "remove.finally.block.quickfix"); + return InspectionGadgetsBundle.message("remove.finally.block.quickfix"); } @Override diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/tryCatch/DeleteEmptyFinally.after.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/tryCatch/DeleteEmptyFinally.after.java new file mode 100644 index 000000000000..4b0b3e4d12bb --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/tryCatch/DeleteEmptyFinally.after.java @@ -0,0 +1,23 @@ +/* + * Copyright 2000-2012 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +class DeleteEmptyFinally { + void m() { + try { + } catch (Exception e) { + e.printStackTrace(); + } + } +} diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/tryCatch/DeleteEmptyFinally.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/tryCatch/DeleteEmptyFinally.java new file mode 100644 index 000000000000..f8b45cd103dd --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/tryCatch/DeleteEmptyFinally.java @@ -0,0 +1,24 @@ +/* + * Copyright 2000-2012 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +class DeleteEmptyFinally { + void m() { + try { + } catch (Exception e) { + e.printStackTrace(); + } finally { + } + } +} diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/tryCatch/deleteEmptyTryStatement.after.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/tryCatch/DeleteEmptyTryStatement.after.java similarity index 93% rename from plugins/InspectionGadgets/test/com/siyeh/igfixes/tryCatch/deleteEmptyTryStatement.after.java rename to plugins/InspectionGadgets/test/com/siyeh/igfixes/tryCatch/DeleteEmptyTryStatement.after.java index ea9292fdda5d..7d3085b055f2 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igfixes/tryCatch/deleteEmptyTryStatement.after.java +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/tryCatch/DeleteEmptyTryStatement.after.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2011 JetBrains s.r.o. + * Copyright 2000-2012 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/tryCatch/deleteEmptyTryStatement.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/tryCatch/DeleteEmptyTryStatement.java similarity index 94% rename from plugins/InspectionGadgets/test/com/siyeh/igfixes/tryCatch/deleteEmptyTryStatement.java rename to plugins/InspectionGadgets/test/com/siyeh/igfixes/tryCatch/DeleteEmptyTryStatement.java index 256de0a54e1d..0b0b8e2f01dd 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igfixes/tryCatch/deleteEmptyTryStatement.java +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/tryCatch/DeleteEmptyTryStatement.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2011 JetBrains s.r.o. + * Copyright 2000-2012 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/tryCatch/deleteTryStatement.after.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/tryCatch/DeleteTryStatement.after.java similarity index 94% rename from plugins/InspectionGadgets/test/com/siyeh/igfixes/tryCatch/deleteTryStatement.after.java rename to plugins/InspectionGadgets/test/com/siyeh/igfixes/tryCatch/DeleteTryStatement.after.java index 6530b4dd0394..151d10ab8d66 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igfixes/tryCatch/deleteTryStatement.after.java +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/tryCatch/DeleteTryStatement.after.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2011 JetBrains s.r.o. + * Copyright 2000-2012 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/tryCatch/deleteTryStatement.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/tryCatch/DeleteTryStatement.java similarity index 94% rename from plugins/InspectionGadgets/test/com/siyeh/igfixes/tryCatch/deleteTryStatement.java rename to plugins/InspectionGadgets/test/com/siyeh/igfixes/tryCatch/DeleteTryStatement.java index a3ef9067f057..85995bc9a27e 100644 --- a/plugins/InspectionGadgets/test/com/siyeh/igfixes/tryCatch/deleteTryStatement.java +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/tryCatch/DeleteTryStatement.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2011 JetBrains s.r.o. + * Copyright 2000-2012 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/tryCatch/DeleteTryWithEmptyFinally.after.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/tryCatch/DeleteTryWithEmptyFinally.after.java new file mode 100644 index 000000000000..e27b15b90c30 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/tryCatch/DeleteTryWithEmptyFinally.after.java @@ -0,0 +1,23 @@ +/* + * Copyright 2000-2012 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +abstract class DeleteTryWithEmptyFinally { + abstract OutputStream open(); + + void m() throws IOException { + OutputStream stream = open(); + stream.write(0xCAFEBABE); + } +} diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/tryCatch/DeleteTryWithEmptyFinally.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/tryCatch/DeleteTryWithEmptyFinally.java new file mode 100644 index 000000000000..65b529b841ab --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/tryCatch/DeleteTryWithEmptyFinally.java @@ -0,0 +1,25 @@ +/* + * Copyright 2000-2012 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +abstract class DeleteTryWithEmptyFinally { + abstract OutputStream open(); + + void m() throws IOException { + try (OutputStream stream = open()) { + stream.write(0xCAFEBABE); + } finally { + } + } +} diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/IGQuickFixesTestCase.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/IGQuickFixesTestCase.java index 281577d0f35e..f8013fd5acfa 100644 --- a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/IGQuickFixesTestCase.java +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/IGQuickFixesTestCase.java @@ -1,17 +1,17 @@ /* * Copyright 2000-2012 JetBrains s.r.o. * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at * - * http://www.apache.org/licenses/LICENSE-2.0 + * http://www.apache.org/licenses/LICENSE-2.0 * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. */ package com.siyeh.ig; @@ -23,7 +23,7 @@ import com.intellij.testFramework.fixtures.JavaCodeInsightFixtureTestCase; /** * @author anna - * Date: 16-Jun-2009 + * @since 16-Jun-2009 */ @SuppressWarnings({"JUnitTestCaseWithNonTrivialConstructors"}) public abstract class IGQuickFixesTestCase extends JavaCodeInsightFixtureTestCase { diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/DeleteCatchSectionFixTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/DeleteTrySectionsFixTest.java similarity index 55% rename from plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/DeleteCatchSectionFixTest.java rename to plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/DeleteTrySectionsFixTest.java index 75ecb6a2b2e6..a87eac584b5b 100644 --- a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/DeleteCatchSectionFixTest.java +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/DeleteTrySectionsFixTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2011 JetBrains s.r.o. + * Copyright 2000-2012 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -15,14 +15,16 @@ */ package com.siyeh.ig.fixes; +import com.siyeh.InspectionGadgetsBundle; import com.siyeh.ig.IGQuickFixesTestCase; import com.siyeh.ig.errorhandling.CaughtExceptionImmediatelyRethrownInspection; +import com.siyeh.ig.errorhandling.EmptyFinallyBlockInspection; -public class DeleteCatchSectionFixTest extends IGQuickFixesTestCase { +public class DeleteTrySectionsFixTest extends IGQuickFixesTestCase { @Override protected void setUp() throws Exception { super.setUp(); - myFixture.enableInspections(new CaughtExceptionImmediatelyRethrownInspection()); + myFixture.enableInspections(new CaughtExceptionImmediatelyRethrownInspection(), new EmptyFinallyBlockInspection()); } @Override @@ -30,11 +32,19 @@ public class DeleteCatchSectionFixTest extends IGQuickFixesTestCase { return "tryCatch"; } - public void testDeleteTryStatement() throws Exception { - doTest("deleteTryStatement", "Remove 'try catch' statement"); + public void testDeleteTryStatement() { + doTest(InspectionGadgetsBundle.message("remove.try.catch.quickfix")); } - public void testDeleteEmptyTryStatement() throws Exception { - doTest("deleteEmptyTryStatement", "Remove 'try catch' statement"); + public void testDeleteEmptyTryStatement() { + doTest(InspectionGadgetsBundle.message("remove.try.catch.quickfix")); + } + + public void testDeleteEmptyFinally() { + doTest(InspectionGadgetsBundle.message("remove.finally.block.quickfix")); + } + + public void testDeleteTryWithEmptyFinally() { + doTest(InspectionGadgetsBundle.message("remove.try.finally.block.quickfix")); } }