mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-04-22 06:21:25 +07:00
DFA - added Boolean.TRUE/FALSE.equals() quick fix to unbox boolean nullable variable
GitOrigin-RevId: 45952b11139cf435e218ddd9c3ba57e237230eed
This commit is contained in:
committed by
intellij-monorepo-bot
parent
445cb5d8d2
commit
a117b21680
@@ -362,6 +362,7 @@ replace.with.comparator=Replace with comparator
|
||||
replace.with.constant.value=Replace with constant value
|
||||
replace.with.expression.lambda=Replace with expression lambda
|
||||
replace.with.lambda=Replace with lambda
|
||||
replace.with.boolean.equals=Replace with Boolean.equals
|
||||
report.suspicious.but.possibly.correct.method.calls=&Report suspicious but possibly correct method calls
|
||||
report.when.interface.is.not.annotated.with.functional.interface=Report when interface is not annotated with @FunctionalInterface
|
||||
searching.for.overriding.methods=Searching for Overriding Methods
|
||||
|
||||
@@ -250,6 +250,10 @@ public abstract class DataFlowInspectionBase extends AbstractBaseJavaLocalInspec
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
protected @NotNull List<LocalQuickFix> createUnboxingNullableFixes(@NotNull PsiExpression qualifier, PsiExpression expression, boolean onTheFly) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
protected List<LocalQuickFix> createMethodReferenceNPEFixes(PsiMethodReferenceExpression methodRef, boolean onTheFly) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
@@ -633,7 +637,10 @@ public abstract class DataFlowInspectionBase extends AbstractBaseJavaLocalInspec
|
||||
if (anchor instanceof PsiTypeCastExpression && anchor.getType() instanceof PsiPrimitiveType) {
|
||||
anchor = Objects.requireNonNull(((PsiTypeCastExpression)anchor).getOperand());
|
||||
}
|
||||
reporter.registerProblem(anchor, problem.getMessage(expressions));
|
||||
if (anchor != null) {
|
||||
LocalQuickFix[] fixes = createUnboxingNullableFixes(anchor, expression, reporter.isOnTheFly()).toArray(LocalQuickFix.EMPTY_ARRAY);
|
||||
reporter.registerProblem(anchor, problem.getMessage(expressions), fixes);
|
||||
}
|
||||
});
|
||||
NullabilityProblemKind.nullableFunctionReturn.ifMyProblem(
|
||||
problem, expr -> reporter.registerProblem(expression == null ? expr : expression, problem.getMessage(expressions)));
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
// Copyright 2000-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
|
||||
package com.intellij.codeInspection.dataFlow.fix;
|
||||
|
||||
import com.intellij.codeInspection.CommonQuickFixBundle;
|
||||
import com.intellij.codeInspection.LocalQuickFix;
|
||||
import com.intellij.codeInspection.ProblemDescriptor;
|
||||
import com.intellij.java.analysis.JavaAnalysisBundle;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.psi.*;
|
||||
import com.intellij.psi.codeStyle.JavaCodeStyleManager;
|
||||
import com.intellij.util.ObjectUtils;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class ReplaceWithBooleanEqualsFix implements LocalQuickFix {
|
||||
private final String myNewExprText;
|
||||
private final boolean myFalseIsAcceptable;
|
||||
private final SmartPsiElementPointer<PsiExpression> myExprToReplacePointer;
|
||||
|
||||
public ReplaceWithBooleanEqualsFix(@NotNull PsiExpression exprToReplace) {
|
||||
myNewExprText = exprToReplace.getText();
|
||||
PsiPrefixExpression parent = ObjectUtils.tryCast(exprToReplace.getParent(), PsiPrefixExpression.class);
|
||||
myFalseIsAcceptable = parent != null && parent.getOperationTokenType() == JavaTokenType.EXCL;
|
||||
myExprToReplacePointer = SmartPointerManager.getInstance(exprToReplace.getProject())
|
||||
.createSmartPsiElementPointer(myFalseIsAcceptable ? parent : exprToReplace);
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull String getName() {
|
||||
return CommonQuickFixBundle.message("fix.replace.with.x", createNewExprText());
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull String getFamilyName() {
|
||||
return JavaAnalysisBundle.message("replace.with.boolean.equals");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
|
||||
PsiExpression exprToReplace = myExprToReplacePointer.getElement();
|
||||
if (exprToReplace == null) return;
|
||||
PsiExpression newExpr = JavaPsiFacade.getElementFactory(project).createExpressionFromText(createNewExprText(), exprToReplace);
|
||||
JavaCodeStyleManager.getInstance(project).shortenClassReferences(exprToReplace.replace(newExpr));
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private String createNewExprText() {
|
||||
return "Boolean." + (myFalseIsAcceptable ? "FALSE" : "TRUE") + ".equals(" + myNewExprText + ")";
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@ import com.intellij.codeInsight.daemon.impl.quickfix.SimplifyBooleanExpressionFi
|
||||
import com.intellij.codeInsight.daemon.impl.quickfix.UnwrapSwitchLabelFix;
|
||||
import com.intellij.codeInspection.*;
|
||||
import com.intellij.codeInspection.dataFlow.fix.FindDfaProblemCauseFix;
|
||||
import com.intellij.codeInspection.dataFlow.fix.ReplaceWithBooleanEqualsFix;
|
||||
import com.intellij.codeInspection.dataFlow.fix.SurroundWithRequireNonNullFix;
|
||||
import com.intellij.codeInspection.nullable.NullableStuffInspection;
|
||||
import com.intellij.pom.java.LanguageLevel;
|
||||
@@ -15,6 +16,7 @@ import com.intellij.psi.tree.IElementType;
|
||||
import com.intellij.psi.util.PsiPrecedenceUtil;
|
||||
import com.intellij.psi.util.PsiTreeUtil;
|
||||
import com.intellij.psi.util.PsiUtil;
|
||||
import com.intellij.psi.util.TypeConversionUtil;
|
||||
import com.intellij.util.IncorrectOperationException;
|
||||
import com.intellij.util.SmartList;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
@@ -204,6 +206,15 @@ public class DataFlowInspection extends DataFlowInspectionBase {
|
||||
return fixes;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected @NotNull List<LocalQuickFix> createUnboxingNullableFixes(@NotNull PsiExpression qualifier, PsiExpression expression, boolean onTheFly) {
|
||||
List<LocalQuickFix> result = new SmartList<>();
|
||||
if (TypeConversionUtil.isBooleanType(qualifier.getType())) {
|
||||
result.add(new ReplaceWithBooleanEqualsFix(qualifier));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected LocalQuickFix createNavigateToNullParameterUsagesFix(PsiParameter parameter) {
|
||||
return new NullableStuffInspection.NavigateToNullLiteralArguments(parameter);
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
// "Replace with 'Boolean.FALSE.equals(flag)'" "true"
|
||||
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
class Test {
|
||||
public boolean b;
|
||||
public boolean c;
|
||||
|
||||
boolean test(@Nullable Boolean flag) {
|
||||
return c ? b && Boolean.FALSE.equals(flag) : c;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
// "Replace with 'Boolean.TRUE.equals(flag)'" "true"
|
||||
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
class Test {
|
||||
boolean test(@Nullable Boolean flag) {
|
||||
if (Boolean.TRUE.equals(flag)) {
|
||||
System.out.println("ok");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
// "Replace with 'Boolean.FALSE.equals(flag)'" "true"
|
||||
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
class Test {
|
||||
public boolean b;
|
||||
public boolean c;
|
||||
|
||||
boolean test(@Nullable Boolean flag) {
|
||||
return c ? b && !<caret>flag : c;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
// "Replace with 'Boolean.TRUE.equals(flag)'" "true"
|
||||
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
class Test {
|
||||
boolean test(@Nullable Boolean flag) {
|
||||
if (<caret>flag) {
|
||||
System.out.println("ok");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
// "Replace with 'Boolean.TRUE.equals(flag)'" "false"
|
||||
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
class Test {
|
||||
int test(@Nullable Integer i) {
|
||||
return <caret>i;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
// "Replace with 'Boolean.TRUE.equals(flag)'" "false"
|
||||
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
class Test {
|
||||
boolean test(@NotNull Boolean flag) {
|
||||
if (<caret>flag) {
|
||||
System.out.println("ok");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
// Copyright 2000-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
|
||||
package com.intellij.java.codeInsight.daemon.quickFix;
|
||||
|
||||
import com.intellij.codeInsight.daemon.quickFix.LightQuickFixParameterizedTestCase;
|
||||
import com.intellij.codeInspection.LocalInspectionTool;
|
||||
import com.intellij.codeInspection.dataFlow.DataFlowInspection;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class ReplaceWithBooleanEqualsFixTest extends LightQuickFixParameterizedTestCase {
|
||||
|
||||
@Override
|
||||
protected LocalInspectionTool @NotNull [] configureLocalInspectionTools() {
|
||||
DataFlowInspection inspection = new DataFlowInspection();
|
||||
inspection.SUGGEST_NULLABLE_ANNOTATIONS = true;
|
||||
return new LocalInspectionTool[]{inspection};
|
||||
}
|
||||
|
||||
@Override
|
||||
protected String getBasePath() {
|
||||
return "/codeInsight/daemonCodeAnalyzer/quickFix/replaceWithBoolean";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user