Skip to content

Commit a6273b0

Browse files
authored
Support AssertJ hasSize() (#1229)
This PR changes to support AssertJ `hasSize()`. See gh-1224
1 parent e39856e commit a6273b0

3 files changed

Lines changed: 31 additions & 0 deletions

File tree

nullaway/src/main/java/com/uber/nullaway/handlers/AssertionHandler.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ public NullnessHint onDataflowVisitMethodInvocation(
6161
// assertThat(A).isInstanceOf(Foo.class)
6262
// A will not be NULL after this statement.
6363
if (methodNameUtil.isMethodIsNotNull(callee, state)
64+
|| methodNameUtil.isMethodHasSize(callee, state)
6465
|| methodNameUtil.isMethodIsInstanceOf(callee, state)) {
6566
AccessPath ap = getAccessPathForNotNullAssertThatExpr(node, state, apContext);
6667
if (ap != null) {

nullaway/src/main/java/com/uber/nullaway/handlers/MethodNameUtil.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class MethodNameUtil {
4444
// Strings corresponding to the names of the methods (and their owners) used to identify
4545
// assertions in this handler.
4646
private static final String IS_NOT_NULL_METHOD = "isNotNull";
47+
private static final String IS_HAS_SIZE_METHOD = "hasSize";
4748
private static final String IS_OWNER_TRUTH_SUBJECT = "com.google.common.truth.Subject";
4849
private static final String IS_INSTANCE_OF_METHOD = "isInstanceOf";
4950
private static final String IS_INSTANCE_OF_ANY_METHOD = "isInstanceOfAny";
@@ -88,6 +89,7 @@ class MethodNameUtil {
8889
// here refers to com.sun.tools.javac.util.Name. Comparing methods using Names is faster than
8990
// comparing using strings.
9091
private Name isNotNull;
92+
private Name hasSize;
9193

9294
private Name isInstanceOf;
9395
private Name isInstanceOfAny;
@@ -133,6 +135,7 @@ class MethodNameUtil {
133135
@Initializer
134136
void initializeMethodNames(Name.Table table) {
135137
isNotNull = table.fromString(IS_NOT_NULL_METHOD);
138+
hasSize = table.fromString(IS_HAS_SIZE_METHOD);
136139
isOwnerTruthSubject = table.fromString(IS_OWNER_TRUTH_SUBJECT);
137140

138141
isInstanceOf = table.fromString(IS_INSTANCE_OF_METHOD);
@@ -180,6 +183,10 @@ boolean isMethodIsNotNull(Symbol.MethodSymbol methodSymbol, VisitorState state)
180183
|| matchesAssertJAssertMethod(methodSymbol, isNotNull, state);
181184
}
182185

186+
boolean isMethodHasSize(Symbol.MethodSymbol methodSymbol, VisitorState state) {
187+
return matchesAssertJAssertMethod(methodSymbol, hasSize, state);
188+
}
189+
183190
boolean isMethodIsInstanceOf(Symbol.MethodSymbol methodSymbol, VisitorState state) {
184191
return matchesMethod(methodSymbol, isInstanceOf, isOwnerTruthSubject)
185192
|| matchesAssertJAssertMethod(methodSymbol, isInstanceOf, state)

nullaway/src/test/java/com/uber/nullaway/AssertionLibsTests.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -549,4 +549,27 @@ public void collectionAssertIsNotNull() {
549549
"}")
550550
.doTest();
551551
}
552+
553+
@Test
554+
public void collectionAssertHasSize() {
555+
makeTestHelperWithArgs(
556+
Arrays.asList(
557+
"-d",
558+
temporaryFolder.getRoot().getAbsolutePath(),
559+
"-XepOpt:NullAway:AnnotatedPackages=com.uber",
560+
"-XepOpt:NullAway:HandleTestAssertionLibraries=true"))
561+
.addSourceLines(
562+
"Test.java",
563+
"import org.jspecify.annotations.*;",
564+
"import java.util.Collection;",
565+
"import static org.assertj.core.api.Assertions.assertThat;",
566+
"@NullMarked",
567+
"class Test {",
568+
" void test(@Nullable Collection<String> c) {",
569+
" assertThat(c).hasSize(1);",
570+
" c.size();",
571+
" }",
572+
"}")
573+
.doTest();
574+
}
552575
}

0 commit comments

Comments
 (0)