Skip to content

Commit 2656f48

Browse files
java-team-github-botError Prone Team
authored andcommitted
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
1 parent 6bd568d commit 2656f48

2 files changed

Lines changed: 68 additions & 8 deletions

File tree

core/src/test/java/com/google/errorprone/bugpatterns/threadsafety/ThreadSafeCheckerTest.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,7 @@
4141
public class ThreadSafeCheckerTest {
4242

4343
private final CompilationTestHelper compilationHelper =
44-
CompilationTestHelper.newInstance(ThreadSafeChecker.class, getClass())
45-
// TODO: b/339025111 - Remove this once ThreadSafeTypeParameter actually exists.
46-
.addSourceLines(
47-
"ThreadSafeTypeParameter.java",
48-
"package com.google.errorprone.annotations;",
49-
"@java.lang.annotation.Target(java.lang.annotation.ElementType.TYPE_PARAMETER)",
50-
"@java.lang.annotation.Retention(java.lang.annotation.RetentionPolicy.RUNTIME)",
51-
"public @interface ThreadSafeTypeParameter {}");
44+
CompilationTestHelper.newInstance(ThreadSafeChecker.class, getClass());
5245

5346
private final BugCheckerRefactoringTestHelper refactoringHelper =
5447
BugCheckerRefactoringTestHelper.newInstance(ThreadSafeChecker.class, getClass());
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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

Comments
 (0)