Skip to content

Commit

Permalink
Fix NPE when checking immutables builders from abstract methods (#1519)
Browse files Browse the repository at this point in the history
Fix null pointer exception when checking immutables builders that are returned from abstract methods
  • Loading branch information
jackwickham authored Oct 21, 2020
1 parent 735e592 commit ee282be
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ private Set<String> removeFieldsPotentiallyInitializedBy(Set<String> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
6 changes: 6 additions & 0 deletions changelog/@unreleased/pr-1519.v2.yml
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit ee282be

Please sign in to comment.