19
19
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20
20
* THE SOFTWARE.
21
21
*/
22
+ package com .uber .nullaway .guava ;
22
23
23
24
import com .google .errorprone .CompilationTestHelper ;
24
25
import com .uber .nullaway .NullAway ;
25
26
import java .util .Arrays ;
27
+ import org .junit .Assume ;
26
28
import org .junit .Before ;
27
29
import org .junit .Rule ;
28
30
import org .junit .Test ;
@@ -33,6 +35,8 @@ public class NullAwayGuavaParametricNullnessTests {
33
35
34
36
private CompilationTestHelper defaultCompilationHelper ;
35
37
38
+ private CompilationTestHelper jspecifyCompilationHelper ;
39
+
36
40
@ Before
37
41
public void setup () {
38
42
defaultCompilationHelper =
@@ -43,6 +47,14 @@ public void setup() {
43
47
temporaryFolder .getRoot ().getAbsolutePath (),
44
48
"-XepOpt:NullAway:AnnotatedPackages=com.uber,com.google.common" ,
45
49
"-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" ));
46
58
}
47
59
48
60
@ Test
@@ -70,6 +82,33 @@ public void testFutureCallbackParametricNullness() {
70
82
.doTest ();
71
83
}
72
84
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
+
73
112
@ Test
74
113
public void testIterableParametricNullness () {
75
114
defaultCompilationHelper
@@ -91,6 +130,59 @@ public void testIterableParametricNullness() {
91
130
.doTest ();
92
131
}
93
132
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
+
94
186
@ Test
95
187
public void testCloserParametricNullness () {
96
188
defaultCompilationHelper
0 commit comments