/* * Copyright 2022 The JSpecify Authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.jspecify.annotations; import static java.lang.annotation.ElementType.TYPE_USE; import static java.lang.annotation.RetentionPolicy.RUNTIME; import java.lang.annotation.Documented; import java.lang.annotation.Retention; import java.lang.annotation.Target; /** * Indicates that the annotated type * usage (commonly a parameter type or return type) is considered to exclude {@code null} * as a value; rarely needed within {@linkplain NullMarked null-marked} code. * *
This annotation serves two primary purposes: * *
For a comprehensive introduction to JSpecify, please see jspecify.org. * *
Warning: These annotations are under development, and any aspect of their * naming, locations, or design is subject to change until the JSpecify 1.0 release. Moreover, * supporting analysis tools will be tracking the changes on varying schedules. Releasing a library * using these annotations in its API is strongly discouraged at this time. * *
In the following example, {@code MyOptional}'s type parameter {@code T} accepts only non-null * type arguments, but {@code MyList}'s type parameter {@code E} will accept either a non-null or * nullable type argument. * *
{@code
* // All the below is null-marked code
*
* class MyOptional { … }
*
* interface MyList {
* // Returns the first non-null element, if such element exists.
* MyOptional firstNonNull() { … } // problem here!
* }
*
* MyList<@Nullable String> maybeNulls = …
* MyList nonNulls = …
* }
*
* Because {@code MyOptional} accepts only non-null type arguments, we need both {@code
* maybeNulls.firstNonNull()} and {@code nonNulls.firstNonNull()} to produce the same return type:
* {@code MyOptional! The solution is to project the type argument to its non-null counterpart:
*
* Here, {@code @NonNull E} selects the non-null form of the type argument, whether it was
* already non-null or not, which is just what we need in this scenario.
*
* If {@code E} has a non-null upper bound, then the apparent projection {@code @NonNull E} is
* redundant but harmless.
*
* Nullable projection serves the equivalent purpose in
* the opposite direction, and is far more commonly useful.
*
* If a type variable has all its usages being projected in one direction or the other, it
* should be given a non-null upper bound, and any non-null projections can then be removed.
*
* {@code @NonNull} is inapplicable in all the same
* locations as {@link Nullable}.
*/
@Documented
@Target(TYPE_USE)
@Retention(RUNTIME)
public @interface NonNull {}
{@code
* // Returns the first non-null element, if such element exists.
* MyOptional<@NonNull E> firstNonNull() { … } // problem fixed!
* }
*
* Where it is not applicable
*
*