From 676ef4b3f91b8eae7c8adda29ae64e63ca5501f7 Mon Sep 17 00:00:00 2001 From: Anna Kozlova Date: Wed, 16 May 2012 16:17:29 +0400 Subject: [PATCH] pointless boolean expression: help for quickfix to replace refs provided --- .../SimplifyBooleanExpressionFix.java | 12 ++++-- .../PointlessBooleanExpressionInspection.java | 23 ++++++++++- .../pointlessboolean/Negation.after.java | 5 +++ .../igfixes/pointlessboolean/Negation.java | 8 ++++ .../pointlessboolean/Polyadic.after.java | 23 +++++++++++ .../igfixes/pointlessboolean/Polyadic.java | 23 +++++++++++ .../PointlessBooleanExpressionFixTest.java | 39 +++++++++++++++++++ 7 files changed, 127 insertions(+), 6 deletions(-) create mode 100644 plugins/InspectionGadgets/test/com/siyeh/igfixes/pointlessboolean/Negation.after.java create mode 100644 plugins/InspectionGadgets/test/com/siyeh/igfixes/pointlessboolean/Negation.java create mode 100644 plugins/InspectionGadgets/test/com/siyeh/igfixes/pointlessboolean/Polyadic.after.java create mode 100644 plugins/InspectionGadgets/test/com/siyeh/igfixes/pointlessboolean/Polyadic.java create mode 100644 plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/PointlessBooleanExpressionFixTest.java diff --git a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/SimplifyBooleanExpressionFix.java b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/SimplifyBooleanExpressionFix.java index 547febd290b2..4efc8bb9ccb2 100644 --- a/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/SimplifyBooleanExpressionFix.java +++ b/java/java-impl/src/com/intellij/codeInsight/daemon/impl/quickfix/SimplifyBooleanExpressionFix.java @@ -73,14 +73,18 @@ public class SimplifyBooleanExpressionFix implements IntentionAction { if (!isAvailable(project, editor, file)) return; LOG.assertTrue(mySubExpression.isValid()); if (!CodeInsightUtilBase.preparePsiElementForWrite(mySubExpression)) return; + simplifyExpression(project, mySubExpression, mySubExpressionValue); + } + + public static void simplifyExpression(Project project, final PsiExpression subExpression, final Boolean subExpressionValue) { PsiExpression expression; - if (mySubExpressionValue == null) { - expression = mySubExpression; + if (subExpressionValue == null) { + expression = subExpression; } else { PsiExpression constExpression = JavaPsiFacade.getInstance(project).getElementFactory() - .createExpressionFromText(Boolean.toString(mySubExpressionValue.booleanValue()), mySubExpression); - expression = (PsiExpression)mySubExpression.replace(constExpression); + .createExpressionFromText(Boolean.toString(subExpressionValue.booleanValue()), subExpression); + expression = (PsiExpression)subExpression.replace(constExpression); } while (expression.getParent() instanceof PsiExpression) { expression = (PsiExpression)expression.getParent(); diff --git a/plugins/InspectionGadgets/src/com/siyeh/ig/controlflow/PointlessBooleanExpressionInspection.java b/plugins/InspectionGadgets/src/com/siyeh/ig/controlflow/PointlessBooleanExpressionInspection.java index 36244c19f6e6..670b869073eb 100644 --- a/plugins/InspectionGadgets/src/com/siyeh/ig/controlflow/PointlessBooleanExpressionInspection.java +++ b/plugins/InspectionGadgets/src/com/siyeh/ig/controlflow/PointlessBooleanExpressionInspection.java @@ -207,7 +207,7 @@ public class PointlessBooleanExpressionInspection extends BaseInspection { return new BooleanLiteralComparisonFix(); } - private static class BooleanLiteralComparisonFix + private class BooleanLiteralComparisonFix extends InspectionGadgetsFix { @NotNull public String getName() { @@ -219,7 +219,26 @@ public class PointlessBooleanExpressionInspection extends BaseInspection { public void doFix(Project project, ProblemDescriptor descriptor) throws IncorrectOperationException { final PsiElement element = descriptor.getPsiElement(); - SimplifyBooleanExpressionFix.simplifyExpression((PsiExpression)element); + processSubExpressions(project, (PsiExpression)element); + } + + private boolean processSubExpressions(Project project, PsiExpression element) { + if (element instanceof PsiPrefixExpression) { + SimplifyBooleanExpressionFix.simplifyExpression(project, element, isTrue(element)); + } else { + if (element instanceof PsiPolyadicExpression) { + for (PsiExpression operand :((PsiPolyadicExpression)element).getOperands()) { + Boolean bool = isTrue(operand) ? Boolean.TRUE : isFalse(operand) ? Boolean.FALSE : null; + if (bool != null) { + SimplifyBooleanExpressionFix.simplifyExpression(project, operand, bool); + return true; + } else { + if (processSubExpressions(project, operand)) return true; + } + } + } + } + return false; } } diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/pointlessboolean/Negation.after.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/pointlessboolean/Negation.after.java new file mode 100644 index 000000000000..84bb80545cd9 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/pointlessboolean/Negation.after.java @@ -0,0 +1,5 @@ +class C { + void m() { + final boolean isCxf = true; + } +} diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/pointlessboolean/Negation.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/pointlessboolean/Negation.java new file mode 100644 index 000000000000..080d62d08462 --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/pointlessboolean/Negation.java @@ -0,0 +1,8 @@ +class C { + void m() { + final boolean isCxf = true; + if (!isCxf) { + //comment + } + } +} diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/pointlessboolean/Polyadic.after.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/pointlessboolean/Polyadic.after.java new file mode 100644 index 000000000000..5815bf26e47b --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/pointlessboolean/Polyadic.after.java @@ -0,0 +1,23 @@ +/* + * 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. + */ +class C { + void m(boolean b) { + final boolean isCxf = true; + if (b) { + //comment + } + } +} diff --git a/plugins/InspectionGadgets/test/com/siyeh/igfixes/pointlessboolean/Polyadic.java b/plugins/InspectionGadgets/test/com/siyeh/igfixes/pointlessboolean/Polyadic.java new file mode 100644 index 000000000000..4afc3d0d59bf --- /dev/null +++ b/plugins/InspectionGadgets/test/com/siyeh/igfixes/pointlessboolean/Polyadic.java @@ -0,0 +1,23 @@ +/* + * 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. + */ +class C { + void m(boolean b) { + final boolean isCxf = true; + if (!isCxf || b) { + //comment + } + } +} diff --git a/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/PointlessBooleanExpressionFixTest.java b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/PointlessBooleanExpressionFixTest.java new file mode 100644 index 000000000000..f3ace921a257 --- /dev/null +++ b/plugins/InspectionGadgets/testsrc/com/siyeh/ig/fixes/PointlessBooleanExpressionFixTest.java @@ -0,0 +1,39 @@ +/* + * 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. + */ + +package com.siyeh.ig.fixes; + +import com.siyeh.InspectionGadgetsBundle; +import com.siyeh.ig.IGQuickFixesTestCase; +import com.siyeh.ig.controlflow.PointlessBooleanExpressionInspection; + +public class PointlessBooleanExpressionFixTest extends IGQuickFixesTestCase { + @Override + public void setUp() throws Exception { + super.setUp(); + myFixture.enableInspections(new PointlessBooleanExpressionInspection()); + myRelativePath = "pointlessboolean"; + myDefaultHint = InspectionGadgetsBundle.message("pointless.nullcheck.simplify.quickfix"); + } + + public void testNegation() { + doTest(); + } + + public void testPolyadic() { + doTest(); + } +}