diff --git a/baseline-error-prone/src/main/java/com/palantir/baseline/errorprone/ImmutablesBuilderMissingInitialization.java b/baseline-error-prone/src/main/java/com/palantir/baseline/errorprone/ImmutablesBuilderMissingInitialization.java index 60ff006c6..c5281e209 100644 --- a/baseline-error-prone/src/main/java/com/palantir/baseline/errorprone/ImmutablesBuilderMissingInitialization.java +++ b/baseline-error-prone/src/main/java/com/palantir/baseline/errorprone/ImmutablesBuilderMissingInitialization.java @@ -238,7 +238,7 @@ private Set removeFieldsPotentiallyInitializedBy(Set uninitializ private boolean methodJustConstructsBuilder( MethodSymbol methodSymbol, VisitorState state, ClassSymbol immutableClass, ClassSymbol interfaceClass) { MethodTree methodTree = ASTHelpers.findMethod(methodSymbol, state); - if (methodTree != null) { + if (methodTree != null && methodTree.getBody() != null) { // Check that the method just contains one statement, which is of the form `return new Something();` or // `return ImmutableType.builder();` if (methodTree.getBody().getStatements().size() != 1) { diff --git a/baseline-error-prone/src/test/java/com/palantir/baseline/errorprone/ImmutablesBuilderMissingInitializationTest.java b/baseline-error-prone/src/test/java/com/palantir/baseline/errorprone/ImmutablesBuilderMissingInitializationTest.java index b31fb7d21..7030dabdf 100644 --- a/baseline-error-prone/src/test/java/com/palantir/baseline/errorprone/ImmutablesBuilderMissingInitializationTest.java +++ b/baseline-error-prone/src/test/java/com/palantir/baseline/errorprone/ImmutablesBuilderMissingInitializationTest.java @@ -202,6 +202,24 @@ public void testPassesWhenAllFieldsPopulated_withImmutableOnAbstractClass() { .doTest(); } + @Test + public void testPassesWhenBuilderFromInterfaceMethod() { + helper().addSourceLines( + "Context.java", + importInterface("Person"), + "public interface Context {", + " Person.Builder personBuilder();", + "}") + .addSourceLines( + "MyTest.java", + "public class MyTest {", + " public void makePerson(Context context) {", + " context.personBuilder().build();", + " }", + "}") + .doTest(); + } + @Test public void testFailsWhenOneMandatoryFieldOmitted() { helper().addSourceLines( diff --git a/changelog/@unreleased/pr-1519.v2.yml b/changelog/@unreleased/pr-1519.v2.yml new file mode 100644 index 000000000..d66555336 --- /dev/null +++ b/changelog/@unreleased/pr-1519.v2.yml @@ -0,0 +1,6 @@ +type: fix +fix: + description: Fix null pointer exception when checking immutables builders that are + returned from abstract methods + links: + - https://github.com/palantir/gradle-baseline/pull/1519