diff --git a/jdk-recent-unit-tests/src/test/java/com/uber/nullaway/jdk17/FlexibleConstructorTests.java b/jdk-recent-unit-tests/src/test/java/com/uber/nullaway/jdk17/FlexibleConstructorTests.java new file mode 100644 index 0000000000..d07d87a001 --- /dev/null +++ b/jdk-recent-unit-tests/src/test/java/com/uber/nullaway/jdk17/FlexibleConstructorTests.java @@ -0,0 +1,79 @@ +package com.uber.nullaway.jdk17; + +import com.google.errorprone.CompilationTestHelper; +import com.uber.nullaway.NullAway; +import java.util.Arrays; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.TemporaryFolder; + +/** Tests for flexible constructor bodies (JEP 513). */ +public class FlexibleConstructorTests { + + @Rule public final TemporaryFolder temporaryFolder = new TemporaryFolder(); + + private CompilationTestHelper defaultCompilationHelper; + + @Before + public void setup() { + defaultCompilationHelper = + CompilationTestHelper.newInstance(NullAway.class, getClass()) + .setArgs( + Arrays.asList( + "-d", + temporaryFolder.getRoot().getAbsolutePath(), + "-XepOpt:NullAway:AnnotatedPackages=com.uber")); + } + + @Test + public void thisCallAfterPrologue() { + defaultCompilationHelper + .addSourceLines( + "Test.java", + """ + package com.uber; + class Test { + Object f; + Test(Object f) { + this.f = f; + } + // this(...) as the first statement + Test(String first, String last) { + this(first + last); + } + // this(...) after one statement + Test(int x) { + String s = String.valueOf(x); + this(s); + } + // this(...) after several statements, with statements after it + Test(long x, long y) { + if (x < 0) { + throw new IllegalArgumentException(); + } + long sum = x + y; + this(String.valueOf(sum)); + f.toString(); + } + // a prologue without this(...) still has to initialize the field + // BUG: Diagnostic contains: initializer method does not guarantee @NonNull field 'f' + Test(char c) { + String s = String.valueOf(c); + super(); + } + // a this(...) call of a local class does not count for the enclosing constructor + // BUG: Diagnostic contains: initializer method does not guarantee @NonNull field 'f' + Test(boolean b) { + class Local { + Local() { + this(1); + } + Local(int i) {} + } + } + } + """) + .doTest(); + } +} diff --git a/nullaway/src/main/java/com/uber/nullaway/NullAway.java b/nullaway/src/main/java/com/uber/nullaway/NullAway.java index 671295b26f..84e91f6818 100644 --- a/nullaway/src/main/java/com/uber/nullaway/NullAway.java +++ b/nullaway/src/main/java/com/uber/nullaway/NullAway.java @@ -2955,13 +2955,18 @@ private ImmutableSet guaranteedNonNullForConstructor( return guaranteedNonNullBuilder.build(); } - /** does the constructor invoke another constructor in the same class via this(...)? */ + /** + * Does the constructor invoke another constructor in the same class via {@code this(...)}? + * + *

With flexible constructor bodies (JEP 513), statements may precede the {@code this(...)} + * call, so every statement of the body is checked. The call is always a direct statement of the + * constructor body; javac rejects it inside a nested block, lambda, or local class. + */ private boolean constructorInvokesAnother(MethodTree constructor, VisitorState state) { - BlockTree body = constructor.getBody(); - List statements = body.getStatements(); - if (!statements.isEmpty()) { - StatementTree statementTree = statements.get(0); - return isThisCall(statementTree, state); + for (StatementTree statementTree : constructor.getBody().getStatements()) { + if (isThisCall(statementTree, state)) { + return true; + } } return false; }