EA-54183 (CCE: BooleanConstructorInspection$BooleanConstructorFix.doFix)

This commit is contained in:
Bas Leijdekkers
2014-02-21 20:15:03 +01:00
parent cf00d70317
commit 6cc57f8484
3 changed files with 91 additions and 4 deletions
@@ -20,7 +20,6 @@ import com.intellij.openapi.project.Project;
import com.intellij.pom.java.LanguageLevel;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiUtil;
import com.intellij.util.IncorrectOperationException;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspection;
import com.siyeh.ig.BaseInspectionVisitor;
@@ -84,8 +83,12 @@ public class BooleanConstructorInspection extends BaseInspection {
}
@Override
public void doFix(Project project, ProblemDescriptor descriptor) throws IncorrectOperationException {
final PsiNewExpression expression = (PsiNewExpression)descriptor.getPsiElement();
public void doFix(Project project, ProblemDescriptor descriptor) {
final PsiElement element = descriptor.getPsiElement().getParent();
if (!(element instanceof PsiNewExpression)) {
return;
}
final PsiNewExpression expression = (PsiNewExpression)element;
final PsiExpressionList argumentList = expression.getArgumentList();
if (argumentList == null) {
return;
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2012 JetBrains s.r.o.
* Copyright 2000-2014 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.
@@ -16,10 +16,14 @@
package com.siyeh.ig;
import com.intellij.codeInsight.intention.IntentionAction;
import com.intellij.ide.highlighter.JavaFileType;
import com.intellij.openapi.application.PluginPathManager;
import com.intellij.pom.java.LanguageLevel;
import com.intellij.testFramework.builders.JavaModuleFixtureBuilder;
import com.intellij.testFramework.fixtures.JavaCodeInsightFixtureTestCase;
import org.intellij.lang.annotations.Language;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
/**
* @author anna
@@ -30,6 +34,20 @@ public abstract class IGQuickFixesTestCase extends JavaCodeInsightFixtureTestCas
protected String myDefaultHint = null;
protected String myRelativePath = null;
@Override
protected void setUp() throws Exception {
super.setUp();
final BaseInspection inspection = getInspection();
if (inspection != null) {
myFixture.enableInspections(inspection);
}
}
protected BaseInspection getInspection() {
return null;
}
@Override
protected void tuneFixture(final JavaModuleFixtureBuilder builder) throws Exception {
builder.setLanguageLevel(LanguageLevel.JDK_1_7);
@@ -59,6 +77,29 @@ public abstract class IGQuickFixesTestCase extends JavaCodeInsightFixtureTestCas
myFixture.checkResultByFile(getRelativePath() + "/" + testName + ".after.java");
}
protected void doExpressionTest(
String hint,
@Language(value = "JAVA", prefix = "class X {{System.out.print(", suffix = ");}}") @NotNull @NonNls String before,
@Language(value = "JAVA", prefix = "class X {{System.out.print(", suffix = ");}}") @NotNull @NonNls String after) {
doTest(hint, "class X {{System.out.print(" + before + ");}}", "class X {{System.out.print(" + after + ");}}");
}
protected void doMemberTest(
String hint,
@Language(value = "JAVA", prefix = "class X {", suffix = "}") @NotNull @NonNls String before,
@Language(value = "JAVA", prefix = "class X {", suffix = "}") @NotNull @NonNls String after) {
doTest(hint, "class X {" + before + "}", "class X {" + after + "}");
}
protected void doTest(String hint, @Language("JAVA") @NotNull @NonNls String before, @Language("JAVA") @NotNull @NonNls String after) {
before = before.replace("/**/", "<caret>");
myFixture.configureByText(JavaFileType.INSTANCE, before);
final IntentionAction intention = myFixture.findSingleIntention(hint);
assertNotNull(intention);
myFixture.launchAction(intention);
myFixture.checkResult(after);
}
protected String getRelativePath() {
assertNotNull(myRelativePath);
return myRelativePath;
@@ -0,0 +1,43 @@
/*
* Copyright 2000-2014 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.performance;
import com.siyeh.InspectionGadgetsBundle;
import com.siyeh.ig.BaseInspection;
import com.siyeh.ig.IGQuickFixesTestCase;
import com.siyeh.ig.performance.BooleanConstructorInspection;
/**
* @author Bas Leijdekkers
*/
public class BooleanConstructorFixTest extends IGQuickFixesTestCase {
@Override
protected BaseInspection getInspection() {
return new BooleanConstructorInspection();
}
public void testSimple() {
doExpressionTest(InspectionGadgetsBundle.message("boolean.constructor.simplify.quickfix"),
"new Boolean/**/(true)", "Boolean.TRUE");
}
public void testSimple2() {
doMemberTest(InspectionGadgetsBundle.message("boolean.constructor.simplify.quickfix"),
"void m(boolean b) { Boolean c = new /**/Boolean(b); }",
"void m(boolean b) { Boolean c = Boolean.valueOf(b); }");
}
}