From 552987fbcf0cda2f019e8509e2967e6c85df8f6c Mon Sep 17 00:00:00 2001 From: Tagir Valeev Date: Mon, 26 Sep 2022 15:22:44 +0200 Subject: [PATCH] [java-dfa] IDEA-302493 Update nullability for overridden methods with 'get' prefix when qualifier type is constrained and subclass method has different nullability GitOrigin-RevId: 6b9e819d236ebec069923f0647b9e42f30d89698 --- .../java/inst/MethodCallInstruction.java | 27 +++++------------ .../fixture/RewiringSubclassMethod.java | 29 +++++++++++++++++++ .../DataFlowInspectionTest.java | 1 + 3 files changed, 38 insertions(+), 19 deletions(-) create mode 100644 java/java-tests/testData/inspection/dataFlow/fixture/RewiringSubclassMethod.java diff --git a/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/java/inst/MethodCallInstruction.java b/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/java/inst/MethodCallInstruction.java index bb08feedfeee..8a3b1e0aa98d 100644 --- a/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/java/inst/MethodCallInstruction.java +++ b/java/java-analysis-impl/src/com/intellij/codeInspection/dataFlow/java/inst/MethodCallInstruction.java @@ -260,14 +260,6 @@ public class MethodCallInstruction extends ExpressionPushingInstruction { return myContext; } - public @Nullable DfaValue getPrecalculatedReturnValue() { - return myPrecalculatedReturnValue; - } - - public @NotNull Nullability getReturnNullability() { - return myReturnNullability; - } - public String toString() { if (myContext instanceof PsiCall) { return "CALL_METHOD: " + myContext.getText(); @@ -363,23 +355,20 @@ public class MethodCallInstruction extends ExpressionPushingInstruction { @NotNull DfaMemoryState state, @NotNull DfaValueFactory factory, PsiMethod realMethod) { - if (callArguments.getArguments() != null) { - PsiMethod method = myTargetMethod; - if (method != null) { - CustomMethodHandlers.CustomMethodHandler handler = CustomMethodHandlers.find(method); - if (handler != null) { - DfaValue value = handler.getMethodResultValue(callArguments, state, factory, method); - if (value != null) { - return value; - } + if (callArguments.getArguments() != null && myTargetMethod != null) { + CustomMethodHandlers.CustomMethodHandler handler = CustomMethodHandlers.find(myTargetMethod); + if (handler != null) { + DfaValue value = handler.getMethodResultValue(callArguments, state, factory, myTargetMethod); + if (value != null) { + return value; } } } DfaValue qualifierValue = callArguments.getQualifier(); - DfaValue precalculated = getPrecalculatedReturnValue(); + DfaValue precalculated = myPrecalculatedReturnValue; PsiType type = getResultType(); - VariableDescriptor descriptor = JavaDfaValueFactory.getAccessedVariableOrGetter(myTargetMethod); + VariableDescriptor descriptor = JavaDfaValueFactory.getAccessedVariableOrGetter(realMethod); if (descriptor instanceof SpecialField || descriptor != null && qualifierValue instanceof DfaVariableValue) { return descriptor.createValue(factory, qualifierValue); } diff --git a/java/java-tests/testData/inspection/dataFlow/fixture/RewiringSubclassMethod.java b/java/java-tests/testData/inspection/dataFlow/fixture/RewiringSubclassMethod.java new file mode 100644 index 000000000000..bb90bf2a319f --- /dev/null +++ b/java/java-tests/testData/inspection/dataFlow/fixture/RewiringSubclassMethod.java @@ -0,0 +1,29 @@ +// IDEA-302493 +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +public class RewiringSubclassMethod { + void test(Parent parent) { + if (parent instanceof C1 || parent instanceof C2) { + String s = parent.readString(); + System.out.println(s.trim()); + String s2 = parent.getString(); + System.out.println(s2.trim()); + } + } + + interface Parent { + @Nullable String readString(); + @Nullable String getString(); + } + + interface C1 extends Parent { + @NotNull String readString(); + @NotNull String getString(); + } + + interface C2 extends Parent { + @NotNull String readString(); + @NotNull String getString(); + } +} \ No newline at end of file diff --git a/java/java-tests/testSrc/com/intellij/java/codeInspection/DataFlowInspectionTest.java b/java/java-tests/testSrc/com/intellij/java/codeInspection/DataFlowInspectionTest.java index f876ba39645a..66974144191f 100644 --- a/java/java-tests/testSrc/com/intellij/java/codeInspection/DataFlowInspectionTest.java +++ b/java/java-tests/testSrc/com/intellij/java/codeInspection/DataFlowInspectionTest.java @@ -723,4 +723,5 @@ public class DataFlowInspectionTest extends DataFlowInspectionTestCase { public void testNestedVersusSuper() { doTest(); } public void testChangeFieldUsedInPureMethod() { doTest(); } public void testSuppression() { doTest(); } + public void testRewiringSubclassMethod() { doTest(); } }