pointless boolean expression: help for quickfix to replace refs provided

This commit is contained in:
Anna Kozlova
2012-05-16 17:08:30 +04:00
parent c2821b3665
commit 676ef4b3f9
7 changed files with 127 additions and 6 deletions
@@ -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();
@@ -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;
}
}
@@ -0,0 +1,5 @@
class C {
void m() {
final boolean isCxf = true;
}
}
@@ -0,0 +1,8 @@
class C {
void m() {
final boolean isCxf = true;
if (!isCx<caret>f) {
//comment
}
}
}
@@ -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
}
}
}
@@ -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 || <caret>b) {
//comment
}
}
}
@@ -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();
}
}