-
Notifications
You must be signed in to change notification settings - Fork 747
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create
ThreadSafeTypeParameter
to replace ThreadSafe.TypeParameter
.
This is the first step in open sourcing the threadsafe equivalent of `ImmutableTypeParameter`. After this an LSC will be performed to replace all usages of `ThreadSafe.TypeParameter` with `ThreadSafeTypeParameter` after which and remaining references to the former (e.g. in messages) will be removed. ``` Startblock: has LGTM b/352709884 is fixed ``` PiperOrigin-RevId: 653005735
- Loading branch information
1 parent
6bd568d
commit 2656f48
Showing
2 changed files
with
68 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
..._annotations/src/main/java/com/google/errorprone/annotations/ThreadSafeTypeParameter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
* Copyright 2018 The Error Prone 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 com.google.errorprone.annotations; | ||
|
||
import static java.lang.annotation.ElementType.TYPE_PARAMETER; | ||
import static java.lang.annotation.RetentionPolicy.RUNTIME; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* When a {@link ThreadSafe} class has type parameters, annotating a parameter with {@code | ||
* ThreadSafeTypeParameter} enforces that declarations of this class must, for that type parameter, | ||
* use a type that is itself thread-safe. | ||
* | ||
* <p>Additionally, only type parameters that are annotated with {@code ThreadSafeTypeParameter} can | ||
* be used as field types that are not {@link | ||
* com.google.errorprone.annotations.concurrent.GuardedBy @GuardedBy}. | ||
* | ||
* <p>In more detail, consider this (valid) class: | ||
* | ||
* <pre>{@code | ||
* @ThreadSafe class MyThreadSafeClass<A, B, @ThreadSafeTypeParameter C> { | ||
* | ||
* @GuardedBy("this") B b; | ||
* | ||
* final C c; | ||
* | ||
* MyThreadSafeClass(B b, C c) { | ||
* this.b = b; | ||
* this.c = c; | ||
* } | ||
* } | ||
* }</pre> | ||
* | ||
* Each of these three type parameters is valid for a different reason: type parameter {@code A} is | ||
* ok because it is simply not used as the type of a field; type parameter {@code B} is ok because | ||
* it is used as the type of a field that is declared to be {@code @GuardedBy}; finally, type | ||
* parameter {@code C} is ok because it is annotated with {@code ThreadSafeTypeParameter}. | ||
* Furthermore, the declaration {@code MyThreadSafeClass<Object, Object, String>} is valid, since | ||
* the type parameter {@code C} (i.e., {@code String}) is thread-safe, whereas a declaration {@code | ||
* MyThreadSafeClass<Object, Object, Object>} would result in a compiler error. | ||
* | ||
* <p>Note: the {@code ThreadSafeTypeParameter} annotation has a secondary use case. If you annotate | ||
* a type parameter of a method, then callers to that method are only allowed to pass in a type that | ||
* is deemed thread-safe. For example, given the method declaration {@code static | ||
* <@ThreadSafeTypeParameter T> void foo(T foo) {}}, a call to {@code foo} must pass a parameter | ||
* that is deemed thread-safe. | ||
*/ | ||
@Documented | ||
@Target(TYPE_PARAMETER) | ||
@Retention(RUNTIME) | ||
public @interface ThreadSafeTypeParameter {} |