1919 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2020 * THE SOFTWARE.
2121 */
22+ package com .uber .nullaway .guava ;
2223
2324import com .google .errorprone .CompilationTestHelper ;
2425import com .uber .nullaway .NullAway ;
2526import java .util .Arrays ;
27+ import org .junit .Assume ;
2628import org .junit .Before ;
2729import org .junit .Rule ;
2830import org .junit .Test ;
@@ -33,6 +35,8 @@ public class NullAwayGuavaParametricNullnessTests {
3335
3436 private CompilationTestHelper defaultCompilationHelper ;
3537
38+ private CompilationTestHelper jspecifyCompilationHelper ;
39+
3640 @ Before
3741 public void setup () {
3842 defaultCompilationHelper =
@@ -43,6 +47,14 @@ public void setup() {
4347 temporaryFolder .getRoot ().getAbsolutePath (),
4448 "-XepOpt:NullAway:AnnotatedPackages=com.uber,com.google.common" ,
4549 "-XepOpt:NullAway:UnannotatedSubPackages=com.uber.nullaway.[a-zA-Z0-9.]+.unannotated" ));
50+ jspecifyCompilationHelper =
51+ CompilationTestHelper .newInstance (NullAway .class , getClass ())
52+ .setArgs (
53+ Arrays .asList (
54+ "-d" ,
55+ temporaryFolder .getRoot ().getAbsolutePath (),
56+ "-XepOpt:NullAway:OnlyNullMarked=true" ,
57+ "-XepOpt:NullAway:JSpecifyMode=true" ));
4658 }
4759
4860 @ Test
@@ -70,6 +82,33 @@ public void testFutureCallbackParametricNullness() {
7082 .doTest ();
7183 }
7284
85+ @ Test
86+ public void jspecifyFutureCallback () {
87+ // to ensure javac reads proper generic types from the Guava jar
88+ Assume .assumeTrue (Runtime .version ().feature () >= 23 );
89+ jspecifyCompilationHelper
90+ .addSourceLines (
91+ "Test.java" ,
92+ "import com.google.common.util.concurrent.FutureCallback;" ,
93+ "import org.jspecify.annotations.*;" ,
94+ "@NullMarked" ,
95+ "class Test {" ,
96+ " public static <T> FutureCallback<@Nullable T> wrapFutureCallback(FutureCallback<@Nullable T> futureCallback) {" ,
97+ " return new FutureCallback<@Nullable T>() {" ,
98+ " @Override" ,
99+ " public void onSuccess(@Nullable T result) {" ,
100+ " futureCallback.onSuccess(result);" ,
101+ " }" ,
102+ " @Override" ,
103+ " public void onFailure(Throwable throwable) {" ,
104+ " futureCallback.onFailure(throwable);" ,
105+ " }" ,
106+ " };" ,
107+ " }" ,
108+ "}" )
109+ .doTest ();
110+ }
111+
73112 @ Test
74113 public void testIterableParametricNullness () {
75114 defaultCompilationHelper
@@ -91,6 +130,59 @@ public void testIterableParametricNullness() {
91130 .doTest ();
92131 }
93132
133+ @ Test
134+ public void jspecifyIterables () {
135+ // to ensure javac reads proper generic types from the Guava jar
136+ Assume .assumeTrue (Runtime .version ().feature () >= 23 );
137+ jspecifyCompilationHelper
138+ .addSourceLines (
139+ "Test.java" ,
140+ "import com.google.common.collect.ImmutableList;" ,
141+ "import com.google.common.collect.Iterables;" ,
142+ "import org.jspecify.annotations.*;" ,
143+ "@NullMarked" ,
144+ "class Test {" ,
145+ " public static String test1() {" ,
146+ " // BUG: Diagnostic contains: returning @Nullable expression" ,
147+ " return Iterables.<@Nullable String>getFirst(ImmutableList.<String>of(), null);" ,
148+ " }" ,
149+ " public static @Nullable String test2() {" ,
150+ " return Iterables.<@Nullable String>getFirst(ImmutableList.<String>of(), null);" ,
151+ " }" ,
152+ " public static String test3() {" ,
153+ " return Iterables.getOnlyElement(ImmutableList.of(\" hi\" ));" ,
154+ " }" ,
155+ " public static String test4() {" ,
156+ " // BUG: Diagnostic contains: returning @Nullable expression" ,
157+ " return Iterables.<@Nullable String>getOnlyElement(ImmutableList.of(\" hi\" ));" ,
158+ " }" ,
159+ "}" )
160+ .doTest ();
161+ }
162+
163+ @ Test
164+ public void jspecifyComparators () {
165+ // to ensure javac reads proper generic types from the Guava jar
166+ Assume .assumeTrue (Runtime .version ().feature () >= 23 );
167+ jspecifyCompilationHelper
168+ .addSourceLines (
169+ "Test.java" ,
170+ "import com.google.common.collect.Comparators;" ,
171+ "import java.util.Comparator;" ,
172+ "import org.jspecify.annotations.*;" ,
173+ "@NullMarked" ,
174+ "class Test {" ,
175+ " public static String test1(String t1, String t2, Comparator<? super String> cmp) {" ,
176+ " return Comparators.min(t1, t2, cmp);" ,
177+ " }" ,
178+ " public static String test2(@Nullable String t1, @Nullable String t2, Comparator<? super @Nullable String> cmp) {" ,
179+ " // BUG: Diagnostic contains: returning @Nullable expression" ,
180+ " return Comparators.<@Nullable String>min(t1, t2, cmp);" ,
181+ " }" ,
182+ "}" )
183+ .doTest ();
184+ }
185+
94186 @ Test
95187 public void testCloserParametricNullness () {
96188 defaultCompilationHelper
0 commit comments