From a89642c25444a3785074b20c70965f2efb49e636 Mon Sep 17 00:00:00 2001 From: ghm Date: Fri, 8 Sep 2023 15:05:47 -0700 Subject: [PATCH] Make TypeCompatibilityUtils injectable, and drop the "Utils". PiperOrigin-RevId: 563861726 --- .../bugpatterns/EqualsIncompatibleType.java | 11 +++---- ...ilityUtils.java => TypeCompatibility.java} | 31 ++++++++----------- .../CollectionIncompatibleType.java | 13 ++++---- .../IncompatibleArgumentType.java | 13 ++++---- .../TruthIncompatibleType.java | 13 ++++---- 5 files changed, 36 insertions(+), 45 deletions(-) rename core/src/main/java/com/google/errorprone/bugpatterns/{TypeCompatibilityUtils.java => TypeCompatibility.java} (95%) diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/EqualsIncompatibleType.java b/core/src/main/java/com/google/errorprone/bugpatterns/EqualsIncompatibleType.java index 1fc91f1fd78..9ba0b0461ac 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/EqualsIncompatibleType.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/EqualsIncompatibleType.java @@ -31,11 +31,10 @@ import static com.google.errorprone.util.ASTHelpers.getType; import com.google.errorprone.BugPattern; -import com.google.errorprone.ErrorProneFlags; import com.google.errorprone.VisitorState; import com.google.errorprone.bugpatterns.BugChecker.MemberReferenceTreeMatcher; import com.google.errorprone.bugpatterns.BugChecker.MethodInvocationTreeMatcher; -import com.google.errorprone.bugpatterns.TypeCompatibilityUtils.TypeCompatibilityReport; +import com.google.errorprone.bugpatterns.TypeCompatibility.TypeCompatibilityReport; import com.google.errorprone.matchers.Description; import com.google.errorprone.matchers.Matcher; import com.google.errorprone.util.ASTHelpers; @@ -69,11 +68,11 @@ public class EqualsIncompatibleType extends BugChecker instanceMethod().anyClass().named("assertFalse"), staticMethod().anyClass().named("assertFalse"))); - private final TypeCompatibilityUtils typeCompatibilityUtils; + private final TypeCompatibility typeCompatibility; @Inject - EqualsIncompatibleType(ErrorProneFlags flags) { - this.typeCompatibilityUtils = TypeCompatibilityUtils.fromFlags(flags); + EqualsIncompatibleType(TypeCompatibility typeCompatibility) { + this.typeCompatibility = typeCompatibility; } @Override @@ -136,7 +135,7 @@ public Description matchMemberReference(MemberReferenceTree tree, VisitorState s private Description match( ExpressionTree invocationTree, Type receiverType, Type argumentType, VisitorState state) { TypeCompatibilityReport compatibilityReport = - typeCompatibilityUtils.compatibilityOfTypes(receiverType, argumentType, state); + typeCompatibility.compatibilityOfTypes(receiverType, argumentType, state); if (compatibilityReport.isCompatible()) { return NO_MATCH; } diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/TypeCompatibilityUtils.java b/core/src/main/java/com/google/errorprone/bugpatterns/TypeCompatibility.java similarity index 95% rename from core/src/main/java/com/google/errorprone/bugpatterns/TypeCompatibilityUtils.java rename to core/src/main/java/com/google/errorprone/bugpatterns/TypeCompatibility.java index 9dfc93299ab..bf827bd6e0d 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/TypeCompatibilityUtils.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/TypeCompatibility.java @@ -44,30 +44,26 @@ import java.util.Set; import java.util.TreeSet; import javax.annotation.Nullable; +import javax.inject.Inject; import javax.lang.model.type.TypeKind; /** - * Logical utility methods to answer the question: Are these two types "compatible" with each other, - * in the context of an equality check. + * Methods to answer the question: are these two types "compatible" with each other, in the context + * of an equality check? * - *

i.e.: It is possible that an object of one type could be equal to an object of the other type. + *

That is, based on the types alone, is it possible for them to be equal. In general this + * requires a common superclass that overrides {@link Object#equals(Object)}, but there are + * complexities and special-cases. */ -public final class TypeCompatibilityUtils { +public final class TypeCompatibility { private static final String WITHOUT_EQUALS_REASON = ". Though these types are the same, the type doesn't implement equals."; private final boolean treatBuildersAsIncomparable; - public static TypeCompatibilityUtils fromFlags(ErrorProneFlags flags) { - return new TypeCompatibilityUtils( - flags.getBoolean("TypeCompatibility:TreatBuildersAsIncomparable").orElse(true)); - } - - public static TypeCompatibilityUtils allOn() { - return new TypeCompatibilityUtils(/* treatBuildersAsIncomparable= */ true); - } - - private TypeCompatibilityUtils(boolean treatBuildersAsIncomparable) { - this.treatBuildersAsIncomparable = treatBuildersAsIncomparable; + @Inject + TypeCompatibility(ErrorProneFlags flags) { + this.treatBuildersAsIncomparable = + flags.getBoolean("TypeCompatibility:TreatBuildersAsIncomparable").orElse(true); } public TypeCompatibilityReport compatibilityOfTypes( @@ -364,7 +360,7 @@ private static TreeSet typeSet(VisitorState state) { @AutoValue public abstract static class TypeCompatibilityReport { private static final TypeCompatibilityReport COMPATIBLE = - new AutoValue_TypeCompatibilityUtils_TypeCompatibilityReport(true, null, null, null); + new AutoValue_TypeCompatibility_TypeCompatibilityReport(true, null, null, null); public abstract boolean isCompatible(); @@ -386,8 +382,7 @@ static TypeCompatibilityReport incompatible(Type lhs, Type rhs) { } static TypeCompatibilityReport incompatible(Type lhs, Type rhs, String extraReason) { - return new AutoValue_TypeCompatibilityUtils_TypeCompatibilityReport( - false, lhs, rhs, extraReason); + return new AutoValue_TypeCompatibility_TypeCompatibilityReport(false, lhs, rhs, extraReason); } } diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/collectionincompatibletype/CollectionIncompatibleType.java b/core/src/main/java/com/google/errorprone/bugpatterns/collectionincompatibletype/CollectionIncompatibleType.java index 57db1931705..370c119b15b 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/collectionincompatibletype/CollectionIncompatibleType.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/collectionincompatibletype/CollectionIncompatibleType.java @@ -26,8 +26,8 @@ import com.google.errorprone.bugpatterns.BugChecker; import com.google.errorprone.bugpatterns.BugChecker.MemberReferenceTreeMatcher; import com.google.errorprone.bugpatterns.BugChecker.MethodInvocationTreeMatcher; -import com.google.errorprone.bugpatterns.TypeCompatibilityUtils; -import com.google.errorprone.bugpatterns.TypeCompatibilityUtils.TypeCompatibilityReport; +import com.google.errorprone.bugpatterns.TypeCompatibility; +import com.google.errorprone.bugpatterns.TypeCompatibility.TypeCompatibilityReport; import com.google.errorprone.bugpatterns.collectionincompatibletype.AbstractCollectionIncompatibleTypeMatcher.MatchResult; import com.google.errorprone.fixes.SuggestedFix; import com.google.errorprone.matchers.Description; @@ -72,13 +72,13 @@ private enum FixType { } private final FixType fixType; - private final TypeCompatibilityUtils typeCompatibilityUtils; + private final TypeCompatibility typeCompatibility; @Inject - CollectionIncompatibleType(ErrorProneFlags flags) { + CollectionIncompatibleType(TypeCompatibility typeCompatibility, ErrorProneFlags flags) { this.fixType = flags.getEnum("CollectionIncompatibleType:FixType", FixType.class).orElse(FixType.NONE); - this.typeCompatibilityUtils = TypeCompatibilityUtils.fromFlags(flags); + this.typeCompatibility = typeCompatibility; } @Override @@ -99,8 +99,7 @@ public Description match(ExpressionTree tree, VisitorState state) { Types types = state.getTypes(); TypeCompatibilityReport compatibilityReport = - typeCompatibilityUtils.compatibilityOfTypes( - result.targetType(), result.sourceType(), state); + typeCompatibility.compatibilityOfTypes(result.targetType(), result.sourceType(), state); if (compatibilityReport.isCompatible()) { return NO_MATCH; } diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/collectionincompatibletype/IncompatibleArgumentType.java b/core/src/main/java/com/google/errorprone/bugpatterns/collectionincompatibletype/IncompatibleArgumentType.java index 581d316ba86..2390babf1a0 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/collectionincompatibletype/IncompatibleArgumentType.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/collectionincompatibletype/IncompatibleArgumentType.java @@ -22,14 +22,13 @@ import com.google.auto.value.AutoValue; import com.google.common.collect.ImmutableListMultimap; import com.google.errorprone.BugPattern; -import com.google.errorprone.ErrorProneFlags; import com.google.errorprone.VisitorState; import com.google.errorprone.annotations.CheckReturnValue; import com.google.errorprone.annotations.CompatibleWith; import com.google.errorprone.bugpatterns.BugChecker; import com.google.errorprone.bugpatterns.BugChecker.MethodInvocationTreeMatcher; -import com.google.errorprone.bugpatterns.TypeCompatibilityUtils; -import com.google.errorprone.bugpatterns.TypeCompatibilityUtils.TypeCompatibilityReport; +import com.google.errorprone.bugpatterns.TypeCompatibility; +import com.google.errorprone.bugpatterns.TypeCompatibility.TypeCompatibilityReport; import com.google.errorprone.matchers.Description; import com.google.errorprone.util.ASTHelpers; import com.google.errorprone.util.Signatures; @@ -58,11 +57,11 @@ severity = ERROR) public class IncompatibleArgumentType extends BugChecker implements MethodInvocationTreeMatcher { - private final TypeCompatibilityUtils typeCompatibilityUtils; + private final TypeCompatibility typeCompatibility; @Inject - IncompatibleArgumentType(ErrorProneFlags flags) { - this.typeCompatibilityUtils = TypeCompatibilityUtils.fromFlags(flags); + IncompatibleArgumentType(TypeCompatibility typeCompatibility) { + this.typeCompatibility = typeCompatibility; } // Nonnull requiredType: The type I need is bound, in requiredType @@ -134,7 +133,7 @@ private void reportAnyViolations( if (requiredType.type() != null) { // Report a violation for this type TypeCompatibilityReport report = - typeCompatibilityUtils.compatibilityOfTypes(requiredType.type(), argType, state); + typeCompatibility.compatibilityOfTypes(requiredType.type(), argType, state); if (!report.isCompatible()) { state.reportMatch( describeViolation(argument, argType, requiredType.type(), types, state)); diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/collectionincompatibletype/TruthIncompatibleType.java b/core/src/main/java/com/google/errorprone/bugpatterns/collectionincompatibletype/TruthIncompatibleType.java index 25d7bf7460e..334cfa8803b 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/collectionincompatibletype/TruthIncompatibleType.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/collectionincompatibletype/TruthIncompatibleType.java @@ -35,12 +35,11 @@ import com.google.common.collect.Streams; import com.google.errorprone.BugPattern; -import com.google.errorprone.ErrorProneFlags; import com.google.errorprone.VisitorState; import com.google.errorprone.bugpatterns.BugChecker; import com.google.errorprone.bugpatterns.BugChecker.MethodInvocationTreeMatcher; -import com.google.errorprone.bugpatterns.TypeCompatibilityUtils; -import com.google.errorprone.bugpatterns.TypeCompatibilityUtils.TypeCompatibilityReport; +import com.google.errorprone.bugpatterns.TypeCompatibility; +import com.google.errorprone.bugpatterns.TypeCompatibility.TypeCompatibilityReport; import com.google.errorprone.matchers.Description; import com.google.errorprone.matchers.Matcher; import com.google.errorprone.suppliers.Supplier; @@ -144,11 +143,11 @@ public class TruthIncompatibleType extends BugChecker implements MethodInvocatio private static final Supplier CORRESPONDENCE = typeFromString("com.google.common.truth.Correspondence"); - private final TypeCompatibilityUtils typeCompatibilityUtils; + private final TypeCompatibility typeCompatibility; @Inject - TruthIncompatibleType(ErrorProneFlags flags) { - this.typeCompatibilityUtils = TypeCompatibilityUtils.fromFlags(flags); + TruthIncompatibleType(TypeCompatibility typeCompatibility) { + this.typeCompatibility = typeCompatibility; } @Override @@ -419,7 +418,7 @@ private static boolean isNonVarargsCall( private Stream checkCompatibility( ExpressionTree tree, Type targetType, Type sourceType, VisitorState state) { TypeCompatibilityReport compatibilityReport = - typeCompatibilityUtils.compatibilityOfTypes(targetType, sourceType, state); + typeCompatibility.compatibilityOfTypes(targetType, sourceType, state); if (compatibilityReport.isCompatible()) { return Stream.empty(); }