From eb937bc8d786ebf1427130fab0fec9eb9692699a Mon Sep 17 00:00:00 2001 From: Pavel Dolgov Date: Wed, 29 Jun 2016 19:33:54 +0300 Subject: [PATCH] Java inspection: convert the intention for "Split Multi-Catch into Separate Catch Blocks" into an INFORMATION-level inspection (IDEA-157727) --- .../src/META-INF/InspectionGadgets.xml | 4 + .../siyeh/InspectionGadgetsBundle.properties | 2 + .../exceptions/SplitMultiCatchInspection.java | 141 ++++++++++++++++++ .../SplitMultiCatch.html | 6 + .../split_multi_catch/AtCodeBlock.java | 10 ++ .../split_multi_catch/AtParameter.after.java | 16 ++ .../split_multi_catch/AtParameter.java | 12 ++ .../split_multi_catch/Simple.after.java} | 0 .../exceptions/split_multi_catch}/Simple.java | 0 .../split_multi_catch/TypeAnno.after.java} | 0 .../split_multi_catch}/TypeAnno.java | 0 .../exceptions/SplitMultiCatchFixTest.java | 35 +++++ .../src/META-INF/IntentionPowerPack.xml | 5 - .../siyeh/IntentionPowerPackBundle.properties | 2 - .../ipp/exceptions/MulticatchPredicate.java | 35 ----- .../exceptions/SplitMultiCatchIntention.java | 70 --------- .../after.java.template | 16 -- .../before.java.template | 12 -- .../SplitMultiCatchIntention/description.html | 6 - .../SplitMultiCatchIntentionTest.java | 34 ----- 20 files changed, 226 insertions(+), 180 deletions(-) create mode 100644 plugins/InspectionGadgets/src/com/siyeh/ig/exceptions/SplitMultiCatchInspection.java create mode 100644 plugins/InspectionGadgets/src/inspectionDescriptions/SplitMultiCatch.html create mode 100644 plugins/InspectionGadgets/test/com/siyeh/igfixes/exceptions/split_multi_catch/AtCodeBlock.java create mode 100644 plugins/InspectionGadgets/test/com/siyeh/igfixes/exceptions/split_multi_catch/AtParameter.after.java create mode 100644 plugins/InspectionGadgets/test/com/siyeh/igfixes/exceptions/split_multi_catch/AtParameter.java rename plugins/{IntentionPowerPak/test/com/siyeh/ipp/exceptions/splitMultiCatch/Simple_after.java => InspectionGadgets/test/com/siyeh/igfixes/exceptions/split_multi_catch/Simple.after.java} (100%) rename plugins/{IntentionPowerPak/test/com/siyeh/ipp/exceptions/splitMultiCatch => InspectionGadgets/test/com/siyeh/igfixes/exceptions/split_multi_catch}/Simple.java (100%) rename plugins/{IntentionPowerPak/test/com/siyeh/ipp/exceptions/splitMultiCatch/TypeAnno_after.java => InspectionGadgets/test/com/siyeh/igfixes/exceptions/split_multi_catch/TypeAnno.after.java} (100%) rename plugins/{IntentionPowerPak/test/com/siyeh/ipp/exceptions/splitMultiCatch => InspectionGadgets/test/com/siyeh/igfixes/exceptions/split_multi_catch}/TypeAnno.java (100%) create mode 100644 plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/exceptions/SplitMultiCatchFixTest.java delete mode 100644 plugins/IntentionPowerPak/src/com/siyeh/ipp/exceptions/MulticatchPredicate.java delete mode 100644 plugins/IntentionPowerPak/src/com/siyeh/ipp/exceptions/SplitMultiCatchIntention.java delete mode 100644 plugins/IntentionPowerPak/src/intentionDescriptions/SplitMultiCatchIntention/after.java.template delete mode 100644 plugins/IntentionPowerPak/src/intentionDescriptions/SplitMultiCatchIntention/before.java.template delete mode 100644 plugins/IntentionPowerPak/src/intentionDescriptions/SplitMultiCatchIntention/description.html delete mode 100644 plugins/IntentionPowerPak/testSrc/com/siyeh/ipp/exceptions/SplitMultiCatchIntentionTest.java diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/META-INF/InspectionGadgets.xml b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/META-INF/InspectionGadgets.xml index e25d1fb34a58..0bfbe519655e 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/META-INF/InspectionGadgets.xml +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/META-INF/InspectionGadgets.xml @@ -2559,6 +2559,10 @@ key="split.try.with.multiple.resources.name" groupBundle="messages.InspectionsBundle" groupKey="group.names.code.style.issues" enabledByDefault="true" level="INFORMATION" implementationClass="com.siyeh.ig.exceptions.SplitTryWithMultipleResourcesInspection"/> + + +This inspection splits a multi-catch section into separate catch blocks. +

+ + diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/exceptions/split_multi_catch/AtCodeBlock.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/exceptions/split_multi_catch/AtCodeBlock.java new file mode 100644 index 000000000000..d633b99f6505 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/exceptions/split_multi_catch/AtCodeBlock.java @@ -0,0 +1,10 @@ +import java.io.*; + +public class AtCodeBlock { + void foo() { + try { + Reader reader = new FileReader(""); + } catch (IndexOutOfBoundsException | FileNotFoundException e) { + } + } +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/exceptions/split_multi_catch/AtParameter.after.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/exceptions/split_multi_catch/AtParameter.after.java new file mode 100644 index 000000000000..b5eacc83956f --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/exceptions/split_multi_catch/AtParameter.after.java @@ -0,0 +1,16 @@ +import java.io.IOException; +import java.net.*; + +public class AtParameter { + void f() { + try { + throw new NoRouteToHostException(); + } catch (NoRouteToHostException e) { + e.printStackTrace(); + } catch (SocketException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/exceptions/split_multi_catch/AtParameter.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/exceptions/split_multi_catch/AtParameter.java new file mode 100644 index 000000000000..7a6913fa5ead --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/exceptions/split_multi_catch/AtParameter.java @@ -0,0 +1,12 @@ +import java.io.IOException; +import java.net.*; + +public class AtParameter { + void f() { + try { + throw new NoRouteToHostException(); + } catch (NoRouteToHostException | SocketException | IOException e) { + e.printStackTrace(); + } + } +} \ No newline at end of file diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/exceptions/splitMultiCatch/Simple_after.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/exceptions/split_multi_catch/Simple.after.java similarity index 100% rename from plugins/IntentionPowerPak/test/com/siyeh/ipp/exceptions/splitMultiCatch/Simple_after.java rename to plugins/InspectionGadgets/test/com/siyeh/igfixes/exceptions/split_multi_catch/Simple.after.java diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/exceptions/splitMultiCatch/Simple.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/exceptions/split_multi_catch/Simple.java similarity index 100% rename from plugins/IntentionPowerPak/test/com/siyeh/ipp/exceptions/splitMultiCatch/Simple.java rename to plugins/InspectionGadgets/test/com/siyeh/igfixes/exceptions/split_multi_catch/Simple.java diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/exceptions/splitMultiCatch/TypeAnno_after.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/exceptions/split_multi_catch/TypeAnno.after.java similarity index 100% rename from plugins/IntentionPowerPak/test/com/siyeh/ipp/exceptions/splitMultiCatch/TypeAnno_after.java rename to plugins/InspectionGadgets/test/com/siyeh/igfixes/exceptions/split_multi_catch/TypeAnno.after.java diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/exceptions/splitMultiCatch/TypeAnno.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/exceptions/split_multi_catch/TypeAnno.java similarity index 100% rename from plugins/IntentionPowerPak/test/com/siyeh/ipp/exceptions/splitMultiCatch/TypeAnno.java rename to plugins/InspectionGadgets/test/com/siyeh/igfixes/exceptions/split_multi_catch/TypeAnno.java diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/exceptions/SplitMultiCatchFixTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/exceptions/SplitMultiCatchFixTest.java new file mode 100644 index 000000000000..cbbda6c4a7aa --- /dev/null +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/exceptions/SplitMultiCatchFixTest.java @@ -0,0 +1,35 @@ +/* + * 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. + * 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. + */ +package com.siyeh.ig.fixes.exceptions; + +import com.siyeh.InspectionGadgetsBundle; +import com.siyeh.ig.IGQuickFixesTestCase; +import com.siyeh.ig.exceptions.SplitMultiCatchInspection; + +public class SplitMultiCatchFixTest extends IGQuickFixesTestCase { + @Override + protected void setUp() throws Exception { + super.setUp(); + myFixture.enableInspections(new SplitMultiCatchInspection()); + myDefaultHint = InspectionGadgetsBundle.message("split.multi.catch.descriptor"); + myRelativePath = "exceptions/split_multi_catch"; + } + + public void testSimple() { doTest(); } + public void testTypeAnno() { doTest(); } + public void testAtParameter() { doTest(); } + public void testAtCodeBlock() { assertQuickfixNotAvailable(); } +} diff --git a/plugins/IntentionPowerPak/src/META-INF/IntentionPowerPack.xml b/plugins/IntentionPowerPak/src/META-INF/IntentionPowerPack.xml index 0b5902ab276f..9296de9c5bf0 100644 --- a/plugins/IntentionPowerPak/src/META-INF/IntentionPowerPack.xml +++ b/plugins/IntentionPowerPak/src/META-INF/IntentionPowerPack.xml @@ -481,11 +481,6 @@ com.siyeh.IntentionPowerPackBundle intention.category.try.statements - - com.siyeh.ipp.exceptions.SplitMultiCatchIntention - com.siyeh.IntentionPowerPackBundle - intention.category.try.statements - com.siyeh.ipp.exceptions.ReplaceArmWithTryFinallyIntention com.siyeh.IntentionPowerPackBundle diff --git a/plugins/IntentionPowerPak/src/com/siyeh/IntentionPowerPackBundle.properties b/plugins/IntentionPowerPak/src/com/siyeh/IntentionPowerPackBundle.properties index 0ea0f4b6036f..ec79b02c346c 100644 --- a/plugins/IntentionPowerPak/src/com/siyeh/IntentionPowerPackBundle.properties +++ b/plugins/IntentionPowerPak/src/com/siyeh/IntentionPowerPackBundle.properties @@ -152,8 +152,6 @@ expand.to.normal.annotation.intention.name=Add 'value=' to annotation attribute annotate.overridden.methods.intention.family.name=Annotate overriding methods and their parameters annotate.overridden.methods.intention.method.name=Annotate overriding methods as ''{0}'' annotate.overridden.methods.intention.parameters.name=Annotate same parameter of overriding methods as ''{0}'' -split.multi.catch.intention.name=Split multi-catch into separate 'catch' blocks -split.multi.catch.intention.family.name=Split Multi-Catch into Separate Catch Blocks replace.arm.with.try.finally.intention.name=Replace 'try-with-resources' with 'try finally' replace.arm.with.try.finally.intention.family.name=Replace Try-With-Resources with Try-Finally merge.nested.try.statements.intention.name=Merge nested 'try' statements diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/exceptions/MulticatchPredicate.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/exceptions/MulticatchPredicate.java deleted file mode 100644 index 5866835ccd43..000000000000 --- a/plugins/IntentionPowerPak/src/com/siyeh/ipp/exceptions/MulticatchPredicate.java +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright 2011-2013 Bas Leijdekkers - * - * 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. - */ -package com.siyeh.ipp.exceptions; - -import com.intellij.psi.*; -import com.siyeh.ipp.base.PsiElementPredicate; - -class MulticatchPredicate implements PsiElementPredicate { - - public boolean satisfiedBy(PsiElement element) { - if (element instanceof PsiCodeBlock) { - return false; - } - final PsiElement parent = element.getParent(); - if (!(parent instanceof PsiCatchSection)) { - return false; - } - final PsiCatchSection catchSection = (PsiCatchSection)parent; - final PsiType type = catchSection.getCatchType(); - return type instanceof PsiDisjunctionType; - } -} diff --git a/plugins/IntentionPowerPak/src/com/siyeh/ipp/exceptions/SplitMultiCatchIntention.java b/plugins/IntentionPowerPak/src/com/siyeh/ipp/exceptions/SplitMultiCatchIntention.java deleted file mode 100644 index d6de66af803a..000000000000 --- a/plugins/IntentionPowerPak/src/com/siyeh/ipp/exceptions/SplitMultiCatchIntention.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Copyright 2011 Bas Leijdekkers - * - * 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. - */ -package com.siyeh.ipp.exceptions; - -import com.intellij.psi.*; -import com.intellij.psi.codeStyle.JavaCodeStyleManager; -import com.intellij.util.IncorrectOperationException; -import com.siyeh.ipp.base.Intention; -import com.siyeh.ipp.base.PsiElementPredicate; -import org.jetbrains.annotations.NotNull; - -import static com.intellij.util.ObjectUtils.assertNotNull; - -public class SplitMultiCatchIntention extends Intention { - - @NotNull - @Override - protected PsiElementPredicate getElementPredicate() { - return new MulticatchPredicate(); - } - - @Override - protected void processIntention(@NotNull PsiElement element) throws IncorrectOperationException { - final PsiElement parent = element.getParent(); - if (!(parent instanceof PsiCatchSection)) { - return; - } - final PsiCatchSection catchSection = (PsiCatchSection)parent; - final PsiElement grandParent = catchSection.getParent(); - if (!(grandParent instanceof PsiTryStatement)) { - return; - } - final PsiParameter parameter = catchSection.getParameter(); - if (parameter == null) { - return; - } - final PsiType type = parameter.getType(); - if (!(type instanceof PsiDisjunctionType)) { - return; - } - - final PsiElementFactory factory = JavaPsiFacade.getElementFactory(element.getProject()); - final JavaCodeStyleManager styleManager = JavaCodeStyleManager.getInstance(element.getProject()); - for (PsiType disjunction : ((PsiDisjunctionType)type).getDisjunctions()) { - final PsiCatchSection copy = (PsiCatchSection)catchSection.copy(); - - final PsiTypeElement typeElement = assertNotNull(assertNotNull(copy.getParameter()).getTypeElement()); - final PsiTypeElement newTypeElement = factory.createTypeElementFromText(disjunction.getCanonicalText(true), catchSection); - final PsiElement replaced = typeElement.replace(newTypeElement); - - grandParent.addBefore(copy, catchSection); - styleManager.shortenClassReferences(replaced); - } - - catchSection.delete(); - } -} diff --git a/plugins/IntentionPowerPak/src/intentionDescriptions/SplitMultiCatchIntention/after.java.template b/plugins/IntentionPowerPak/src/intentionDescriptions/SplitMultiCatchIntention/after.java.template deleted file mode 100644 index 8a3ba03bb373..000000000000 --- a/plugins/IntentionPowerPak/src/intentionDescriptions/SplitMultiCatchIntention/after.java.template +++ /dev/null @@ -1,16 +0,0 @@ -import java.io.IOException; -import java.net.*; - -public class X { - void f() { - try { - throw new NoRouteToHostException(); - } catch (NoRouteToHostException e) { - e.printStackTrace(); - } catch (SocketException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } - } -} \ No newline at end of file diff --git a/plugins/IntentionPowerPak/src/intentionDescriptions/SplitMultiCatchIntention/before.java.template b/plugins/IntentionPowerPak/src/intentionDescriptions/SplitMultiCatchIntention/before.java.template deleted file mode 100644 index 8d08a5453a3b..000000000000 --- a/plugins/IntentionPowerPak/src/intentionDescriptions/SplitMultiCatchIntention/before.java.template +++ /dev/null @@ -1,12 +0,0 @@ -import java.io.IOException; -import java.net.*; - -public class X { - void f() { - try { - throw new NoRouteToHostException(); - } catch (NoRouteToHostException | SocketException | IOException e) { - e.printStackTrace(); - } - } -} \ No newline at end of file diff --git a/plugins/IntentionPowerPak/src/intentionDescriptions/SplitMultiCatchIntention/description.html b/plugins/IntentionPowerPak/src/intentionDescriptions/SplitMultiCatchIntention/description.html deleted file mode 100644 index 0cc13ea9ca45..000000000000 --- a/plugins/IntentionPowerPak/src/intentionDescriptions/SplitMultiCatchIntention/description.html +++ /dev/null @@ -1,6 +0,0 @@ - - -This intention splits a multi-catch section into separate catch blocks. -

- - diff --git a/plugins/IntentionPowerPak/testSrc/com/siyeh/ipp/exceptions/SplitMultiCatchIntentionTest.java b/plugins/IntentionPowerPak/testSrc/com/siyeh/ipp/exceptions/SplitMultiCatchIntentionTest.java deleted file mode 100644 index 360ea156ba14..000000000000 --- a/plugins/IntentionPowerPak/testSrc/com/siyeh/ipp/exceptions/SplitMultiCatchIntentionTest.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright 2000-2013 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. - */ -package com.siyeh.ipp.exceptions; - -import com.siyeh.IntentionPowerPackBundle; -import com.siyeh.ipp.IPPTestCase; - -public class SplitMultiCatchIntentionTest extends IPPTestCase { - public void testSimple() { doTest(); } - public void testTypeAnno() { doTest(); } - - @Override - protected String getIntentionName() { - return IntentionPowerPackBundle.message("split.multi.catch.intention.name"); - } - - @Override - protected String getRelativePath() { - return "exceptions/splitMultiCatch"; - } -}