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 72df2025348f..44cd9f7dc587 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,5 +1,5 @@ /* - * Copyright 2000-2014 JetBrains s.r.o. + * Copyright 2000-2015 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. @@ -25,6 +25,7 @@ import com.intellij.psi.search.LocalSearchScope; import com.intellij.psi.search.searches.ReferencesSearch; import com.intellij.psi.util.PsiUtil; import com.intellij.util.IncorrectOperationException; +import com.siyeh.ig.psiutils.ParenthesesUtils; import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; @@ -117,7 +118,9 @@ public class ConvertSwitchToIfIntention implements IntentionAction { else { hadSideEffects = false; declarationString = null; - expressionText = switchExpression.getText(); + expressionText = ParenthesesUtils.getPrecedence(switchExpression) > ParenthesesUtils.EQUALITY_PRECEDENCE + ? '(' + switchExpression.getText() + ')' + : switchExpression.getText(); } final PsiCodeBlock body = switchStatement.getBody(); if (body == null) { diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/convertSwitchToIf/after4.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/convertSwitchToIf/after4.java new file mode 100644 index 000000000000..8ab4527ff5a4 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/convertSwitchToIf/after4.java @@ -0,0 +1,13 @@ +// "Replace 'switch' with 'if'" "true" +class Precedence { + + void m() { + int a = 10; + if ((a & 1) == 0) { + System.out.println("0"); + + } else if ((a & 1) == 1) { + System.out.println("1"); + } + } +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/convertSwitchToIf/before4.java b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/convertSwitchToIf/before4.java new file mode 100644 index 000000000000..7301511c7ef4 --- /dev/null +++ b/java/java-tests/testData/codeInsight/daemonCodeAnalyzer/quickFix/convertSwitchToIf/before4.java @@ -0,0 +1,14 @@ +// "Replace 'switch' with 'if'" "true" +class Precedence { + + void m() { + int a = 10; + switch(a & 1) { + case 0: + System.out.println("0"); + break; + case 1: + System.out.println("1"); + } + } +} \ No newline at end of file