From 57ee8e587952ef24ed69bf4683b470d2c3920d1a Mon Sep 17 00:00:00 2001 From: Bas Leijdekkers Date: Wed, 20 Jan 2016 11:25:02 +0100 Subject: [PATCH] IPP: keep block if declarations would conflict otherwise --- .../ConvertCatchToThrowsIntention.java | 19 +++++++++++++------ .../TryWithConflictingDeclaration.java | 15 +++++++++++++++ .../TryWithConflictingDeclaration_after.java | 15 +++++++++++++++ .../exceptions/ConvertCatchToThrowsTest.java | 6 +++++- 4 files changed, 48 insertions(+), 7 deletions(-) create mode 100644 plugins/IntentionPowerPak/test/com/siyeh/ipp/exceptions/catchToThrows/TryWithConflictingDeclaration.java create mode 100644 plugins/IntentionPowerPak/test/com/siyeh/ipp/exceptions/catchToThrows/TryWithConflictingDeclaration_after.java diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/exceptions/ConvertCatchToThrowsIntention.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/exceptions/ConvertCatchToThrowsIntention.java index 683a87083e2a..0673d6f64b50 100644 --- a/plugins/IntentionPowerPak/src/com/siyeh/ipp/exceptions/ConvertCatchToThrowsIntention.java +++ b/plugins/IntentionPowerPak/src/com/siyeh/ipp/exceptions/ConvertCatchToThrowsIntention.java @@ -1,5 +1,5 @@ /* - * Copyright 2007-2014 Bas Leijdekkers + * Copyright 2007-2016 Bas Leijdekkers * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -20,6 +20,7 @@ import com.intellij.openapi.project.Project; import com.intellij.psi.*; import com.intellij.psi.util.PsiTreeUtil; import com.intellij.util.IncorrectOperationException; +import com.siyeh.ig.psiutils.VariableSearchUtils; import com.siyeh.ipp.base.Intention; import com.siyeh.ipp.base.PsiElementPredicate; import org.jetbrains.annotations.NotNull; @@ -69,12 +70,18 @@ public class ConvertCatchToThrowsIntention extends Intention { if (tryBlock == null) { return; } - final PsiElement first = tryBlock.getFirstBodyElement(); - final PsiElement last = tryBlock.getLastBodyElement(); - if (first != null && last != null) { - tryStatement.getParent().addRangeAfter(first, last, tryStatement); + final PsiCodeBlock parentCodeBlock = PsiTreeUtil.getParentOfType(tryStatement, PsiCodeBlock.class); + if (parentCodeBlock == null || !VariableSearchUtils.containsConflictingDeclarations(tryBlock, parentCodeBlock)) { + final PsiElement first = tryBlock.getFirstBodyElement(); + final PsiElement last = tryBlock.getLastBodyElement(); + if (first != null && last != null) { + tryStatement.getParent().addRangeAfter(first, last, tryStatement); + } + tryStatement.delete(); + } + else { + tryStatement.replace(tryBlock); } - tryStatement.delete(); } } diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/exceptions/catchToThrows/TryWithConflictingDeclaration.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/exceptions/catchToThrows/TryWithConflictingDeclaration.java new file mode 100644 index 000000000000..923df9ba5dfe --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/exceptions/catchToThrows/TryWithConflictingDeclaration.java @@ -0,0 +1,15 @@ +class TryWithConflictingDeclaration { + + abstract void f(String s) throws Exception; + + void m() { + try { + /* important comment */ + String s = "hello"; + f(); + // another comment + } catch (Exception ignore) { } + String s = "bye"; + System.out.println(s); + } +} \ No newline at end of file diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/exceptions/catchToThrows/TryWithConflictingDeclaration_after.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/exceptions/catchToThrows/TryWithConflictingDeclaration_after.java new file mode 100644 index 000000000000..3f33b590738c --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/exceptions/catchToThrows/TryWithConflictingDeclaration_after.java @@ -0,0 +1,15 @@ +class TryWithConflictingDeclaration { + + abstract void f(String s) throws Exception; + + void m() throws Exception { + { + /* important comment */ + String s = "hello"; + f(); + // another comment + } + String s = "bye"; + System.out.println(s); + } +} \ No newline at end of file diff --git a/plugins/IntentionPowerPak/testSrc/com/siyeh/ipp/exceptions/ConvertCatchToThrowsTest.java b/plugins/IntentionPowerPak/testSrc/com/siyeh/ipp/exceptions/ConvertCatchToThrowsTest.java index 737d40e0cc39..03f7750746d0 100644 --- a/plugins/IntentionPowerPak/testSrc/com/siyeh/ipp/exceptions/ConvertCatchToThrowsTest.java +++ b/plugins/IntentionPowerPak/testSrc/com/siyeh/ipp/exceptions/ConvertCatchToThrowsTest.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2014 JetBrains s.r.o. + * Copyright 2000-2016 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. @@ -18,6 +18,9 @@ package com.siyeh.ipp.exceptions; import com.siyeh.IntentionPowerPackBundle; import com.siyeh.ipp.IPPTestCase; +/** + * @see ConvertCatchToThrowsIntention + */ public class ConvertCatchToThrowsTest extends IPPTestCase { public void testSingleCatch() { doTest(); } public void testPluralCatches() { doTest(); } @@ -27,6 +30,7 @@ public class ConvertCatchToThrowsTest extends IPPTestCase { public void testExistingThrows() { doTest(); } public void testLambda() { doTest(); } public void testLeaveFinallySection() { doTest(); } + public void testTryWithConflictingDeclaration() { doTest(); } @Override protected String getIntentionName() {