[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
This commit is contained in:
Tagir Valeev
2022-09-26 15:10:10 +00:00
committed by intellij-monorepo-bot
parent f5622aee87
commit 552987fbcf
3 changed files with 38 additions and 19 deletions
@@ -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);
}
@@ -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();
}
}
@@ -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(); }
}