Skip to content

Commit bdca1c3

Browse files
authored
Test reading JSpecify annotations from bytecodes on JDK 21 (#1245)
As of JDK 21.0.8, the patch to properly expose type use annotations via standard APIs has landed, [guarded behind the flag `-XDaddTypeAnnotationsToSymbol=true`](openjdk/jdk21u@bb83d59). This PR runs the relevant tests on JDK 21 by passing that flag where needed. This means NullAway regression tests will fail on versions of JDK 21 older than 21.0.8, but we can document that. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - New Features - Improved compatibility with newer JDKs (21+), enabling enhanced type-annotation handling where available. - Tests - Expanded test coverage by removing version-based exclusions and adding runtime guards for newer JDKs. - Updated test setup to conditionally enable features on supported JDK versions. - Chores - Simplified build configuration by eliminating Java-version-specific test filtering. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
1 parent f59d952 commit bdca1c3

File tree

4 files changed

+24
-25
lines changed

4 files changed

+24
-25
lines changed

guava-recent-unit-tests/src/test/java/com/uber/nullaway/guava/NullAwayGuavaParametricNullnessTests.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.google.errorprone.CompilationTestHelper;
2525
import com.uber.nullaway.NullAway;
2626
import java.util.Arrays;
27+
import java.util.List;
2728
import org.junit.Assume;
2829
import org.junit.Before;
2930
import org.junit.Rule;
@@ -57,11 +58,12 @@ public void setup() {
5758
jspecifyCompilationHelper =
5859
CompilationTestHelper.newInstance(NullAway.class, getClass())
5960
.setArgs(
60-
Arrays.asList(
61+
List.of(
6162
"-d",
6263
temporaryFolder.getRoot().getAbsolutePath(),
6364
"-XepOpt:NullAway:OnlyNullMarked=true",
64-
"-XepOpt:NullAway:JSpecifyMode=true"));
65+
"-XepOpt:NullAway:JSpecifyMode=true",
66+
"-XDaddTypeAnnotationsToSymbol=true"));
6567
}
6668

6769
@Test
@@ -92,7 +94,7 @@ public void testFutureCallbackParametricNullness() {
9294
@Test
9395
public void jspecifyFutureCallback() {
9496
// to ensure javac reads proper generic types from the Guava jar
95-
Assume.assumeTrue(Runtime.version().feature() >= 23);
97+
Assume.assumeTrue(Runtime.version().feature() >= 21);
9698
jspecifyCompilationHelper
9799
.addSourceLines(
98100
"Test.java",
@@ -140,7 +142,7 @@ public void testIterableParametricNullness() {
140142
@Test
141143
public void jspecifyIterables() {
142144
// to ensure javac reads proper generic types from the Guava jar
143-
Assume.assumeTrue(Runtime.version().feature() >= 23);
145+
Assume.assumeTrue(Runtime.version().feature() >= 21);
144146
jspecifyCompilationHelper
145147
.addSourceLines(
146148
"Test.java",
@@ -170,7 +172,7 @@ public void jspecifyIterables() {
170172
@Test
171173
public void jspecifyComparators() {
172174
// to ensure javac reads proper generic types from the Guava jar
173-
Assume.assumeTrue(Runtime.version().feature() >= 23);
175+
Assume.assumeTrue(Runtime.version().feature() >= 21);
174176
jspecifyCompilationHelper
175177
.addSourceLines(
176178
"Test.java",
@@ -252,7 +254,7 @@ public void testFunctionMethodOverride() {
252254
@Test
253255
public void newHashSetPassingNullable() {
254256
// to ensure javac reads proper generic types from the Guava jar
255-
Assume.assumeTrue(Runtime.version().feature() >= 23);
257+
Assume.assumeTrue(Runtime.version().feature() >= 21);
256258
jspecifyCompilationHelper
257259
.addSourceLines(
258260
"Test.java",

nullaway/build.gradle

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -187,15 +187,6 @@ project.tasks.named('check').configure {
187187
}
188188

189189
tasks.withType(Test).configureEach { test ->
190-
if (test.javaVersion < JavaVersion.VERSION_22) {
191-
// Certain tests involving reading annotations from bytecode will not pass on pre-JDK-22 javac
192-
// until the fix for https://bugs.openjdk.org/browse/JDK-8225377 is backported or until we add
193-
// workarounds. See https://github.com/uber/NullAway/issues/1005.
194-
test.filter {
195-
excludeTestsMatching "com.uber.nullaway.jspecify.BytecodeGenericsTests.genericsChecksForParamPassingAndReturns"
196-
excludeTestsMatching "com.uber.nullaway.jspecify.BytecodeGenericsTests.genericsChecksForFieldAssignments"
197-
}
198-
}
199190
// hack: for some reasons the ErrorProneCLIFlagsConfigTest does not pass on EP 2.31.0,
200191
// though it passes on both older and newer Error Prone versions (???). Not worth tracking
201192
// down

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.uber.nullaway;
22

33
import com.google.errorprone.CompilationTestHelper;
4-
import java.util.Arrays;
54
import java.util.List;
65
import org.junit.Before;
76
import org.junit.Rule;
@@ -17,7 +16,7 @@ public abstract class NullAwayTestsBase {
1716
public void setup() {
1817
defaultCompilationHelper =
1918
makeTestHelperWithArgs(
20-
Arrays.asList(
19+
List.of(
2120
"-d",
2221
temporaryFolder.getRoot().getAbsolutePath(),
2322
"-XepOpt:NullAway:KnownInitializers="
@@ -33,7 +32,8 @@ public void setup() {
3332
"-XepOpt:NullAway:ExcludedClassAnnotations=com.uber.nullaway.testdata.TestAnnot",
3433
"-XepOpt:NullAway:CastToNonNullMethod=com.uber.nullaway.testdata.Util.castToNonNull",
3534
"-XepOpt:NullAway:ExternalInitAnnotations=com.uber.ExternalInit",
36-
"-XepOpt:NullAway:ExcludedFieldAnnotations=com.uber.ExternalFieldInit"));
35+
"-XepOpt:NullAway:ExcludedFieldAnnotations=com.uber.ExternalFieldInit",
36+
"-XDaddTypeAnnotationsToSymbol=true"));
3737
}
3838

3939
/**

nullaway/src/test/java/com/uber/nullaway/jspecify/BytecodeGenericsTests.java

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import com.google.errorprone.CompilationTestHelper;
44
import com.uber.nullaway.NullAwayTestsBase;
5-
import java.util.Arrays;
6-
import org.junit.Ignore;
5+
import java.util.List;
6+
import org.junit.Assume;
77
import org.junit.Test;
88

99
public class BytecodeGenericsTests extends NullAwayTestsBase {
@@ -76,6 +76,8 @@ public void genericsChecksForAssignments() {
7676

7777
@Test
7878
public void genericsChecksForFieldAssignments() {
79+
// to read annotations properly from bytecode
80+
Assume.assumeTrue(Runtime.version().feature() >= 21);
7981
makeHelper()
8082
.addSourceLines(
8183
"Test.java",
@@ -99,6 +101,9 @@ public void genericsChecksForFieldAssignments() {
99101

100102
@Test
101103
public void genericsChecksForParamPassingAndReturns() {
104+
// to read annotations properly from bytecode
105+
Assume.assumeTrue(Runtime.version().feature() >= 21);
106+
102107
makeHelper()
103108
.addSourceLines(
104109
"Test.java",
@@ -224,10 +229,9 @@ public void overrideReturnTypes() {
224229
}
225230

226231
@Test
227-
@Ignore("Failing due to https://bugs.openjdk.org/browse/JDK-8337795")
228-
// TODO Re-enable this test once the JDK bug is fixed, on appropriate JDK versions
229-
// See https://github.com/uber/NullAway/issues/1011
230232
public void callMethodTakingJavaUtilFunction() {
233+
// to read annotations properly from bytecode
234+
Assume.assumeTrue(Runtime.version().feature() >= 21);
231235
makeHelper()
232236
.addSourceLines(
233237
"Test.java",
@@ -244,7 +248,9 @@ public void callMethodTakingJavaUtilFunction() {
244248

245249
private CompilationTestHelper makeHelper() {
246250
return makeTestHelperWithArgs(
247-
Arrays.asList(
248-
"-XepOpt:NullAway:AnnotatedPackages=com.uber", "-XepOpt:NullAway:JSpecifyMode=true"));
251+
List.of(
252+
"-XepOpt:NullAway:AnnotatedPackages=com.uber",
253+
"-XepOpt:NullAway:JSpecifyMode=true",
254+
"-XDaddTypeAnnotationsToSymbol=true"));
249255
}
250256
}

0 commit comments

Comments
 (0)