Consolidates `null` checks in `JSType` equals methods into `checkEquivalenceHelper`, the lowest level method.
This change introduces a `null` check in one case where it was missing.
@@ -636,19 +636,25 @@ public abstract class JSType implements Serializable {
return false;
}
- /**
- * Checks if two types are equivalent.
- */
- public final boolean isEquivalentTo(JSType that) {
+ @Override
+ public boolean equals(@Nullable Object jsType) {
+ return (jsType instanceof JSType) && isEquivalentTo((JSType) jsType);
+ }
+
+ /** Checks if two types are equivalent. */
+ public final boolean isEquivalentTo(@Nullable JSType that) {
return isEquivalentTo(that, false);
}
- public final boolean isEquivalentTo(JSType that, boolean isStructural) {
- EqCache eqCache = isStructural ? EqCache.create()
- : EqCache.createWithoutStructuralTyping();
+ public final boolean isEquivalentTo(@Nullable JSType that, boolean isStructural) {
+ EqCache eqCache = isStructural ? EqCache.create() : EqCache.createWithoutStructuralTyping();
return checkEquivalenceHelper(that, EquivalenceMethod.IDENTITY, eqCache);
}
+ public static final boolean isEquivalent(@Nullable JSType typeA, @Nullable JSType typeB) {
+ return (typeA == null) ? (typeB == null) : typeA.isEquivalentTo(typeB);
+ }
+
/**
* Whether this type is meaningfully different from {@code that} type for
* the purposes of data flow analysis.
@@ -673,10 +679,14 @@ public abstract class JSType implements Serializable {
EqCache.create());
}
- boolean checkEquivalenceHelper(final JSType that, EquivalenceMethod eqMethod, EqCache eqCache) {
- if (areIdentical(this, that)) {
+ boolean checkEquivalenceHelper(
+ final @Nullable JSType that, EquivalenceMethod eqMethod, EqCache eqCache) {
+ if (that == null) {
+ return false;
+ } else if (areIdentical(this, that)) {
return true;
}
+
if (this.isNoResolvedType() && that.isNoResolvedType()) {
if (this.isNamedType() && that.isNamedType()) {
return Objects.equals(
@@ -770,17 +780,6 @@ public abstract class JSType implements Serializable {
return objType.getReferenceName();
}
- public static boolean isEquivalent(JSType typeA, JSType typeB) {
- return (typeA == null || typeB == null)
- ? areIdentical(typeA, typeB)
- : typeA.isEquivalentTo(typeB);
- }
-
- @Override
- public boolean equals(Object jsType) {
- return (jsType instanceof JSType) && isEquivalentTo((JSType) jsType);
- }
-
/**
* Calculates a hash of the object as per {@link Object#hashCode()}.
*
