Skip to content

Commit

Permalink
Prevent NestedOptionals from throwing an NPE (#412)
Browse files Browse the repository at this point in the history
Previously, a `NullPointerException` was thrown if during compilation the
`java.util.Optional` class was not loaded at all.
  • Loading branch information
gtoison authored Dec 16, 2022
1 parent 9611423 commit 870d16a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,11 @@ public NestedOptionals() {}

@Override
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {
return state.getTypes().isSubtype(ASTHelpers.getType(tree), OPTIONAL_OF_OPTIONAL.get(state))
? describeMatch(tree)
: Description.NO_MATCH;
Type type = OPTIONAL_OF_OPTIONAL.get(state);
if (type == null || !state.getTypes().isSubtype(ASTHelpers.getType(tree), type)) {
return Description.NO_MATCH;
}

return describeMatch(tree);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,19 @@ void identification() {
"}")
.doTest();
}

@Test
void identificationOptionalTypeNotLoaded() {
compilationTestHelper
.addSourceLines(
"A.java",
"import java.time.Duration;",
"",
"class A {",
" void m() {",
" Duration.ofSeconds(1);",
" }",
"}")
.doTest();
}
}

0 comments on commit 870d16a

Please sign in to comment.