|
| 1 | +/* |
| 2 | + * Copyright 2018 The Error Prone Authors. |
| 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 | + * http://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 | +package com.google.errorprone.annotations; |
| 17 | + |
| 18 | +import static java.lang.annotation.ElementType.TYPE_PARAMETER; |
| 19 | +import static java.lang.annotation.RetentionPolicy.RUNTIME; |
| 20 | + |
| 21 | +import java.lang.annotation.Documented; |
| 22 | +import java.lang.annotation.Retention; |
| 23 | +import java.lang.annotation.Target; |
| 24 | + |
| 25 | +/** |
| 26 | + * When a {@link ThreadSafe} class has type parameters, annotating a parameter with {@code |
| 27 | + * ThreadSafeTypeParameter} enforces that declarations of this class must, for that type parameter, |
| 28 | + * use a type that is itself thread-safe. |
| 29 | + * |
| 30 | + * <p>Additionally, only type parameters that are annotated with {@code ThreadSafeTypeParameter} can |
| 31 | + * be used as field types that are not {@link |
| 32 | + * com.google.errorprone.annotations.concurrent.GuardedBy @GuardedBy}. |
| 33 | + * |
| 34 | + * <p>In more detail, consider this (valid) class: |
| 35 | + * |
| 36 | + * <pre>{@code |
| 37 | + * @ThreadSafe class MyThreadSafeClass<A, B, @ThreadSafeTypeParameter C> { |
| 38 | + * |
| 39 | + * @GuardedBy("this") B b; |
| 40 | + * |
| 41 | + * final C c; |
| 42 | + * |
| 43 | + * MyThreadSafeClass(B b, C c) { |
| 44 | + * this.b = b; |
| 45 | + * this.c = c; |
| 46 | + * } |
| 47 | + * } |
| 48 | + * }</pre> |
| 49 | + * |
| 50 | + * Each of these three type parameters is valid for a different reason: type parameter {@code A} is |
| 51 | + * ok because it is simply not used as the type of a field; type parameter {@code B} is ok because |
| 52 | + * it is used as the type of a field that is declared to be {@code @GuardedBy}; finally, type |
| 53 | + * parameter {@code C} is ok because it is annotated with {@code ThreadSafeTypeParameter}. |
| 54 | + * Furthermore, the declaration {@code MyThreadSafeClass<Object, Object, String>} is valid, since |
| 55 | + * the type parameter {@code C} (i.e., {@code String}) is thread-safe, whereas a declaration {@code |
| 56 | + * MyThreadSafeClass<Object, Object, Object>} would result in a compiler error. |
| 57 | + * |
| 58 | + * <p>Note: the {@code ThreadSafeTypeParameter} annotation has a secondary use case. If you annotate |
| 59 | + * a type parameter of a method, then callers to that method are only allowed to pass in a type that |
| 60 | + * is deemed thread-safe. For example, given the method declaration {@code static |
| 61 | + * <@ThreadSafeTypeParameter T> void foo(T foo) {}}, a call to {@code foo} must pass a parameter |
| 62 | + * that is deemed thread-safe. |
| 63 | + */ |
| 64 | +@Documented |
| 65 | +@Target(TYPE_PARAMETER) |
| 66 | +@Retention(RUNTIME) |
| 67 | +public @interface ThreadSafeTypeParameter {} |
0 commit comments