[java-dfa] IDEA-349054 Ignore current object field nullity inside readResolve()

GitOrigin-RevId: dfe65dc4aa335626874a8b2a01aa68fe627e8ed9
This commit is contained in:
Tagir Valeev
2024-03-13 21:17:42 +00:00
committed by intellij-monorepo-bot
parent 0bed8cf5eb
commit e19f123a6c
3 changed files with 34 additions and 1 deletions
@@ -159,12 +159,18 @@ public final class PlainDescriptor extends PsiVarDescriptor {
if (!placeMethod.hasModifierProperty(PsiModifier.STATIC) && target.hasModifierProperty(PsiModifier.STATIC)) return false;
if (!target.hasModifierProperty(PsiModifier.STATIC) &&
!placeMethod.hasModifierProperty(PsiModifier.STATIC) &&
methodCanBeCalledFromConstructorBeforeFieldInitializing(target, placeMethod, placeClass)) {
(isReadResolveMethod(placeMethod, placeClass) ||
methodCanBeCalledFromConstructorBeforeFieldInitializing(target, placeMethod, placeClass))) {
return true;
}
return getAccessOffset(placeMethod) < getWriteOffset(target);
}
private static boolean isReadResolveMethod(@NotNull PsiMethod method, @NotNull PsiClass aClass) {
return "readResolve".equals(method.getName()) && method.getParameterList().isEmpty() &&
InheritanceUtil.isInheritor(aClass, CommonClassNames.JAVA_IO_SERIALIZABLE);
}
private record TargetCallInfo(@NotNull PsiMethod constructor,
@NotNull PsiMethodCallExpression call){}
private static boolean methodCanBeCalledFromConstructorBeforeFieldInitializing(@NotNull PsiField target,
@@ -0,0 +1,26 @@
import org.jetbrains.annotations.NotNull;
import java.io.*;
import java.util.Base64;
class Test implements Serializable {
@NotNull String string;
Test(@NotNull String string) {
this.string = string;
}
@Serial
protected Object readResolve() {
if (string == null) {
throw new IllegalStateException("Wrong object!");
}
return this;
}
public static void main(String[] args) throws IOException, ClassNotFoundException {
byte[] data = Base64.getDecoder().decode("rO0ABXNyAARUZXN0ST9d8XKvH/0CAAFMAAZzdHJpbmd0ABJMamF2YS9sYW5nL1N0cmluZzt4cHA=");
Test object = (Test) new ObjectInputStream(new ByteArrayInputStream(data)).readObject();
System.out.println(object);
}
}
@@ -113,6 +113,7 @@ public class DataFlowInspection21Test extends DataFlowInspectionTestCase {
public void testSuspiciousLabelElementsJava20() {
doTest();
}
public void testReadResolve() { doTest(); }
public void testForEachPattern() {
myFixture.addClass("""