| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2014 Stephan Herrmann and others. |
| 3 | * |
| 4 | * This program and the accompanying materials |
| 5 | * are made available under the terms of the Eclipse Public License 2.0 |
| 6 | * which accompanies this distribution, and is available at |
| 7 | * https://www.eclipse.org/legal/epl-2.0/ |
| 8 | * |
| 9 | * SPDX-License-Identifier: EPL-2.0 |
| 10 | * |
| 11 | * Contributors: |
| 12 | * Stephan Herrmann - initial API and implementation |
| 13 | *******************************************************************************/ |
| 14 | package org.eclipse.jdt.annotation; |
| 15 | |
| 16 | /** |
| 17 | * Locations that can be affected by a {@link NonNullByDefault} annotation. |
| 18 | * Each constant of this enum describes a specific kind of type use. |
| 19 | * Wildcards and the use of type variables are always excluded from {@link NonNullByDefault}. |
| 20 | * @since 2.0 |
| 21 | */ |
| 22 | public enum DefaultLocation { |
| 23 | |
| 24 | /** |
| 25 | * Defines that a given {@link NonNullByDefault} annotation should affect all unannotated |
| 26 | * parameters of any method or constructor within the scope of the annotated declaration. |
| 27 | * |
| 28 | * <h2>Example</h2> |
| 29 | * <pre> @NonNullByDefault(PARAMETER) |
| 30 | * interface X { |
| 31 | * void print(Number n); |
| 32 | * }</pre> |
| 33 | * <p> |
| 34 | * Here <code>Number</code> will be interpreted as <code>@NonNull Number</code>. |
| 35 | * </p> |
| 36 | */ |
| 37 | PARAMETER, |
| 38 | |
| 39 | /** |
| 40 | * Defines that a given {@link NonNullByDefault} annotation should affect all unannotated |
| 41 | * method return types within the scope of the annotated declaration. |
| 42 | * |
| 43 | * <h2>Example</h2> |
| 44 | * <pre> @NonNullByDefault(RETURN_TYPE) |
| 45 | * interface X { |
| 46 | * Number getNumber(); |
| 47 | * }</pre> |
| 48 | * <p> |
| 49 | * Here <code>Number</code> will be interpreted as <code>@NonNull Number</code>. |
| 50 | * </p> |
| 51 | */ |
| 52 | RETURN_TYPE, |
| 53 | |
| 54 | /** |
| 55 | * Defines that a given {@link NonNullByDefault} annotation should affect all unannotated |
| 56 | * field types within the scope of the annotated declaration. |
| 57 | * |
| 58 | * <h2>Example</h2> |
| 59 | * <pre> @NonNullByDefault(FIELD) |
| 60 | * class X { |
| 61 | * Number number = Integer.MAX_VALUE; |
| 62 | * }</pre> |
| 63 | * <p> |
| 64 | * Here <code>Number</code> will be interpreted as <code>@NonNull Number</code>. |
| 65 | * </p> |
| 66 | */ |
| 67 | FIELD, |
| 68 | |
| 69 | /** |
| 70 | * Defines that a given {@link NonNullByDefault} annotation should affect all unannotated |
| 71 | * type parameter declarations within the scope of the annotated declaration. |
| 72 | * |
| 73 | * <h2>Example</h2> |
| 74 | * <pre> @NonNullByDefault(TYPE_PARAMETER) |
| 75 | * class X { |
| 76 | * <T> T identity(T t) { return t; } |
| 77 | * }</pre> |
| 78 | * <p> |
| 79 | * Here <code><T></code> will be interpreted as <code><@NonNull T></code>. |
| 80 | * </p> |
| 81 | */ |
| 82 | TYPE_PARAMETER, |
| 83 | |
| 84 | /** |
| 85 | * Defines that a given {@link NonNullByDefault} annotation should affect all unannotated |
| 86 | * explicit type bounds within the scope of the annotated declaration. A type bound of |
| 87 | * type {@link java.lang.Object} is <strong>never</strong> considered as an explicit bound, |
| 88 | * i.e., <code>T extends Object</code> is never affected by {@link NonNullByDefault}. |
| 89 | * |
| 90 | * <h2>Example</h2> |
| 91 | * <pre> @NonNullByDefault(TYPE_BOUND) |
| 92 | * interface X { |
| 93 | * <T extends Number> void process(T t, List<? super Number> l); |
| 94 | * }</pre> |
| 95 | * <p> |
| 96 | * Here both occurrences of <code>Number</code> will be interpreted as <code>@NonNull Number</code>. |
| 97 | * </p> |
| 98 | */ |
| 99 | TYPE_BOUND, |
| 100 | |
| 101 | /** |
| 102 | * Defines that a given {@link NonNullByDefault} annotation should affect all unannotated |
| 103 | * type arguments within the scope of the annotated declaration (except wildcards and |
| 104 | * type variables). |
| 105 | * |
| 106 | * <h2>Example</h2> |
| 107 | * <pre> @NonNullByDefault(TYPE_ARGUMENT) |
| 108 | * interface X<T> { |
| 109 | * void process(List<T> tl, List<Number> nl); |
| 110 | * }</pre> |
| 111 | * <p> |
| 112 | * Here <code>Number</code> will be interpreted as <code>@NonNull Number</code>, |
| 113 | * but the use of type variable <code>T</code> is not affected. |
| 114 | * </p> |
| 115 | */ |
| 116 | TYPE_ARGUMENT, |
| 117 | |
| 118 | /** |
| 119 | * Defines that a given {@link NonNullByDefault} annotation should affect all unannotated |
| 120 | * array components within the scope of the annotated declaration. |
| 121 | * |
| 122 | * <h2>Example</h2> |
| 123 | * <pre> @NonNullByDefault(ARRAY_CONTENTS) |
| 124 | * interface X { |
| 125 | * Number[] n1; |
| 126 | * Number[][] n2; |
| 127 | * }</pre> |
| 128 | * <p> |
| 129 | * These declarations are interpreted as: |
| 130 | * </p> |
| 131 | * <pre> @NonNull Number [] n1; |
| 132 | * @NonNull Number [] @NonNull[] n2;</pre> |
| 133 | * <p> |
| 134 | * I.e., both fields can still be <code>null</code> (see the unannotated left-most pair |
| 135 | * of brackets) but none of the <em>contents</em> of these arrays is allowed to be |
| 136 | * <code>null</code> (at any dimension). |
| 137 | * </p> |
| 138 | */ |
| 139 | ARRAY_CONTENTS |
| 140 | } |