Skip to content

Commit

Permalink
Consolidate the construction of ThreadSafety for ThreadSafe usage.
Browse files Browse the repository at this point in the history
So that the teaching of checks about the new annotation is only in one place.

PiperOrigin-RevId: 515113701
  • Loading branch information
graememorgan authored and Error Prone Team committed Mar 8, 2023
1 parent 1b80a5e commit cbb7bd1
Showing 1 changed file with 32 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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<String> markerAnnotations) {
return markerAnnotations(ImmutableSet.copyOf(markerAnnotations));
}

// TODO(ringwalt): Remove this constructor. We need it for binary compatibility.
@CanIgnoreReturnValue
public Builder markerAnnotations(ImmutableSet<String> markerAnnotations) {
public Builder markerAnnotations(Iterable<String> markerAnnotations) {
checkNotNull(markerAnnotations);
this.markerAnnotations = markerAnnotations;
this.markerAnnotations = ImmutableSet.copyOf(markerAnnotations);
return this;
}

Expand All @@ -184,15 +203,9 @@ public Builder markerAnnotations(ImmutableSet<String> markerAnnotations) {
* thread-safe.
*/
@CanIgnoreReturnValue
public Builder acceptedAnnotations(Set<String> acceptedAnnotations) {
return acceptedAnnotations(ImmutableSet.copyOf(acceptedAnnotations));
}

// TODO(ringwalt): Remove this constructor. We need it for binary compatibility.
@CanIgnoreReturnValue
public Builder acceptedAnnotations(ImmutableSet<String> acceptedAnnotations) {
public Builder acceptedAnnotations(Iterable<String> acceptedAnnotations) {
checkNotNull(acceptedAnnotations);
this.acceptedAnnotations = acceptedAnnotations;
this.acceptedAnnotations = ImmutableSet.copyOf(acceptedAnnotations);
return this;
}

Expand All @@ -206,7 +219,7 @@ public Builder containerOfAnnotation(Class<? extends Annotation> containerOfAnno

/** An annotation which marks a generic parameter as a container type. */
@CanIgnoreReturnValue
public Builder containerOfAnnotation(Set<String> containerOfAnnotation) {
public Builder containerOfAnnotation(Iterable<String> containerOfAnnotation) {
checkNotNull(containerOfAnnotation);
this.containerOfAnnotation = ImmutableSet.copyOf(containerOfAnnotation);
return this;
Expand All @@ -222,7 +235,7 @@ public Builder suppressAnnotation(Class<? extends Annotation> suppressAnnotation

/** An annotation which, when found on a class, should suppress the test */
@CanIgnoreReturnValue
public Builder suppressAnnotation(Set<String> suppressAnnotation) {
public Builder suppressAnnotation(Iterable<String> suppressAnnotation) {
checkNotNull(suppressAnnotation);
this.suppressAnnotation = ImmutableSet.copyOf(suppressAnnotation);
return this;
Expand All @@ -248,7 +261,7 @@ public Builder typeParameterAnnotation(Class<? extends Annotation> typeParameter
* only be instantiated with thread-safe types.
*/
@CanIgnoreReturnValue
public Builder typeParameterAnnotation(Set<String> typeParameterAnnotation) {
public Builder typeParameterAnnotation(Iterable<String> typeParameterAnnotation) {
checkNotNull(typeParameterAnnotation);
this.typeParameterAnnotation = ImmutableSet.copyOf(typeParameterAnnotation);
return this;
Expand Down

0 comments on commit cbb7bd1

Please sign in to comment.