Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix NPE when checking immutables builders from abstract methods #1519

Merged
merged 2 commits into from
Oct 21, 2020
Merged
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
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