|
| 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 javax.annotation.CheckForNull; |
| 18 | + |
| 19 | +/** A utility method to perform unchecked casts to suppress errors produced by nullness analyses. */ |
| 20 | +final class NullnessCasts { |
| 21 | + /** |
| 22 | + * Accepts a {@code @Nullable T} and returns a plain {@code T}, without performing any check that |
| 23 | + * that conversion is safe. |
| 24 | + * |
| 25 | + * <p>This method is intended to help with usages of type parameters that have {@linkplain |
| 26 | + * ParametricNullness parametric nullness}. If a type parameter instead ranges over only non-null |
| 27 | + * types (or if the type is a non-variable type, like {@code String}), then code should almost |
| 28 | + * never use this method, preferring instead to call {@code requireNonNull} so as to benefit from |
| 29 | + * its runtime check. |
| 30 | + * |
| 31 | + * <p>An example use case for this method is in implementing an {@code Iterator<T>} whose {@code |
| 32 | + * next} field is lazily initialized. The type of that field would be {@code @Nullable T}, and the |
| 33 | + * code would be responsible for populating a "real" {@code T} (which might still be the value |
| 34 | + * {@code null}!) before returning it to callers. Depending on how the code is structured, a |
| 35 | + * nullness analysis might not understand that the field has been populated. To avoid that problem |
| 36 | + * without having to add {@code @SuppressWarnings}, the code can call this method. |
| 37 | + * |
| 38 | + * <p>Why <i>not</i> just add {@code SuppressWarnings}? The problem is that this method is |
| 39 | + * typically useful for {@code return} statements. That leaves the code with two options: Either |
| 40 | + * add the suppression to the whole method (which turns off checking for a large section of code), |
| 41 | + * or extract a variable, and put the suppression on that. However, a local variable typically |
| 42 | + * doesn't work: Because nullness analyses typically infer the nullness of local variables, |
| 43 | + * there's no way to assign a {@code @Nullable T} to a field {@code T foo;} and instruct the |
| 44 | + * analysis that that means "plain {@code T}" rather than the inferred type {@code @Nullable T}. |
| 45 | + * (Even if supported added {@code @NonNull}, that would not help, since the problem case |
| 46 | + * addressed by this method is the case in which {@code T} has parametric nullness -- and thus its |
| 47 | + * value may be legitimately {@code null}.) |
| 48 | + */ |
| 49 | + @SuppressWarnings("nullness") |
| 50 | + static <T extends Object> T uncheckedCastNullableTToT(@CheckForNull T t) { |
| 51 | + return t; |
| 52 | + } |
| 53 | + |
| 54 | + private NullnessCasts() {} |
| 55 | +} |
0 commit comments