From cbb7bd18ed4a0f6006c4944a6178647e31cd6569 Mon Sep 17 00:00:00 2001 From: ghm Date: Wed, 8 Mar 2023 12:42:56 -0800 Subject: [PATCH] Consolidate the construction of ThreadSafety for ThreadSafe usage. So that the teaching of checks about the new annotation is only in one place. PiperOrigin-RevId: 515113701 --- .../threadsafety/ThreadSafety.java | 51 ++++++++++++------- 1 file changed, 32 insertions(+), 19 deletions(-) diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/threadsafety/ThreadSafety.java b/core/src/main/java/com/google/errorprone/bugpatterns/threadsafety/ThreadSafety.java index 64783668c85..0155150e239 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/threadsafety/ThreadSafety.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/threadsafety/ThreadSafety.java @@ -21,6 +21,7 @@ import static com.google.common.collect.ImmutableList.toImmutableList; import static com.google.errorprone.util.ASTHelpers.isStatic; +import com.google.async.threadsafety.annotations.ThreadSafe; import com.google.auto.value.AutoValue; import com.google.common.base.Joiner; import com.google.common.collect.ImmutableList; @@ -31,6 +32,7 @@ import com.google.common.collect.Streams; import com.google.errorprone.VisitorState; import com.google.errorprone.annotations.CanIgnoreReturnValue; +import com.google.errorprone.annotations.Immutable; import com.google.errorprone.bugpatterns.CanBeStaticAnalyzer; import com.google.errorprone.suppliers.Supplier; import com.google.errorprone.util.ASTHelpers; @@ -87,6 +89,29 @@ public static Builder builder() { return new Builder(); } + public static ThreadSafety.Builder threadSafeBuilder( + WellKnownThreadSafety wellKnownThreadSafety) { + return ThreadSafety.builder() + .setPurpose(Purpose.FOR_THREAD_SAFE_CHECKER) + .knownTypes(wellKnownThreadSafety) + .markerAnnotations( + ImmutableSet.of( + ThreadSafe.class.getName(), "com.google.errorprone.annotations.ThreadSafe")) + .acceptedAnnotations(ImmutableSet.of(Immutable.class.getName())) + .containerOfAnnotation( + ImmutableSet.of( + ThreadSafe.Element.class.getName(), + "com.google.errorprone.annotations.ThreadSafe$Element")) + .typeParameterAnnotation( + ImmutableSet.of( + ThreadSafe.TypeParameter.class.getName(), + "com.google.errorprone.annotations.ThreadSafe$TypeParameter")) + .suppressAnnotation( + ImmutableSet.of( + ThreadSafe.Suppress.class.getName(), + "com.google.errorprone.annotations.ThreadSafe$Suppress")); + } + /** * The {@link ThreadSafety} utility class can be used by either the bug checker that enforces * immutability or by the bug checker that enforces thread-safety. Depending on which of these bug @@ -165,15 +190,9 @@ public Builder knownTypes(KnownTypes knownTypes) { * example, when testing a class for immutability, this should be @Immutable. */ @CanIgnoreReturnValue - public Builder markerAnnotations(Set markerAnnotations) { - return markerAnnotations(ImmutableSet.copyOf(markerAnnotations)); - } - - // TODO(ringwalt): Remove this constructor. We need it for binary compatibility. - @CanIgnoreReturnValue - public Builder markerAnnotations(ImmutableSet markerAnnotations) { + public Builder markerAnnotations(Iterable markerAnnotations) { checkNotNull(markerAnnotations); - this.markerAnnotations = markerAnnotations; + this.markerAnnotations = ImmutableSet.copyOf(markerAnnotations); return this; } @@ -184,15 +203,9 @@ public Builder markerAnnotations(ImmutableSet markerAnnotations) { * thread-safe. */ @CanIgnoreReturnValue - public Builder acceptedAnnotations(Set acceptedAnnotations) { - return acceptedAnnotations(ImmutableSet.copyOf(acceptedAnnotations)); - } - - // TODO(ringwalt): Remove this constructor. We need it for binary compatibility. - @CanIgnoreReturnValue - public Builder acceptedAnnotations(ImmutableSet acceptedAnnotations) { + public Builder acceptedAnnotations(Iterable acceptedAnnotations) { checkNotNull(acceptedAnnotations); - this.acceptedAnnotations = acceptedAnnotations; + this.acceptedAnnotations = ImmutableSet.copyOf(acceptedAnnotations); return this; } @@ -206,7 +219,7 @@ public Builder containerOfAnnotation(Class containerOfAnno /** An annotation which marks a generic parameter as a container type. */ @CanIgnoreReturnValue - public Builder containerOfAnnotation(Set containerOfAnnotation) { + public Builder containerOfAnnotation(Iterable containerOfAnnotation) { checkNotNull(containerOfAnnotation); this.containerOfAnnotation = ImmutableSet.copyOf(containerOfAnnotation); return this; @@ -222,7 +235,7 @@ public Builder suppressAnnotation(Class suppressAnnotation /** An annotation which, when found on a class, should suppress the test */ @CanIgnoreReturnValue - public Builder suppressAnnotation(Set suppressAnnotation) { + public Builder suppressAnnotation(Iterable suppressAnnotation) { checkNotNull(suppressAnnotation); this.suppressAnnotation = ImmutableSet.copyOf(suppressAnnotation); return this; @@ -248,7 +261,7 @@ public Builder typeParameterAnnotation(Class typeParameter * only be instantiated with thread-safe types. */ @CanIgnoreReturnValue - public Builder typeParameterAnnotation(Set typeParameterAnnotation) { + public Builder typeParameterAnnotation(Iterable typeParameterAnnotation) { checkNotNull(typeParameterAnnotation); this.typeParameterAnnotation = ImmutableSet.copyOf(typeParameterAnnotation); return this;