diff --git a/java/java-impl/src/com/intellij/codeInsight/intention/impl/InvertIfConditionAction.java b/java/java-impl/src/com/intellij/codeInsight/intention/impl/InvertIfConditionAction.java index dd00db5830ea..bc9e5651055c 100644 --- a/java/java-impl/src/com/intellij/codeInsight/intention/impl/InvertIfConditionAction.java +++ b/java/java-impl/src/com/intellij/codeInsight/intention/impl/InvertIfConditionAction.java @@ -16,7 +16,6 @@ package com.intellij.codeInsight.intention.impl; import com.intellij.codeInsight.CodeInsightBundle; -import com.intellij.codeInsight.CodeInsightServicesUtil; import com.intellij.codeInsight.intention.PsiElementBaseIntentionAction; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.editor.Editor; @@ -29,6 +28,7 @@ import com.intellij.psi.util.PsiTreeUtil; import com.intellij.psi.util.PsiUtil; import com.intellij.util.ArrayUtilRt; import com.intellij.util.IncorrectOperationException; +import com.siyeh.ig.psiutils.BoolUtils; import com.siyeh.ig.psiutils.CommentTracker; import org.jetbrains.annotations.NotNull; @@ -93,7 +93,9 @@ public class InvertIfConditionAction extends PsiElementBaseIntentionAction { ifStatement = setupBranches(ifStatement, controlFlow); if (condition != null) { - Objects.requireNonNull(ifStatement.getCondition()).replace(CodeInsightServicesUtil.invertCondition(condition)); + PsiExpression negatedExpression = + JavaPsiFacade.getElementFactory(project).createExpressionFromText(BoolUtils.getNegatedExpressionText(condition), condition); + Objects.requireNonNull(ifStatement.getCondition()).replace(negatedExpression); } formatIf(ifStatement); diff --git a/java/java-impl/src/com/intellij/codeInsight/template/postfix/util/JavaPostfixTemplatesUtils.java b/java/java-impl/src/com/intellij/codeInsight/template/postfix/util/JavaPostfixTemplatesUtils.java index 7f1ff9875bc8..db67b66abfaf 100644 --- a/java/java-impl/src/com/intellij/codeInsight/template/postfix/util/JavaPostfixTemplatesUtils.java +++ b/java/java-impl/src/com/intellij/codeInsight/template/postfix/util/JavaPostfixTemplatesUtils.java @@ -1,7 +1,6 @@ // Copyright 2000-2017 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.template.postfix.util; -import com.intellij.codeInsight.CodeInsightServicesUtil; import com.intellij.codeInsight.template.postfix.templates.PostfixTemplateExpressionSelector; import com.intellij.codeInsight.template.postfix.templates.PostfixTemplateExpressionSelectorBase; import com.intellij.codeInsight.template.postfix.templates.PostfixTemplatePsiInfo; @@ -18,6 +17,7 @@ import com.intellij.psi.util.PsiUtil; import com.intellij.refactoring.introduceVariable.IntroduceVariableBase; import com.intellij.util.Function; import com.intellij.util.containers.ContainerUtil; +import com.siyeh.ig.psiutils.BoolUtils; import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -128,7 +128,8 @@ public abstract class JavaPostfixTemplatesUtils { @Override public PsiExpression getNegatedExpression(@NotNull PsiElement element) { assert element instanceof PsiExpression; - return CodeInsightServicesUtil.invertCondition((PsiExpression)element); + String negatedExpressionText = BoolUtils.getNegatedExpressionText((PsiExpression)element); + return JavaPsiFacade.getElementFactory(element.getProject()).createExpressionFromText(negatedExpressionText, element); } }; diff --git a/java/java-impl/src/com/intellij/refactoring/invertBoolean/JavaInvertBooleanDelegate.java b/java/java-impl/src/com/intellij/refactoring/invertBoolean/JavaInvertBooleanDelegate.java index a426b7771237..d0e92a00e54b 100644 --- a/java/java-impl/src/com/intellij/refactoring/invertBoolean/JavaInvertBooleanDelegate.java +++ b/java/java-impl/src/com/intellij/refactoring/invertBoolean/JavaInvertBooleanDelegate.java @@ -15,7 +15,6 @@ */ package com.intellij.refactoring.invertBoolean; -import com.intellij.codeInsight.CodeInsightServicesUtil; import com.intellij.codeInsight.daemon.impl.RecursiveCallLineMarkerProvider; import com.intellij.ide.util.SuperMethodWarningUtil; import com.intellij.lang.java.JavaLanguage; @@ -33,6 +32,7 @@ import com.intellij.usageView.UsageInfo; import com.intellij.util.Query; import com.intellij.util.containers.HashSet; import com.intellij.util.containers.MultiMap; +import com.siyeh.ig.psiutils.BoolUtils; import org.jetbrains.annotations.NotNull; import java.util.Collection; @@ -172,11 +172,15 @@ public class JavaInvertBooleanDelegate extends InvertBooleanDelegate { if (expression instanceof PsiMethodReferenceExpression) { final PsiExpression callExpression = LambdaRefactoringUtil.convertToMethodCallInLambdaBody((PsiMethodReferenceExpression)expression); if (callExpression instanceof PsiCallExpression) { - callExpression.replace(CodeInsightServicesUtil.invertCondition(callExpression)); + PsiExpression negatedExpression = JavaPsiFacade.getElementFactory(callExpression.getProject()) + .createExpressionFromText(BoolUtils.getNegatedExpressionText(callExpression), callExpression); + callExpression.replace(negatedExpression); } } else if (!(expression.getParent() instanceof PsiExpressionStatement)) { - expression.replace(CodeInsightServicesUtil.invertCondition((PsiExpression)expression)); + PsiExpression negatedExpression = JavaPsiFacade.getElementFactory(expression.getProject()) + .createExpressionFromText(BoolUtils.getNegatedExpressionText((PsiExpression)expression), expression); + expression.replace(negatedExpression); } } diff --git a/java/java-tests/testData/codeInsight/invertIfCondition/afterRedundantParenthesis.java b/java/java-tests/testData/codeInsight/invertIfCondition/afterRedundantParenthesis.java new file mode 100644 index 000000000000..c491c8a87bdc --- /dev/null +++ b/java/java-tests/testData/codeInsight/invertIfCondition/afterRedundantParenthesis.java @@ -0,0 +1,13 @@ +// "Invert 'if' condition" "true" +class A { + + void method(Object x) { + if (x instanceof String) { + System.out.println(((String) x).toUpperCase()); + } + else { + System.out.println(x.toString()); + } + } + +} \ No newline at end of file diff --git a/java/java-tests/testData/codeInsight/invertIfCondition/beforeRedundantParenthesis.java b/java/java-tests/testData/codeInsight/invertIfCondition/beforeRedundantParenthesis.java new file mode 100644 index 000000000000..894d323af79c --- /dev/null +++ b/java/java-tests/testData/codeInsight/invertIfCondition/beforeRedundantParenthesis.java @@ -0,0 +1,12 @@ +// "Invert 'if' condition" "true" +class A { + + void method(Object x) { + if (!(x instanceof String)) { + System.out.println(x.toString()); + } else { + System.out.println(((String) x).toUpperCase()); + } + } + +} \ No newline at end of file diff --git a/java/java-tests/testData/inspection/commonIfParts/afterDifferentNamesFinalVar.java b/java/java-tests/testData/inspection/commonIfParts/afterDifferentNamesFinalVar.java index 0ee01f7750fb..4ee7aa2444f2 100644 --- a/java/java-tests/testData/inspection/commonIfParts/afterDifferentNamesFinalVar.java +++ b/java/java-tests/testData/inspection/commonIfParts/afterDifferentNamesFinalVar.java @@ -11,7 +11,7 @@ public class IfStatementWithIdenticalBranches { int work() { final int y = getX(); - if (!true) { + if (false) { System.out.println(y); } return y; diff --git a/java/java-tests/testData/inspection/commonIfParts/afterThenRemove.java b/java/java-tests/testData/inspection/commonIfParts/afterThenRemove.java index 87b30491a5a0..4f15f42afe57 100644 --- a/java/java-tests/testData/inspection/commonIfParts/afterThenRemove.java +++ b/java/java-tests/testData/inspection/commonIfParts/afterThenRemove.java @@ -11,7 +11,7 @@ public class IfStatementWithIdenticalBranches { int work() { int x = getX(); - if (!true) { + if (false) { return x; } } diff --git a/java/openapi/src/com/intellij/codeInsight/CodeInsightServicesUtil.java b/java/openapi/src/com/intellij/codeInsight/CodeInsightServicesUtil.java index a93a15836113..8ebb7f26c370 100644 --- a/java/openapi/src/com/intellij/codeInsight/CodeInsightServicesUtil.java +++ b/java/openapi/src/com/intellij/codeInsight/CodeInsightServicesUtil.java @@ -21,8 +21,12 @@ import com.intellij.psi.tree.IElementType; import com.intellij.util.IncorrectOperationException; /** - * @author ven + * Consider using instead + * {@link com.siyeh.ig.psiutils.BoolUtils#getNegatedExpressionText(com.intellij.psi.PsiExpression)} + * + * to be deleted in 2018.2 */ +@Deprecated public class CodeInsightServicesUtil { private static final Logger LOG = Logger.getInstance("#com.intellij.codeInsight.CodeInsightServicesUtil"); diff --git a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/BoolUtils.java b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/BoolUtils.java index 051e2881b38c..1521a3b29f2b 100644 --- a/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/BoolUtils.java +++ b/plugins/InspectionGadgets/InspectionGadgetsAnalysis/src/com/siyeh/ig/psiutils/BoolUtils.java @@ -80,7 +80,10 @@ public class BoolUtils { } if (expression instanceof PsiParenthesizedExpression) { final PsiParenthesizedExpression parenthesizedExpression = (PsiParenthesizedExpression)expression; - return '(' + getNegatedExpressionText(parenthesizedExpression.getExpression(), tracker) + ')'; + PsiExpression operand = parenthesizedExpression.getExpression(); + if (operand != null) { + return '(' + getNegatedExpressionText(operand, tracker) + ')'; + } } if (expression instanceof PsiConditionalExpression) { final PsiConditionalExpression conditionalExpression = (PsiConditionalExpression)expression; @@ -147,6 +150,12 @@ public class BoolUtils { return (newPrecedence > precedence) ? '(' + join + ')' : join; } } + if (expression instanceof PsiLiteralExpression) { + Object value = ((PsiLiteralExpression)expression).getValue(); + if (value instanceof Boolean) { + return String.valueOf(!((Boolean)value)); + } + } return '!' + ParenthesesUtils.getText(tracker.markUnchanged(expression), ParenthesesUtils.PREFIX_PRECEDENCE); }