Skip to content

Commit 3b9ffc2

Browse files
java-team-github-botError Prone Team
authored andcommitted
Add support in ErrorProne for a new ThreadSafeTypeParamerter annotation that is replacing ThreadSafe.TypeParameter.
A follow-up CL will actually create the annotation once ErrorProne has this CL released to avoid premature dependencies on the new annotation. 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. PiperOrigin-RevId: 651816128
1 parent 77e4f72 commit 3b9ffc2

2 files changed

Lines changed: 31 additions & 3 deletions

File tree

core/src/main/java/com/google/errorprone/bugpatterns/threadsafety/ThreadSafeChecker.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,10 @@ public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState
9999

100100
@Override
101101
public Description matchNewClass(NewClassTree tree, VisitorState state) {
102-
// check instantiations of `@ThreadSafe.TypeParameter`s in generic constructor invocations
102+
// check instantiations of `@ThreadSafeTypeParameter`s in generic constructor invocations
103103
checkInvocation(
104104
tree, ((JCNewClass) tree).constructorType, state, ((JCNewClass) tree).constructor);
105-
// check instantiations of `@ThreadSafe.TypeParameter`s in class constructor invocations
105+
// check instantiations of `@ThreadSafeTypeParameter`s in class constructor invocations
106106
ThreadSafeAnalysis analysis = new ThreadSafeAnalysis(this, state, wellKnownThreadSafety);
107107
Violation info =
108108
analysis.checkInstantiation(
@@ -137,6 +137,7 @@ public Description matchTypeParameter(TypeParameterTree tree, VisitorState state
137137
ThreadSafeAnalysis analysis = new ThreadSafeAnalysis(this, state, wellKnownThreadSafety);
138138
if (analysis.hasThreadSafeTypeParameterAnnotation((TypeVariableSymbol) sym)) {
139139
if (analysis.getThreadSafeAnnotation(sym.owner, state) == null) {
140+
// TODO: b/324092874 -- Update this message to use the new annotation name.
140141
return buildDescription(tree)
141142
.setMessage("@ThreadSafe.TypeParameter is only supported on threadsafe classes")
142143
.build();
@@ -198,6 +199,7 @@ public Description matchClass(ClassTree tree, VisitorState state) {
198199
.map(Entry::getKey)
199200
.collect(toImmutableSet());
200201
if (!threadSafeAndContainer.isEmpty()) {
202+
// TODO: b/324092874 -- Update this message to use the new annotation name.
201203
return buildDescription(tree)
202204
.setMessage(
203205
String.format(

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,14 @@
4141
public class ThreadSafeCheckerTest {
4242

4343
private final CompilationTestHelper compilationHelper =
44-
CompilationTestHelper.newInstance(ThreadSafeChecker.class, getClass());
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 {}");
4552

4653
private final BugCheckerRefactoringTestHelper refactoringHelper =
4754
BugCheckerRefactoringTestHelper.newInstance(ThreadSafeChecker.class, getClass());
@@ -1001,6 +1008,16 @@ public void knownThreadSafeFlag() {
10011008
.doTest();
10021009
}
10031010

1011+
// TODO: b/324092874 - Remove this test once ThreadSafe.TypeParameter is removed.
1012+
1013+
// TODO: b/324092874 - Remove this test once ThreadSafe.TypeParameter is removed.
1014+
1015+
// TODO: b/324092874 - Remove this test once ThreadSafe.TypeParameter is removed.
1016+
1017+
// TODO: b/324092874 - Remove this test once ThreadSafe.TypeParameter is removed.
1018+
1019+
// TODO: b/324092874 - Remove this test once ThreadSafe.TypeParameter is removed.
1020+
10041021
@Test
10051022
public void annotatedClassType() {
10061023
compilationHelper
@@ -1015,7 +1032,16 @@ public void annotatedClassType() {
10151032
.doTest();
10161033
}
10171034

1035+
// TODO: b/324092874 - Remove this test once ThreadSafe.TypeParameter is removed.
1036+
10181037
// Regression test for b/117937500
1038+
// TODO: b/324092874 - Remove this test once ThreadSafe.TypeParameter is removed.
1039+
1040+
// Regression test for b/117937500
1041+
1042+
// javac does not instantiate type variables when they are not used for target typing, so we
1043+
// cannot check whether their instantiations are thread-safe.
1044+
// TODO: b/324092874 - Remove this test once ThreadSafe.TypeParameter is removed.
10191045

10201046
// javac does not instantiate type variables when they are not used for target typing, so we
10211047
// cannot check whether their instantiations are thread-safe.

0 commit comments

Comments
 (0)