DataFlowInspectionBase: fixes extracted and moved to separate package

This commit is contained in:
Tagir Valeev
2017-05-11 17:48:21 +07:00
parent 8d6a1b1e27
commit 516f50339d
6 changed files with 185 additions and 86 deletions
@@ -24,6 +24,10 @@ import com.intellij.codeInsight.daemon.impl.quickfix.SimplifyBooleanExpressionFi
import com.intellij.codeInsight.intention.impl.AddNotNullAnnotationFix;
import com.intellij.codeInsight.intention.impl.AddNullableAnnotationFix;
import com.intellij.codeInspection.*;
import com.intellij.codeInspection.dataFlow.fix.RedundantInstanceofFix;
import com.intellij.codeInspection.dataFlow.fix.ReplaceWithConstantValueFix;
import com.intellij.codeInspection.dataFlow.fix.ReplaceWithObjectsEqualsFix;
import com.intellij.codeInspection.dataFlow.fix.SimplifyToAssignmentFix;
import com.intellij.codeInspection.dataFlow.instructions.*;
import com.intellij.codeInspection.dataFlow.value.DfaConstValue;
import com.intellij.codeInspection.dataFlow.value.DfaUnknownValue;
@@ -42,7 +46,6 @@ import com.intellij.psi.util.InheritanceUtil;
import com.intellij.psi.util.PsiTreeUtil;
import com.intellij.psi.util.PsiUtil;
import com.intellij.psi.util.TypeConversionUtil;
import com.intellij.refactoring.extractMethod.ExtractMethodUtil;
import com.intellij.util.*;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.containers.MultiMap;
@@ -532,38 +535,8 @@ public class DataFlowInspectionBase extends BaseJavaBatchLocalInspectionTool {
continue;
}
holder.registerProblem(ref, "Value <code>#ref</code> #loc is always '" + presentableName + "'", new LocalQuickFix() {
@NotNull
@Override
public String getName() {
return "Replace with '" + presentableName + "'";
}
@NotNull
@Override
public String getFamilyName() {
return "Replace with constant value";
}
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
PsiElement problemElement = descriptor.getPsiElement();
if (problemElement == null) return;
PsiMethodCallExpression call = problemElement.getParent() instanceof PsiExpressionList &&
problemElement.getParent().getParent() instanceof PsiMethodCallExpression ?
(PsiMethodCallExpression)problemElement.getParent().getParent() :
null;
PsiMethod targetMethod = call == null ? null : call.resolveMethod();
JavaPsiFacade facade = JavaPsiFacade.getInstance(project);
problemElement.replace(facade.getElementFactory().createExpressionFromText(exprText, null));
if (targetMethod != null) {
ExtractMethodUtil.addCastsToEnsureResolveTarget(targetMethod, call);
}
}
});
holder.registerProblem(ref, "Value <code>#ref</code> #loc is always '" + presentableName + "'",
new ReplaceWithConstantValueFix(presentableName, exprText));
}
}
@@ -933,36 +906,7 @@ public class DataFlowInspectionBase extends BaseJavaBatchLocalInspectionTool {
@NotNull
protected static LocalQuickFix createSimplifyToAssignmentFix() {
return new LocalQuickFix() {
@NotNull
@Override
public String getName() {
return InspectionsBundle.message("inspection.data.flow.simplify.to.assignment.quickfix.name");
}
@NotNull
@Override
public String getFamilyName() {
return InspectionsBundle.message("inspection.data.flow.simplify.boolean.expression.quickfix");
}
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
final PsiElement psiElement = descriptor.getPsiElement();
if (psiElement == null) return;
final PsiAssignmentExpression assignmentExpression = PsiTreeUtil.getParentOfType(psiElement, PsiAssignmentExpression.class);
if (assignmentExpression == null) {
return;
}
final PsiElementFactory factory = JavaPsiFacade.getElementFactory(project);
final String lExpressionText = assignmentExpression.getLExpression().getText();
final PsiExpression rExpression = assignmentExpression.getRExpression();
final String rExpressionText = rExpression != null ? rExpression.getText() : "";
assignmentExpression.replace(factory.createExpressionFromText(lExpressionText + " = " + rExpressionText, psiElement));
}
};
return new SimplifyToAssignmentFix();
}
private static SimplifyBooleanExpressionFix createIntention(PsiElement element, boolean value) {
@@ -982,24 +926,6 @@ public class DataFlowInspectionBase extends BaseJavaBatchLocalInspectionTool {
return fix;
}
private static class RedundantInstanceofFix implements LocalQuickFix {
@Override
@NotNull
public String getFamilyName() {
return InspectionsBundle.message("inspection.data.flow.redundant.instanceof.quickfix");
}
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
final PsiElement psiElement = descriptor.getPsiElement();
if (psiElement instanceof PsiInstanceOfExpression) {
PsiExpression compareToNull = JavaPsiFacade.getInstance(psiElement.getProject()).getElementFactory().
createExpressionFromText(((PsiInstanceOfExpression)psiElement).getOperand().getText() + " != null", psiElement.getParent());
psiElement.replace(compareToNull);
}
}
}
@Override
@NotNull
@@ -0,0 +1,47 @@
/*
* Copyright 2000-2017 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.intellij.codeInspection.dataFlow.fix;
import com.intellij.codeInspection.InspectionsBundle;
import com.intellij.codeInspection.LocalQuickFix;
import com.intellij.codeInspection.ProblemDescriptor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.JavaPsiFacade;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiExpression;
import com.intellij.psi.PsiInstanceOfExpression;
import org.jetbrains.annotations.NotNull;
/**
* @author peter
*/
public class RedundantInstanceofFix implements LocalQuickFix {
@Override
@NotNull
public String getFamilyName() {
return InspectionsBundle.message("inspection.data.flow.redundant.instanceof.quickfix");
}
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
final PsiElement psiElement = descriptor.getPsiElement();
if (psiElement instanceof PsiInstanceOfExpression) {
PsiExpression compareToNull = JavaPsiFacade.getInstance(psiElement.getProject()).getElementFactory().
createExpressionFromText(((PsiInstanceOfExpression)psiElement).getOperand().getText() + " != null", psiElement.getParent());
psiElement.replace(compareToNull);
}
}
}
@@ -0,0 +1,67 @@
/*
* Copyright 2000-2017 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.intellij.codeInspection.dataFlow.fix;
import com.intellij.codeInspection.LocalQuickFix;
import com.intellij.codeInspection.ProblemDescriptor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.refactoring.extractMethod.ExtractMethodUtil;
import org.jetbrains.annotations.NotNull;
/**
* @author peter
*/
public class ReplaceWithConstantValueFix implements LocalQuickFix {
private final String myPresentableName;
private final String myReplacementText;
public ReplaceWithConstantValueFix(String presentableName, String replacementText) {
myPresentableName = presentableName;
myReplacementText = replacementText;
}
@NotNull
@Override
public String getName() {
return "Replace with '" + myPresentableName + "'";
}
@NotNull
@Override
public String getFamilyName() {
return "Replace with constant value";
}
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
PsiElement problemElement = descriptor.getPsiElement();
if (problemElement == null) return;
PsiMethodCallExpression call = problemElement.getParent() instanceof PsiExpressionList &&
problemElement.getParent().getParent() instanceof PsiMethodCallExpression ?
(PsiMethodCallExpression)problemElement.getParent().getParent() :
null;
PsiMethod targetMethod = call == null ? null : call.resolveMethod();
JavaPsiFacade facade = JavaPsiFacade.getInstance(project);
problemElement.replace(facade.getElementFactory().createExpressionFromText(myReplacementText, null));
if (targetMethod != null) {
ExtractMethodUtil.addCastsToEnsureResolveTarget(targetMethod, call);
}
}
}
@@ -1,5 +1,5 @@
/*
* Copyright 2000-2016 JetBrains s.r.o.
* Copyright 2000-2017 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.
@@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.intellij.codeInspection.dataFlow;
package com.intellij.codeInspection.dataFlow.fix;
import com.intellij.codeInspection.LocalQuickFix;
import com.intellij.codeInspection.ProblemDescriptor;
@@ -30,7 +30,7 @@ import org.jetbrains.annotations.Nullable;
/**
* @author peter
*/
class ReplaceWithObjectsEqualsFix implements LocalQuickFix {
public class ReplaceWithObjectsEqualsFix implements LocalQuickFix {
private final String myQualifierText;
private final String myReplacementText;
@@ -67,7 +67,8 @@ class ReplaceWithObjectsEqualsFix implements LocalQuickFix {
}
@Nullable
static ReplaceWithObjectsEqualsFix createFix(@NotNull PsiMethodCallExpression call, @NotNull PsiReferenceExpression methodExpression) {
public static ReplaceWithObjectsEqualsFix createFix(@NotNull PsiMethodCallExpression call,
@NotNull PsiReferenceExpression methodExpression) {
if (!"equals".equals(methodExpression.getReferenceName()) ||
call.getArgumentList().getExpressions().length != 1 ||
!PsiUtil.getLanguageLevel(call).isAtLeast(LanguageLevel.JDK_1_7)) {
@@ -0,0 +1,58 @@
/*
* Copyright 2000-2017 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.intellij.codeInspection.dataFlow.fix;
import com.intellij.codeInspection.InspectionsBundle;
import com.intellij.codeInspection.LocalQuickFix;
import com.intellij.codeInspection.ProblemDescriptor;
import com.intellij.openapi.project.Project;
import com.intellij.psi.*;
import com.intellij.psi.util.PsiTreeUtil;
import org.jetbrains.annotations.NotNull;
/**
* @author peter
*/
public class SimplifyToAssignmentFix implements LocalQuickFix {
@NotNull
@Override
public String getName() {
return InspectionsBundle.message("inspection.data.flow.simplify.to.assignment.quickfix.name");
}
@NotNull
@Override
public String getFamilyName() {
return InspectionsBundle.message("inspection.data.flow.simplify.boolean.expression.quickfix");
}
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
final PsiElement psiElement = descriptor.getPsiElement();
if (psiElement == null) return;
final PsiAssignmentExpression assignmentExpression = PsiTreeUtil.getParentOfType(psiElement, PsiAssignmentExpression.class);
if (assignmentExpression == null) {
return;
}
final PsiElementFactory factory = JavaPsiFacade.getElementFactory(project);
final String lExpressionText = assignmentExpression.getLExpression().getText();
final PsiExpression rExpression = assignmentExpression.getRExpression();
final String rExpressionText = rExpression != null ? rExpression.getText() : "";
assignmentExpression.replace(factory.createExpressionFromText(lExpressionText + " = " + rExpressionText, psiElement));
}
}
@@ -48,7 +48,7 @@ inspection.contract.display.name=Contract issues
inspection.data.flow.nullable.quickfix.option=<html><body>Suggest @Nullable annotation for methods that may possibly return null and <br>report nullable values passed to non-annotated parameters</body></html>
inspection.data.flow.true.asserts.option=<html><body>Don't report assertions with condition statically proven to be always <code>true</code></body></html>
inspection.data.flow.redundant.instanceof.quickfix=Replace with != null
inspection.data.flow.simplify.boolean.expression.quickfix=Simplify Boolean Expression
inspection.data.flow.simplify.boolean.expression.quickfix=Simplify boolean expression
inspection.data.flow.simplify.to.assignment.quickfix.name=Simplify to normal assignment
configure.annotations.option=Configure annotations
configure.checker.option.button=Configure Assert/Check Methods