From 516f50339d5309192ec6123bcbb282771f78c9be Mon Sep 17 00:00:00 2001 From: Tagir Valeev Date: Thu, 11 May 2017 17:46:20 +0700 Subject: [PATCH] DataFlowInspectionBase: fixes extracted and moved to separate package --- .../dataFlow/DataFlowInspectionBase.java | 88 ++----------------- .../dataFlow/fix/RedundantInstanceofFix.java | 47 ++++++++++ .../fix/ReplaceWithConstantValueFix.java | 67 ++++++++++++++ .../ReplaceWithObjectsEqualsFix.java | 9 +- .../dataFlow/fix/SimplifyToAssignmentFix.java | 58 ++++++++++++ .../src/messages/InspectionsBundle.properties | 2 +- 6 files changed, 185 insertions(+), 86 deletions(-) create mode 100644 java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/fix/RedundantInstanceofFix.java create mode 100644 java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/fix/ReplaceWithConstantValueFix.java rename java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/{ => fix}/ReplaceWithObjectsEqualsFix.java (89%) create mode 100644 java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/fix/SimplifyToAssignmentFix.java diff --git a/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/DataFlowInspectionBase.java b/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/DataFlowInspectionBase.java index daac9b0135b4..bcf417ec9bf0 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/DataFlowInspectionBase.java +++ b/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/DataFlowInspectionBase.java @@ -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 #ref #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 #ref #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 diff --git a/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/fix/RedundantInstanceofFix.java b/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/fix/RedundantInstanceofFix.java new file mode 100644 index 000000000000..ae609f7c5371 --- /dev/null +++ b/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/fix/RedundantInstanceofFix.java @@ -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); + } + } +} diff --git a/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/fix/ReplaceWithConstantValueFix.java b/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/fix/ReplaceWithConstantValueFix.java new file mode 100644 index 000000000000..66ad49aa0f83 --- /dev/null +++ b/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/fix/ReplaceWithConstantValueFix.java @@ -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); + } + } +} diff --git a/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/ReplaceWithObjectsEqualsFix.java b/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/fix/ReplaceWithObjectsEqualsFix.java similarity index 89% rename from java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/ReplaceWithObjectsEqualsFix.java rename to java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/fix/ReplaceWithObjectsEqualsFix.java index dc40a0eb9339..eafa17ddcb5b 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/ReplaceWithObjectsEqualsFix.java +++ b/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/fix/ReplaceWithObjectsEqualsFix.java @@ -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)) { diff --git a/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/fix/SimplifyToAssignmentFix.java b/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/fix/SimplifyToAssignmentFix.java new file mode 100644 index 000000000000..59f82b4847b2 --- /dev/null +++ b/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/fix/SimplifyToAssignmentFix.java @@ -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)); + } +} diff --git a/platform/platform-resources-en/src/messages/InspectionsBundle.properties b/platform/platform-resources-en/src/messages/InspectionsBundle.properties index 514fbd7612d4..253da07f65e4 100644 --- a/platform/platform-resources-en/src/messages/InspectionsBundle.properties +++ b/platform/platform-resources-en/src/messages/InspectionsBundle.properties @@ -48,7 +48,7 @@ inspection.contract.display.name=Contract issues inspection.data.flow.nullable.quickfix.option=Suggest @Nullable annotation for methods that may possibly return null and
report nullable values passed to non-annotated parameters inspection.data.flow.true.asserts.option=Don't report assertions with condition statically proven to be always true 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