From a3805fbf38b2725ef91503aa5f53ecee44d74526 Mon Sep 17 00:00:00 2001 From: Andrew Yefanov <1134togo@gmail.com> Date: Thu, 26 Jul 2018 12:50:37 +0200 Subject: [PATCH] IDEA-192953 Fix replace switch with if on primitive wrapper uses equals --- .../quickfix/ConvertSwitchToIfIntention.java | 10 +++---- .../replaceSwitchToIf/ReplaceChar.java | 9 ++++++ .../replaceSwitchToIf/ReplaceChar_after.java | 8 ++++++ .../replaceSwitchToIf/ReplaceCharacter.java | 9 ++++++ .../ReplaceCharacter_after.java | 8 ++++++ .../replaceSwitchToIf/ReplaceInteger.java | 8 ++++++ .../ReplaceInteger_after.java | 7 +++++ .../ReplaceSwitchWithIflIntentionTest.java | 28 +++++++++---------- 8 files changed, 66 insertions(+), 21 deletions(-) create mode 100644 plugins/IntentionPowerPak/test/com/siyeh/ipp/switchtoif/replaceSwitchToIf/ReplaceChar.java create mode 100644 plugins/IntentionPowerPak/test/com/siyeh/ipp/switchtoif/replaceSwitchToIf/ReplaceChar_after.java create mode 100644 plugins/IntentionPowerPak/test/com/siyeh/ipp/switchtoif/replaceSwitchToIf/ReplaceCharacter.java create mode 100644 plugins/IntentionPowerPak/test/com/siyeh/ipp/switchtoif/replaceSwitchToIf/ReplaceCharacter_after.java create mode 100644 plugins/IntentionPowerPak/test/com/siyeh/ipp/switchtoif/replaceSwitchToIf/ReplaceInteger.java create mode 100644 plugins/IntentionPowerPak/test/com/siyeh/ipp/switchtoif/replaceSwitchToIf/ReplaceInteger_after.java diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ConvertSwitchToIfIntention.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ConvertSwitchToIfIntention.java index 641280fe3cd7..c4612dbbf74a 100644 --- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ConvertSwitchToIfIntention.java +++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/ConvertSwitchToIfIntention.java @@ -1,6 +1,4 @@ -/* - * 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. - */ +// 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.intellij.codeInsight.daemon.impl.quickfix; import com.intellij.codeInsight.intention.IntentionAction; @@ -12,8 +10,8 @@ import com.intellij.psi.controlFlow.*; import com.intellij.psi.search.LocalSearchScope; import com.intellij.psi.search.searches.ReferencesSearch; import com.intellij.psi.util.PsiUtil; +import com.intellij.psi.util.TypeConversionUtil; import com.intellij.refactoring.util.RefactoringUtil; -import com.intellij.util.IncorrectOperationException; import com.siyeh.ig.psiutils.CommentTracker; import com.siyeh.ig.psiutils.ParenthesesUtils; import org.jetbrains.annotations.NonNls; @@ -50,7 +48,7 @@ public class ConvertSwitchToIfIntention implements IntentionAction { } @Override - public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException { + public void invoke(@NotNull Project project, Editor editor, PsiFile file) { doProcessIntention(mySwitchExpression); } @@ -80,7 +78,7 @@ public class ConvertSwitchToIfIntention implements IntentionAction { boolean useEquals = isSwitchOnString; if (!useEquals) { final PsiClass aClass = PsiUtil.resolveClassInType(switchExpressionType); - useEquals = aClass != null && !aClass.isEnum(); + useEquals = aClass != null && !aClass.isEnum() && !TypeConversionUtil.isPrimitiveWrapper(aClass.getQualifiedName()); } final String declarationString; final boolean hadSideEffects; diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/switchtoif/replaceSwitchToIf/ReplaceChar.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/switchtoif/replaceSwitchToIf/ReplaceChar.java new file mode 100644 index 000000000000..d39cbdcaa72a --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/switchtoif/replaceSwitchToIf/ReplaceChar.java @@ -0,0 +1,9 @@ +// 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. +class T { + void foo(char i) { + switch (i) { + case '0': + System.out.println(i); + } + } +} \ No newline at end of file diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/switchtoif/replaceSwitchToIf/ReplaceChar_after.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/switchtoif/replaceSwitchToIf/ReplaceChar_after.java new file mode 100644 index 000000000000..de4b34744dd4 --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/switchtoif/replaceSwitchToIf/ReplaceChar_after.java @@ -0,0 +1,8 @@ +// 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. +class T { + void foo(char i) { + if (i == '0') { + System.out.println(i); + } + } +} \ No newline at end of file diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/switchtoif/replaceSwitchToIf/ReplaceCharacter.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/switchtoif/replaceSwitchToIf/ReplaceCharacter.java new file mode 100644 index 000000000000..97016878e284 --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/switchtoif/replaceSwitchToIf/ReplaceCharacter.java @@ -0,0 +1,9 @@ +// 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. +class T { + void foo(Character i) { + switch (i) { + case '0': + System.out.println(i); + } + } +} \ No newline at end of file diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/switchtoif/replaceSwitchToIf/ReplaceCharacter_after.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/switchtoif/replaceSwitchToIf/ReplaceCharacter_after.java new file mode 100644 index 000000000000..51a956b0b9a8 --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/switchtoif/replaceSwitchToIf/ReplaceCharacter_after.java @@ -0,0 +1,8 @@ +// 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. +class T { + void foo(Character i) { + if (i == '0') { + System.out.println(i); + } + } +} \ No newline at end of file diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/switchtoif/replaceSwitchToIf/ReplaceInteger.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/switchtoif/replaceSwitchToIf/ReplaceInteger.java new file mode 100644 index 000000000000..56917c013ec7 --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/switchtoif/replaceSwitchToIf/ReplaceInteger.java @@ -0,0 +1,8 @@ +class T { + void foo(Integer i) { + switch (i) { + case 0: + System.out.println(i); + } + } +} \ No newline at end of file diff --git a/plugins/IntentionPowerPak/test/com/siyeh/ipp/switchtoif/replaceSwitchToIf/ReplaceInteger_after.java b/plugins/IntentionPowerPak/test/com/siyeh/ipp/switchtoif/replaceSwitchToIf/ReplaceInteger_after.java new file mode 100644 index 000000000000..0ef5fc05950e --- /dev/null +++ b/plugins/IntentionPowerPak/test/com/siyeh/ipp/switchtoif/replaceSwitchToIf/ReplaceInteger_after.java @@ -0,0 +1,7 @@ +class T { + void foo(Integer i) { + if (i == 0) { + System.out.println(i); + } + } +} \ No newline at end of file diff --git a/plugins/IntentionPowerPak/testSrc/com/siyeh/ipp/switchtoif/ReplaceSwitchWithIflIntentionTest.java b/plugins/IntentionPowerPak/testSrc/com/siyeh/ipp/switchtoif/ReplaceSwitchWithIflIntentionTest.java index 05c4f36ef19d..2b2b7cc45eaf 100644 --- a/plugins/IntentionPowerPak/testSrc/com/siyeh/ipp/switchtoif/ReplaceSwitchWithIflIntentionTest.java +++ b/plugins/IntentionPowerPak/testSrc/com/siyeh/ipp/switchtoif/ReplaceSwitchWithIflIntentionTest.java @@ -1,18 +1,4 @@ -/* - * 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. - */ +// 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.ipp.switchtoif; import com.siyeh.ipp.IPPTestCase; @@ -23,6 +9,18 @@ public class ReplaceSwitchWithIflIntentionTest extends IPPTestCase { doTest(); } + public void testReplaceInteger() { + doTest(); + } + + public void testReplaceChar() { + doTest(); + } + + public void testReplaceCharacter() { + doTest(); + } + @Override protected String getIntentionName() { return "Replace 'switch' with 'if'";