Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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();
}
}
17 changes: 11 additions & 6 deletions nullaway/src/main/java/com/uber/nullaway/NullAway.java
Original file line number Diff line number Diff line change
Expand Up @@ -2955,13 +2955,18 @@ private ImmutableSet<Element> 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(...)}?
*
* <p>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<? extends StatementTree> 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;
}
Expand Down