• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 The Guava Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.common.base;
18 
19 import static java.lang.annotation.ElementType.FIELD;
20 import static java.lang.annotation.ElementType.METHOD;
21 import static java.lang.annotation.ElementType.PARAMETER;
22 import static java.lang.annotation.RetentionPolicy.RUNTIME;
23 
24 import com.google.common.annotations.GwtCompatible;
25 import java.lang.annotation.Retention;
26 import java.lang.annotation.Target;
27 
28 /**
29  * Annotates a "top-level" type-variable usage that takes its nullness from the type argument
30  * supplied by the user of the class. For example, {@code Multiset.Entry.getElement()} returns
31  * {@code @ParametricNullness E}, which means:
32  *
33  * <ul>
34  *   <li>{@code getElement} on a {@code Multiset.Entry<@NonNull String>} returns {@code @NonNull
35  *       String}.
36  *   <li>{@code getElement} on a {@code Multiset.Entry<@Nullable String>} returns {@code @Nullable
37  *       String}.
38  * </ul>
39  *
40  * This is the same behavior as type-variable usages have to Kotlin and to the Checker Framework.
41  * Contrast the method above to:
42  *
43  * <ul>
44  *   <li>methods whose return type is a type variable but which can never return {@code null},
45  *       typically because the type forbids nullable type arguments: For example, {@code
46  *       ImmutableList.get} returns {@code E}, but that value is never {@code null}. (Accordingly,
47  *       {@code ImmutableList} is declared to forbid {@code ImmutableList<@Nullable String>}.)
48  *   <li>methods whose return type is a type variable but which can return {@code null} regardless
49  *       of the type argument supplied by the user of the class: For example, {@code
50  *       ImmutableMap.get} returns {@code @Nullable E} because the method can return {@code null}
51  *       even on an {@code ImmutableMap<K, @NonNull String>}.
52  * </ul>
53  *
54  * <p>Consumers of this annotation include:
55  *
56  * <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 "undo" {@link ElementTypesAreNonnullByDefault}. It is the best we can do for Kotlin
60  *       under our current constraints.
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>
73  * </ul>
74  *
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
77  * href="https://github.com/google/guava/issues/6126#issuecomment-1203145963">tools no longer need
78  * it</a>.
79  */
80 @GwtCompatible
81 @Retention(RUNTIME)
82 @Target({FIELD, METHOD, PARAMETER})
83 @javax.annotation.meta.TypeQualifierNickname
84 @javax.annotation.Nonnull(when = javax.annotation.meta.When.UNKNOWN)
85 @interface ParametricNullness {}
86