| 1 | /* |
| 2 | * Copyright 2000-2021 JetBrains s.r.o. |
| 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 | * https://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 org.jetbrains.annotations; |
| 18 | |
| 19 | import java.lang.annotation.*; |
| 20 | |
| 21 | /** |
| 22 | * An element annotated with {@link Nullable} claims {@code null} value is perfectly <em>valid</em> |
| 23 | * to return (for methods), pass to (parameters) or hold in (local variables and fields). |
| 24 | * Apart from documentation purposes this annotation is intended to be used by static analysis tools |
| 25 | * to validate against probable runtime errors or element contract violations. |
| 26 | * <p> |
| 27 | * By convention, this annotation applied only when the value should <em>always</em> be checked against {@code null} |
| 28 | * because the developer could do nothing to prevent null from happening. |
| 29 | * Otherwise, too eager {@link Nullable} usage could lead to too many false positives from static analysis tools. |
| 30 | * <p> |
| 31 | * For example, {@link java.util.Map#get(Object key)} should <em>not</em> be annotated {@link Nullable} because |
| 32 | * someone may have put not-null value in the map by this key and is expecting to find this value there ever since. |
| 33 | * <p> |
| 34 | * On the other hand, the {@link java.lang.ref.Reference#get()} should be annotated {@link Nullable} because |
| 35 | * it returns {@code null} if object got collected which can happen at any time completely unexpectedly. |
| 36 | */ |
| 37 | @Documented |
| 38 | @Retention(RetentionPolicy.CLASS) |
| 39 | @Target({ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER, ElementType.LOCAL_VARIABLE, ElementType.TYPE_USE}) |
| 40 | public @interface Nullable { |
| 41 | /** |
| 42 | * @return textual reason when the annotated value could be null, for documentation purposes. |
| 43 | */ |
| 44 | @NonNls String value() default ""; |
| 45 | } |