Coverage Summary for Class: NullnessCasts (com.google.common.util.concurrent)
| Class | Class, % | Method, % | Line, % |
|---|---|---|---|
| NullnessCasts | 100% (1/1) | 33.3% (1/3) | 33.3% (1/3) |
1 /* 2 * Copyright (C) 2021 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14 15 package com.google.common.util.concurrent; 16 17 import com.google.common.annotations.GwtCompatible; 18 import javax.annotation.CheckForNull; 19 import org.checkerframework.checker.nullness.qual.Nullable; 20 21 /** A utility method to perform unchecked casts to suppress errors produced by nullness analyses. */ 22 @GwtCompatible 23 @ElementTypesAreNonnullByDefault 24 final class NullnessCasts { 25 /** 26 * Accepts a {@code @Nullable T} and returns a plain {@code T}, without performing any check that 27 * that conversion is safe. 28 * 29 * <p>This method is intended to help with usages of type parameters that have {@linkplain 30 * ParametricNullness parametric nullness}. If a type parameter instead ranges over only non-null 31 * types (or if the type is a non-variable type, like {@code String}), then code should almost 32 * never use this method, preferring instead to call {@code requireNonNull} so as to benefit from 33 * its runtime check. 34 * 35 * <p>An example use case for this method is in implementing an {@code Iterator<T>} whose {@code 36 * next} field is lazily initialized. The type of that field would be {@code @Nullable T}, and the 37 * code would be responsible for populating a "real" {@code T} (which might still be the value 38 * {@code null}!) before returning it to callers. Depending on how the code is structured, a 39 * nullness analysis might not understand that the field has been populated. To avoid that problem 40 * without having to add {@code @SuppressWarnings}, the code can call this method. 41 * 42 * <p>Why <i>not</i> just add {@code SuppressWarnings}? The problem is that this method is 43 * typically useful for {@code return} statements. That leaves the code with two options: Either 44 * add the suppression to the whole method (which turns off checking for a large section of code), 45 * or extract a variable, and put the suppression on that. However, a local variable typically 46 * doesn't work: Because nullness analyses typically infer the nullness of local variables, 47 * there's no way to assign a {@code @Nullable T} to a field {@code T foo;} and instruct the 48 * analysis that that means "plain {@code T}" rather than the inferred type {@code @Nullable T}. 49 * (Even if supported added {@code @NonNull}, that would not help, since the problem case 50 * addressed by this method is the case in which {@code T} has parametric nullness -- and thus its 51 * value may be legitimately {@code null}.) 52 */ 53 @SuppressWarnings("nullness") 54 @ParametricNullness 55 static <T extends @Nullable Object> T uncheckedCastNullableTToT(@CheckForNull T t) { 56 return t; 57 } 58 59 /** 60 * Returns {@code null} cast to any type. 61 * 62 * <p>This method is intended to help with usages of type parameters that have {@linkplain 63 * ParametricNullness parametric nullness}. Sometimes, code may receive a null {@code T} but store 64 * a "null sentinel" to take its place. When the time comes to convert it back to a {@code T} to 65 * return to a caller, the code needs to a way to return {@code null} from a method that returns 66 * "plain {@code T}." This API provides that. 67 */ 68 @SuppressWarnings("nullness") 69 @ParametricNullness 70 static <T extends @Nullable Object> T uncheckedNull() { 71 return null; 72 } 73 74 private NullnessCasts() {} 75 }