Skip to content

Commit ae36f57

Browse files
cpovirkGoogle Java Core Libraries
authored and
Google Java Core Libraries
committed
Turn @ParametricNullness into a no-op for Kotlin and IntelliJ, and reduce its retention to CLASS.
We'll continue annotating our APIs with it for now, since it can be helpful for NullAway and perhaps other tools. (But please [let us know](https://github.com/google/guava/issues/new?template=Blank+issue) if you are depending on it from a tool other than NullAway.) This is the next step toward [using JSpecify in Guava](jspecify/jspecify#239 (comment)). Also, I've updated the Javadoc to belatedly note that `ParametricNulless` isn't used by `NullPointerTester` anymore (as of cl/530341970). RELNOTES=Changed `@ParametricNullness` into a no-op for Kotlin and IntelliJ. Before now, it was forcing many usages of type variables to have [platform types](https://kotlinlang.org/docs/java-interop.html#null-safety-and-platform-types), which meant that Kotlin couldn't check those usages for nullness errors. With this change, Kotlin can detect more errors. PiperOrigin-RevId: 708610470
1 parent 17634b5 commit ae36f57

30 files changed

+270
-660
lines changed

android/guava/src/com/google/common/base/ParametricNullness.java

+9-22
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import static java.lang.annotation.ElementType.FIELD;
2020
import static java.lang.annotation.ElementType.METHOD;
2121
import static java.lang.annotation.ElementType.PARAMETER;
22-
import static java.lang.annotation.RetentionPolicy.RUNTIME;
22+
import static java.lang.annotation.RetentionPolicy.CLASS;
2323

2424
import com.google.common.annotations.GwtCompatible;
2525
import java.lang.annotation.Retention;
@@ -54,32 +54,19 @@
5454
* <p>Consumers of this annotation include:
5555
*
5656
* <ul>
57-
* <li>Kotlin, for which it makes the type-variable usage (a) a Kotlin platform type when the type
58-
* argument is non-nullable and (b) nullable when the type argument is nullable. We use this
59-
* to reduce the immediate effects of {@code NullMarked} as we transition to JSpecify
60-
* annotations.
61-
* <li>NullAway, which will <a
62-
* href="https://github.com/google/guava/issues/6126#issuecomment-1204399671">treat it
63-
* identically to {@code Nullable} as of version 0.9.9</a>. To treat it that way before then,
64-
* you can set {@code
65-
* -XepOpt:NullAway:CustomNullableAnnotations=com.google.common.base.ParametricNullness,...,com.google.common.util.concurrent.ParametricNullness},
66-
* where the {@code ...} contains the names of all the other {@code ParametricNullness}
67-
* annotations in Guava. Or you might prefer to omit Guava from your {@code AnnotatedPackages}
68-
* list.
69-
* <li><a href="https://developers.google.com/j2objc">J2ObjC</a>
70-
* <li>{@code NullPointerTester}, at least in the Android backport (where the type-use annotations
71-
* {@code NullPointerTester} would need are not available) and in case of <a
72-
* href="https://bugs.openjdk.java.net/browse/JDK-8202469">JDK-8202469</a>
57+
* <li>NullAway, which <a
58+
* href="https://github.com/google/guava/issues/6126#issuecomment-1204399671">treats it
59+
* identically to {@code Nullable}</a> as of version 0.9.9.
60+
* <li><a href="https://developers.google.com/j2objc">J2ObjC</a>, maybe: It might no longer be
61+
* necessary there, since we have stopped using the {@code @ParametersAreNonnullByDefault}
62+
* annotations that {@code ParametricNullness} was counteracting.
7363
* </ul>
7464
*
75-
* <p>This annotation is a temporary hack. We will remove it after we're able to adopt the <a
76-
* href="https://jspecify.dev/">JSpecify</a> nullness annotations and <a
65+
* <p>This annotation is a temporary hack. We will remove it after <a
7766
* href="https://github.com/google/guava/issues/6126#issuecomment-1203145963">tools no longer need
7867
* it</a>.
7968
*/
8069
@GwtCompatible
81-
@Retention(RUNTIME)
70+
@Retention(CLASS)
8271
@Target({FIELD, METHOD, PARAMETER})
83-
@javax.annotation.meta.TypeQualifierNickname
84-
@javax.annotation.Nonnull(when = javax.annotation.meta.When.UNKNOWN)
8572
@interface ParametricNullness {}

android/guava/src/com/google/common/cache/ParametricNullness.java

+9-22
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import static java.lang.annotation.ElementType.FIELD;
2020
import static java.lang.annotation.ElementType.METHOD;
2121
import static java.lang.annotation.ElementType.PARAMETER;
22-
import static java.lang.annotation.RetentionPolicy.RUNTIME;
22+
import static java.lang.annotation.RetentionPolicy.CLASS;
2323

2424
import com.google.common.annotations.GwtCompatible;
2525
import java.lang.annotation.Retention;
@@ -54,32 +54,19 @@
5454
* <p>Consumers of this annotation include:
5555
*
5656
* <ul>
57-
* <li>Kotlin, for which it makes the type-variable usage (a) a Kotlin platform type when the type
58-
* argument is non-nullable and (b) nullable when the type argument is nullable. We use this
59-
* to reduce the immediate effects of {@code NullMarked} as we transition to JSpecify
60-
* annotations.
61-
* <li>NullAway, which will <a
62-
* href="https://github.com/google/guava/issues/6126#issuecomment-1204399671">treat it
63-
* identically to {@code Nullable} as of version 0.9.9</a>. To treat it that way before then,
64-
* you can set {@code
65-
* -XepOpt:NullAway:CustomNullableAnnotations=com.google.common.base.ParametricNullness,...,com.google.common.util.concurrent.ParametricNullness},
66-
* where the {@code ...} contains the names of all the other {@code ParametricNullness}
67-
* annotations in Guava. Or you might prefer to omit Guava from your {@code AnnotatedPackages}
68-
* list.
69-
* <li><a href="https://developers.google.com/j2objc">J2ObjC</a>
70-
* <li>{@code NullPointerTester}, at least in the Android backport (where the type-use annotations
71-
* {@code NullPointerTester} would need are not available) and in case of <a
72-
* href="https://bugs.openjdk.java.net/browse/JDK-8202469">JDK-8202469</a>
57+
* <li>NullAway, which <a
58+
* href="https://github.com/google/guava/issues/6126#issuecomment-1204399671">treats it
59+
* identically to {@code Nullable}</a> as of version 0.9.9.
60+
* <li><a href="https://developers.google.com/j2objc">J2ObjC</a>, maybe: It might no longer be
61+
* necessary there, since we have stopped using the {@code @ParametersAreNonnullByDefault}
62+
* annotations that {@code ParametricNullness} was counteracting.
7363
* </ul>
7464
*
75-
* <p>This annotation is a temporary hack. We will remove it after we're able to adopt the <a
76-
* href="https://jspecify.dev/">JSpecify</a> nullness annotations and <a
65+
* <p>This annotation is a temporary hack. We will remove it after <a
7766
* href="https://github.com/google/guava/issues/6126#issuecomment-1203145963">tools no longer need
7867
* it</a>.
7968
*/
8069
@GwtCompatible
81-
@Retention(RUNTIME)
70+
@Retention(CLASS)
8271
@Target({FIELD, METHOD, PARAMETER})
83-
@javax.annotation.meta.TypeQualifierNickname
84-
@javax.annotation.Nonnull(when = javax.annotation.meta.When.UNKNOWN)
8572
@interface ParametricNullness {}

android/guava/src/com/google/common/collect/ParametricNullness.java

+9-22
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import static java.lang.annotation.ElementType.FIELD;
2020
import static java.lang.annotation.ElementType.METHOD;
2121
import static java.lang.annotation.ElementType.PARAMETER;
22-
import static java.lang.annotation.RetentionPolicy.RUNTIME;
22+
import static java.lang.annotation.RetentionPolicy.CLASS;
2323

2424
import com.google.common.annotations.GwtCompatible;
2525
import java.lang.annotation.Retention;
@@ -54,32 +54,19 @@
5454
* <p>Consumers of this annotation include:
5555
*
5656
* <ul>
57-
* <li>Kotlin, for which it makes the type-variable usage (a) a Kotlin platform type when the type
58-
* argument is non-nullable and (b) nullable when the type argument is nullable. We use this
59-
* to reduce the immediate effects of {@code NullMarked} as we transition to JSpecify
60-
* annotations.
61-
* <li>NullAway, which will <a
62-
* href="https://github.com/google/guava/issues/6126#issuecomment-1204399671">treat it
63-
* identically to {@code Nullable} as of version 0.9.9</a>. To treat it that way before then,
64-
* you can set {@code
65-
* -XepOpt:NullAway:CustomNullableAnnotations=com.google.common.base.ParametricNullness,...,com.google.common.util.concurrent.ParametricNullness},
66-
* where the {@code ...} contains the names of all the other {@code ParametricNullness}
67-
* annotations in Guava. Or you might prefer to omit Guava from your {@code AnnotatedPackages}
68-
* list.
69-
* <li><a href="https://developers.google.com/j2objc">J2ObjC</a>
70-
* <li>{@code NullPointerTester}, at least in the Android backport (where the type-use annotations
71-
* {@code NullPointerTester} would need are not available) and in case of <a
72-
* href="https://bugs.openjdk.java.net/browse/JDK-8202469">JDK-8202469</a>
57+
* <li>NullAway, which <a
58+
* href="https://github.com/google/guava/issues/6126#issuecomment-1204399671">treats it
59+
* identically to {@code Nullable}</a> as of version 0.9.9.
60+
* <li><a href="https://developers.google.com/j2objc">J2ObjC</a>, maybe: It might no longer be
61+
* necessary there, since we have stopped using the {@code @ParametersAreNonnullByDefault}
62+
* annotations that {@code ParametricNullness} was counteracting.
7363
* </ul>
7464
*
75-
* <p>This annotation is a temporary hack. We will remove it after we're able to adopt the <a
76-
* href="https://jspecify.dev/">JSpecify</a> nullness annotations and <a
65+
* <p>This annotation is a temporary hack. We will remove it after <a
7766
* href="https://github.com/google/guava/issues/6126#issuecomment-1203145963">tools no longer need
7867
* it</a>.
7968
*/
8069
@GwtCompatible
81-
@Retention(RUNTIME)
70+
@Retention(CLASS)
8271
@Target({FIELD, METHOD, PARAMETER})
83-
@javax.annotation.meta.TypeQualifierNickname
84-
@javax.annotation.Nonnull(when = javax.annotation.meta.When.UNKNOWN)
8572
@interface ParametricNullness {}

android/guava/src/com/google/common/escape/ParametricNullness.java

+9-22
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import static java.lang.annotation.ElementType.FIELD;
2020
import static java.lang.annotation.ElementType.METHOD;
2121
import static java.lang.annotation.ElementType.PARAMETER;
22-
import static java.lang.annotation.RetentionPolicy.RUNTIME;
22+
import static java.lang.annotation.RetentionPolicy.CLASS;
2323

2424
import com.google.common.annotations.GwtCompatible;
2525
import java.lang.annotation.Retention;
@@ -54,32 +54,19 @@
5454
* <p>Consumers of this annotation include:
5555
*
5656
* <ul>
57-
* <li>Kotlin, for which it makes the type-variable usage (a) a Kotlin platform type when the type
58-
* argument is non-nullable and (b) nullable when the type argument is nullable. We use this
59-
* to reduce the immediate effects of {@code NullMarked} as we transition to JSpecify
60-
* annotations.
61-
* <li>NullAway, which will <a
62-
* href="https://github.com/google/guava/issues/6126#issuecomment-1204399671">treat it
63-
* identically to {@code Nullable} as of version 0.9.9</a>. To treat it that way before then,
64-
* you can set {@code
65-
* -XepOpt:NullAway:CustomNullableAnnotations=com.google.common.base.ParametricNullness,...,com.google.common.util.concurrent.ParametricNullness},
66-
* where the {@code ...} contains the names of all the other {@code ParametricNullness}
67-
* annotations in Guava. Or you might prefer to omit Guava from your {@code AnnotatedPackages}
68-
* list.
69-
* <li><a href="https://developers.google.com/j2objc">J2ObjC</a>
70-
* <li>{@code NullPointerTester}, at least in the Android backport (where the type-use annotations
71-
* {@code NullPointerTester} would need are not available) and in case of <a
72-
* href="https://bugs.openjdk.java.net/browse/JDK-8202469">JDK-8202469</a>
57+
* <li>NullAway, which <a
58+
* href="https://github.com/google/guava/issues/6126#issuecomment-1204399671">treats it
59+
* identically to {@code Nullable}</a> as of version 0.9.9.
60+
* <li><a href="https://developers.google.com/j2objc">J2ObjC</a>, maybe: It might no longer be
61+
* necessary there, since we have stopped using the {@code @ParametersAreNonnullByDefault}
62+
* annotations that {@code ParametricNullness} was counteracting.
7363
* </ul>
7464
*
75-
* <p>This annotation is a temporary hack. We will remove it after we're able to adopt the <a
76-
* href="https://jspecify.dev/">JSpecify</a> nullness annotations and <a
65+
* <p>This annotation is a temporary hack. We will remove it after <a
7766
* href="https://github.com/google/guava/issues/6126#issuecomment-1203145963">tools no longer need
7867
* it</a>.
7968
*/
8069
@GwtCompatible
81-
@Retention(RUNTIME)
70+
@Retention(CLASS)
8271
@Target({FIELD, METHOD, PARAMETER})
83-
@javax.annotation.meta.TypeQualifierNickname
84-
@javax.annotation.Nonnull(when = javax.annotation.meta.When.UNKNOWN)
8572
@interface ParametricNullness {}

android/guava/src/com/google/common/eventbus/ParametricNullness.java

+9-22
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import static java.lang.annotation.ElementType.FIELD;
2020
import static java.lang.annotation.ElementType.METHOD;
2121
import static java.lang.annotation.ElementType.PARAMETER;
22-
import static java.lang.annotation.RetentionPolicy.RUNTIME;
22+
import static java.lang.annotation.RetentionPolicy.CLASS;
2323

2424
import com.google.common.annotations.GwtCompatible;
2525
import java.lang.annotation.Retention;
@@ -54,32 +54,19 @@
5454
* <p>Consumers of this annotation include:
5555
*
5656
* <ul>
57-
* <li>Kotlin, for which it makes the type-variable usage (a) a Kotlin platform type when the type
58-
* argument is non-nullable and (b) nullable when the type argument is nullable. We use this
59-
* to reduce the immediate effects of {@code NullMarked} as we transition to JSpecify
60-
* annotations.
61-
* <li>NullAway, which will <a
62-
* href="https://github.com/google/guava/issues/6126#issuecomment-1204399671">treat it
63-
* identically to {@code Nullable} as of version 0.9.9</a>. To treat it that way before then,
64-
* you can set {@code
65-
* -XepOpt:NullAway:CustomNullableAnnotations=com.google.common.base.ParametricNullness,...,com.google.common.util.concurrent.ParametricNullness},
66-
* where the {@code ...} contains the names of all the other {@code ParametricNullness}
67-
* annotations in Guava. Or you might prefer to omit Guava from your {@code AnnotatedPackages}
68-
* list.
69-
* <li><a href="https://developers.google.com/j2objc">J2ObjC</a>
70-
* <li>{@code NullPointerTester}, at least in the Android backport (where the type-use annotations
71-
* {@code NullPointerTester} would need are not available) and in case of <a
72-
* href="https://bugs.openjdk.java.net/browse/JDK-8202469">JDK-8202469</a>
57+
* <li>NullAway, which <a
58+
* href="https://github.com/google/guava/issues/6126#issuecomment-1204399671">treats it
59+
* identically to {@code Nullable}</a> as of version 0.9.9.
60+
* <li><a href="https://developers.google.com/j2objc">J2ObjC</a>, maybe: It might no longer be
61+
* necessary there, since we have stopped using the {@code @ParametersAreNonnullByDefault}
62+
* annotations that {@code ParametricNullness} was counteracting.
7363
* </ul>
7464
*
75-
* <p>This annotation is a temporary hack. We will remove it after we're able to adopt the <a
76-
* href="https://jspecify.dev/">JSpecify</a> nullness annotations and <a
65+
* <p>This annotation is a temporary hack. We will remove it after <a
7766
* href="https://github.com/google/guava/issues/6126#issuecomment-1203145963">tools no longer need
7867
* it</a>.
7968
*/
8069
@GwtCompatible
81-
@Retention(RUNTIME)
70+
@Retention(CLASS)
8271
@Target({FIELD, METHOD, PARAMETER})
83-
@javax.annotation.meta.TypeQualifierNickname
84-
@javax.annotation.Nonnull(when = javax.annotation.meta.When.UNKNOWN)
8572
@interface ParametricNullness {}

android/guava/src/com/google/common/graph/ParametricNullness.java

+9-22
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import static java.lang.annotation.ElementType.FIELD;
2020
import static java.lang.annotation.ElementType.METHOD;
2121
import static java.lang.annotation.ElementType.PARAMETER;
22-
import static java.lang.annotation.RetentionPolicy.RUNTIME;
22+
import static java.lang.annotation.RetentionPolicy.CLASS;
2323

2424
import com.google.common.annotations.GwtCompatible;
2525
import java.lang.annotation.Retention;
@@ -54,32 +54,19 @@
5454
* <p>Consumers of this annotation include:
5555
*
5656
* <ul>
57-
* <li>Kotlin, for which it makes the type-variable usage (a) a Kotlin platform type when the type
58-
* argument is non-nullable and (b) nullable when the type argument is nullable. We use this
59-
* to reduce the immediate effects of {@code NullMarked} as we transition to JSpecify
60-
* annotations.
61-
* <li>NullAway, which will <a
62-
* href="https://github.com/google/guava/issues/6126#issuecomment-1204399671">treat it
63-
* identically to {@code Nullable} as of version 0.9.9</a>. To treat it that way before then,
64-
* you can set {@code
65-
* -XepOpt:NullAway:CustomNullableAnnotations=com.google.common.base.ParametricNullness,...,com.google.common.util.concurrent.ParametricNullness},
66-
* where the {@code ...} contains the names of all the other {@code ParametricNullness}
67-
* annotations in Guava. Or you might prefer to omit Guava from your {@code AnnotatedPackages}
68-
* list.
69-
* <li><a href="https://developers.google.com/j2objc">J2ObjC</a>
70-
* <li>{@code NullPointerTester}, at least in the Android backport (where the type-use annotations
71-
* {@code NullPointerTester} would need are not available) and in case of <a
72-
* href="https://bugs.openjdk.java.net/browse/JDK-8202469">JDK-8202469</a>
57+
* <li>NullAway, which <a
58+
* href="https://github.com/google/guava/issues/6126#issuecomment-1204399671">treats it
59+
* identically to {@code Nullable}</a> as of version 0.9.9.
60+
* <li><a href="https://developers.google.com/j2objc">J2ObjC</a>, maybe: It might no longer be
61+
* necessary there, since we have stopped using the {@code @ParametersAreNonnullByDefault}
62+
* annotations that {@code ParametricNullness} was counteracting.
7363
* </ul>
7464
*
75-
* <p>This annotation is a temporary hack. We will remove it after we're able to adopt the <a
76-
* href="https://jspecify.dev/">JSpecify</a> nullness annotations and <a
65+
* <p>This annotation is a temporary hack. We will remove it after <a
7766
* href="https://github.com/google/guava/issues/6126#issuecomment-1203145963">tools no longer need
7867
* it</a>.
7968
*/
8069
@GwtCompatible
81-
@Retention(RUNTIME)
70+
@Retention(CLASS)
8271
@Target({FIELD, METHOD, PARAMETER})
83-
@javax.annotation.meta.TypeQualifierNickname
84-
@javax.annotation.Nonnull(when = javax.annotation.meta.When.UNKNOWN)
8572
@interface ParametricNullness {}

android/guava/src/com/google/common/hash/ParametricNullness.java

+9-22
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import static java.lang.annotation.ElementType.FIELD;
2020
import static java.lang.annotation.ElementType.METHOD;
2121
import static java.lang.annotation.ElementType.PARAMETER;
22-
import static java.lang.annotation.RetentionPolicy.RUNTIME;
22+
import static java.lang.annotation.RetentionPolicy.CLASS;
2323

2424
import com.google.common.annotations.GwtCompatible;
2525
import java.lang.annotation.Retention;
@@ -54,32 +54,19 @@
5454
* <p>Consumers of this annotation include:
5555
*
5656
* <ul>
57-
* <li>Kotlin, for which it makes the type-variable usage (a) a Kotlin platform type when the type
58-
* argument is non-nullable and (b) nullable when the type argument is nullable. We use this
59-
* to reduce the immediate effects of {@code NullMarked} as we transition to JSpecify
60-
* annotations.
61-
* <li>NullAway, which will <a
62-
* href="https://github.com/google/guava/issues/6126#issuecomment-1204399671">treat it
63-
* identically to {@code Nullable} as of version 0.9.9</a>. To treat it that way before then,
64-
* you can set {@code
65-
* -XepOpt:NullAway:CustomNullableAnnotations=com.google.common.base.ParametricNullness,...,com.google.common.util.concurrent.ParametricNullness},
66-
* where the {@code ...} contains the names of all the other {@code ParametricNullness}
67-
* annotations in Guava. Or you might prefer to omit Guava from your {@code AnnotatedPackages}
68-
* list.
69-
* <li><a href="https://developers.google.com/j2objc">J2ObjC</a>
70-
* <li>{@code NullPointerTester}, at least in the Android backport (where the type-use annotations
71-
* {@code NullPointerTester} would need are not available) and in case of <a
72-
* href="https://bugs.openjdk.java.net/browse/JDK-8202469">JDK-8202469</a>
57+
* <li>NullAway, which <a
58+
* href="https://github.com/google/guava/issues/6126#issuecomment-1204399671">treats it
59+
* identically to {@code Nullable}</a> as of version 0.9.9.
60+
* <li><a href="https://developers.google.com/j2objc">J2ObjC</a>, maybe: It might no longer be
61+
* necessary there, since we have stopped using the {@code @ParametersAreNonnullByDefault}
62+
* annotations that {@code ParametricNullness} was counteracting.
7363
* </ul>
7464
*
75-
* <p>This annotation is a temporary hack. We will remove it after we're able to adopt the <a
76-
* href="https://jspecify.dev/">JSpecify</a> nullness annotations and <a
65+
* <p>This annotation is a temporary hack. We will remove it after <a
7766
* href="https://github.com/google/guava/issues/6126#issuecomment-1203145963">tools no longer need
7867
* it</a>.
7968
*/
8069
@GwtCompatible
81-
@Retention(RUNTIME)
70+
@Retention(CLASS)
8271
@Target({FIELD, METHOD, PARAMETER})
83-
@javax.annotation.meta.TypeQualifierNickname
84-
@javax.annotation.Nonnull(when = javax.annotation.meta.When.UNKNOWN)
8572
@interface ParametricNullness {}

0 commit comments

Comments
 (0)