Coverage Summary for Class: NullnessCasts (com.google.common.base)

Class Class, % Method, % Line, %
NullnessCasts 100% (1/1) 50% (1/2) 50% (1/2)


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.base; 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  @ParametricNullness 54  @SuppressWarnings("nullness") 55  static <T extends @Nullable Object> T uncheckedCastNullableTToT(@CheckForNull T t) { 56  return t; 57  } 58  59  private NullnessCasts() {} 60 }