From c23738a77a9624bab13e6b6f05fdde630cdc6f2a Mon Sep 17 00:00:00 2001 From: Jack Wickham Date: Wed, 21 Oct 2020 13:26:22 +0100 Subject: [PATCH 1/2] Fix null pointer exception when checking immutables builders that come from an abstract method --- ...ImmutablesBuilderMissingInitialization.java | 2 +- ...tablesBuilderMissingInitializationTest.java | 18 ++++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) 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( From 4c88b2b8bf7e7301cbea37f18d70100bfd91dddd Mon Sep 17 00:00:00 2001 From: Jack Wickham Date: Wed, 21 Oct 2020 12:26:22 +0000 Subject: [PATCH 2/2] Add generated changelog entries --- changelog/@unreleased/pr-1519.v2.yml | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 changelog/@unreleased/pr-1519.v2.yml 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