IDEA-192953 Fix replace switch with if on primitive wrapper uses equals

This commit is contained in:
Andrew Yefanov
2018-07-26 13:06:03 +02:00
committed by Bas Leijdekkers
parent a43ac282e4
commit a3805fbf38
8 changed files with 66 additions and 21 deletions
@@ -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;
@@ -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) {
sw<caret>itch (i) {
case '0':
System.out.println(i);
}
}
}
@@ -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);
}
}
}
@@ -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) {
sw<caret>itch (i) {
case '0':
System.out.println(i);
}
}
}
@@ -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);
}
}
}
@@ -0,0 +1,8 @@
class T {
void foo(Integer i) {
sw<caret>itch (i) {
case 0:
System.out.println(i);
}
}
}
@@ -0,0 +1,7 @@
class T {
void foo(Integer i) {
if (i == 0) {
System.out.println(i);
}
}
}
@@ -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'";