in nullaway/src/main/java/com/uber/nullaway/handlers/OptionalEmptinessHandler.java [165:216]
private void handleTestAssertions(
VisitorState state,
AccessPath.AccessPathContext apContext,
AccessPathNullnessPropagation.Updates bothUpdates,
MethodInvocationNode node,
Symbol.MethodSymbol symbol) {
Consumer<Node> nonNullMarker =
nonNullNode ->
updateNonNullAPsForOptionalContent(state.context, bothUpdates, nonNullNode, apContext);
boolean isAssertTrueMethod = methodNameUtil.isMethodAssertTrue(symbol);
boolean isAssertFalseMethod = methodNameUtil.isMethodAssertFalse(symbol);
boolean isTrueMethod = methodNameUtil.isMethodIsTrue(symbol);
boolean isFalseMethod = methodNameUtil.isMethodIsFalse(symbol);
if (isAssertTrueMethod || isAssertFalseMethod) {
// assertTrue(optionalFoo.isPresent())
// assertFalse("optional was empty", optionalFoo.isEmpty())
// note: in junit4 the optional string message comes first, but in junit5 it comes last
Optional<MethodInvocationNode> assertedOnMethod =
node.getArguments().stream()
.filter(n -> TypeKind.BOOLEAN.equals(n.getType().getKind()))
.filter(n -> n instanceof MethodInvocationNode)
.map(n -> (MethodInvocationNode) n)
.findFirst();
if (assertedOnMethod.isPresent()) {
handleBooleanAssertionOnMethod(
nonNullMarker,
state.getTypes(),
assertedOnMethod.get(),
isAssertTrueMethod,
isAssertFalseMethod);
}
} else if (isTrueMethod || isFalseMethod) {
// asertThat(optionalFoo.isPresent()).isTrue()
// asertThat(optionalFoo.isEmpty()).isFalse()
Optional<MethodInvocationNode> wrappedMethod =
getNodeWrappedByAssertThat(node)
.filter(n -> n instanceof MethodInvocationNode)
.map(n -> (MethodInvocationNode) n)
.map(this::maybeUnwrapBooleanValueOf);
if (wrappedMethod.isPresent()) {
handleBooleanAssertionOnMethod(
nonNullMarker, state.getTypes(), wrappedMethod.get(), isTrueMethod, isFalseMethod);
}
} else if (methodNameUtil.isMethodThatEnsuresOptionalPresent(symbol)) {
// assertThat(optionalRef).isPresent()
// assertThat(methodReturningOptional()).isNotEmpty()
// assertThat(mapWithOptionalValues.get("key")).isNotEmpty()
getNodeWrappedByAssertThat(node).ifPresent(nonNullMarker);
}
}