Skip to content

Commit

Permalink
Make TypeCompatibilityUtils injectable, and drop the "Utils".
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 563861726
  • Loading branch information
graememorgan authored and Error Prone Team committed Sep 8, 2023
1 parent a992ae7 commit a89642c
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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?
*
* <p>i.e.: It is possible that an object of one type could be equal to an object of the other type.
* <p>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(
Expand Down Expand Up @@ -364,7 +360,7 @@ private static TreeSet<Type> 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();

Expand All @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -144,11 +143,11 @@ public class TruthIncompatibleType extends BugChecker implements MethodInvocatio
private static final Supplier<Type> 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
Expand Down Expand Up @@ -419,7 +418,7 @@ private static boolean isNonVarargsCall(
private Stream<Description> 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();
}
Expand Down

0 comments on commit a89642c

Please sign in to comment.