From c35a66da8cfe9d47c28e10312143b670cb30e0d8 Mon Sep 17 00:00:00 2001 From: Liam Miller-Cushon Date: Tue, 10 Sep 2024 11:52:06 -0700 Subject: [PATCH] Create a separate target for WellKnownThreadSafety and WellKnownMutability PiperOrigin-RevId: 673048715 --- .../threadsafety/ThreadSafety.java | 76 +--------------- .../threadsafety/ThreadSafetyKnownTypes.java | 91 +++++++++++++++++++ .../threadsafety/WellKnownMutability.java | 2 +- .../threadsafety/WellKnownThreadSafety.java | 2 +- 4 files changed, 97 insertions(+), 74 deletions(-) create mode 100644 core/src/main/java/com/google/errorprone/bugpatterns/threadsafety/ThreadSafetyKnownTypes.java 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 263bda79707..91025b33995 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 @@ -25,11 +25,9 @@ import com.google.common.base.Joiner; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableListMultimap; -import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Iterables; import com.google.common.collect.Sets; -import com.google.common.collect.Sets.SetView; import com.google.common.collect.Streams; import com.google.errorprone.VisitorState; import com.google.errorprone.annotations.CanIgnoreReturnValue; @@ -58,7 +56,6 @@ import java.lang.annotation.Annotation; import java.lang.annotation.ElementType; import java.lang.annotation.Target; -import java.lang.reflect.TypeVariable; import java.util.Arrays; import java.util.Collection; import java.util.HashSet; @@ -78,7 +75,7 @@ public final class ThreadSafety { private final VisitorState state; private final Purpose purpose; - private final KnownTypes knownTypes; + private final ThreadSafetyKnownTypes knownTypes; private final ImmutableSet markerAnnotations; private final ImmutableSet acceptedAnnotations; private final ImmutableSet containerOfAnnotation; @@ -153,7 +150,7 @@ public static class Builder { private Builder() {} private Purpose purpose = Purpose.FOR_IMMUTABLE_CHECKER; - private KnownTypes knownTypes; + private ThreadSafetyKnownTypes knownTypes; private ImmutableSet markerAnnotations; private ImmutableSet acceptedAnnotations = ImmutableSet.of(); private ImmutableSet containerOfAnnotation = ImmutableSet.of(); @@ -169,7 +166,7 @@ public Builder setPurpose(Purpose purpose) { /** Information about known types and whether they're known to be safe or unsafe. */ @CanIgnoreReturnValue - public Builder knownTypes(KnownTypes knownTypes) { + public Builder knownTypes(ThreadSafetyKnownTypes knownTypes) { this.knownTypes = knownTypes; return this; } @@ -275,7 +272,7 @@ public ThreadSafety build(VisitorState state) { private ThreadSafety( VisitorState state, Purpose purpose, - KnownTypes knownTypes, + ThreadSafetyKnownTypes knownTypes, Set markerAnnotations, Set acceptedAnnotations, Set containerOfAnnotation, @@ -291,71 +288,6 @@ private ThreadSafety( this.typeParameterAnnotation = ImmutableSet.copyOf(checkNotNull(typeParameterAnnotation)); } - /** Information about known types and whether they're known to be safe or unsafe. */ - public interface KnownTypes { - /** - * Types that are known to be safe even if they're not annotated with an expected annotation. - */ - ImmutableMap getKnownSafeClasses(); - - /** Types that are known to be unsafe and don't need testing. */ - ImmutableSet getKnownUnsafeClasses(); - - /** Helper for building maps of classes to {@link AnnotationInfo}. */ - final class MapBuilder { - final ImmutableMap.Builder mapBuilder = ImmutableMap.builder(); - - @CanIgnoreReturnValue - public MapBuilder addClasses(Set> clazzs) { - clazzs.forEach(this::add); - return this; - } - - @CanIgnoreReturnValue - public MapBuilder addStrings(List classNames) { - classNames.forEach(this::add); - return this; - } - - @CanIgnoreReturnValue - public MapBuilder addAll(ImmutableMap map) { - mapBuilder.putAll(map); - return this; - } - - @CanIgnoreReturnValue - public MapBuilder add(Class clazz, String... containerOf) { - ImmutableSet containerTyParams = ImmutableSet.copyOf(containerOf); - HashSet actualTyParams = new HashSet<>(); - for (TypeVariable x : clazz.getTypeParameters()) { - actualTyParams.add(x.getName()); - } - SetView difference = Sets.difference(containerTyParams, actualTyParams); - if (!difference.isEmpty()) { - throw new AssertionError( - String.format( - "For %s, please update the type parameter(s) from %s to %s", - clazz, difference, actualTyParams)); - } - mapBuilder.put( - clazz.getName(), - AnnotationInfo.create(clazz.getName(), ImmutableList.copyOf(containerOf))); - return this; - } - - @CanIgnoreReturnValue - public MapBuilder add(String className, String... containerOf) { - mapBuilder.put( - className, AnnotationInfo.create(className, ImmutableList.copyOf(containerOf))); - return this; - } - - public ImmutableMap build() { - return mapBuilder.buildKeepingLast(); - } - } - } - /** * A human-friendly explanation of a thread safety violations. * diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/threadsafety/ThreadSafetyKnownTypes.java b/core/src/main/java/com/google/errorprone/bugpatterns/threadsafety/ThreadSafetyKnownTypes.java new file mode 100644 index 00000000000..f7f57ae982d --- /dev/null +++ b/core/src/main/java/com/google/errorprone/bugpatterns/threadsafety/ThreadSafetyKnownTypes.java @@ -0,0 +1,91 @@ +/* + * Copyright 2024 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.bugpatterns.threadsafety; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; +import com.google.common.collect.Sets; +import com.google.common.collect.Sets.SetView; +import com.google.errorprone.annotations.CanIgnoreReturnValue; +import java.lang.reflect.TypeVariable; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +/** Information about known types and whether they're known to be safe or unsafe. */ +public interface ThreadSafetyKnownTypes { + /** Types that are known to be safe even if they're not annotated with an expected annotation. */ + ImmutableMap getKnownSafeClasses(); + + /** Types that are known to be unsafe and don't need testing. */ + ImmutableSet getKnownUnsafeClasses(); + + /** Helper for building maps of classes to {@link AnnotationInfo}. */ + final class MapBuilder { + final ImmutableMap.Builder mapBuilder = ImmutableMap.builder(); + + @CanIgnoreReturnValue + public MapBuilder addClasses(Set> clazzs) { + clazzs.forEach(this::add); + return this; + } + + @CanIgnoreReturnValue + public MapBuilder addStrings(List classNames) { + classNames.forEach(this::add); + return this; + } + + @CanIgnoreReturnValue + public MapBuilder addAll(ImmutableMap map) { + mapBuilder.putAll(map); + return this; + } + + @CanIgnoreReturnValue + public MapBuilder add(Class clazz, String... containerOf) { + ImmutableSet containerTyParams = ImmutableSet.copyOf(containerOf); + HashSet actualTyParams = new HashSet<>(); + for (TypeVariable x : clazz.getTypeParameters()) { + actualTyParams.add(x.getName()); + } + SetView difference = Sets.difference(containerTyParams, actualTyParams); + if (!difference.isEmpty()) { + throw new AssertionError( + String.format( + "For %s, please update the type parameter(s) from %s to %s", + clazz, difference, actualTyParams)); + } + mapBuilder.put( + clazz.getName(), + AnnotationInfo.create(clazz.getName(), ImmutableList.copyOf(containerOf))); + return this; + } + + @CanIgnoreReturnValue + public MapBuilder add(String className, String... containerOf) { + mapBuilder.put( + className, AnnotationInfo.create(className, ImmutableList.copyOf(containerOf))); + return this; + } + + public ImmutableMap build() { + return mapBuilder.buildKeepingLast(); + } + } +} diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/threadsafety/WellKnownMutability.java b/core/src/main/java/com/google/errorprone/bugpatterns/threadsafety/WellKnownMutability.java index 8a2bb04a368..1b6230b565f 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/threadsafety/WellKnownMutability.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/threadsafety/WellKnownMutability.java @@ -36,7 +36,7 @@ /** A collection of types with known mutability. */ @Immutable -public final class WellKnownMutability implements ThreadSafety.KnownTypes { +public final class WellKnownMutability implements ThreadSafetyKnownTypes { /** Types that are known to be immutable. */ private final ImmutableMap knownImmutableClasses; diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/threadsafety/WellKnownThreadSafety.java b/core/src/main/java/com/google/errorprone/bugpatterns/threadsafety/WellKnownThreadSafety.java index dc2202906a1..41e1b070fdf 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/threadsafety/WellKnownThreadSafety.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/threadsafety/WellKnownThreadSafety.java @@ -24,7 +24,7 @@ import javax.inject.Inject; /** A collection of types with known thread safety. */ -public final class WellKnownThreadSafety implements ThreadSafety.KnownTypes { +public final class WellKnownThreadSafety implements ThreadSafetyKnownTypes { @Inject WellKnownThreadSafety(ErrorProneFlags flags, WellKnownMutability wellKnownMutability) { ImmutableList knownThreadSafe = flags.getListOrEmpty("ThreadSafe:KnownThreadSafe");