aboutsummaryrefslogtreecommitdiff
path: root/common/src/main/java/com/google/auto/common/MoreTypes.java
diff options
context:
space:
mode:
Diffstat (limited to 'common/src/main/java/com/google/auto/common/MoreTypes.java')
-rw-r--r--common/src/main/java/com/google/auto/common/MoreTypes.java70
1 files changed, 43 insertions, 27 deletions
diff --git a/common/src/main/java/com/google/auto/common/MoreTypes.java b/common/src/main/java/com/google/auto/common/MoreTypes.java
index e09680bc..1a490626 100644
--- a/common/src/main/java/com/google/auto/common/MoreTypes.java
+++ b/common/src/main/java/com/google/auto/common/MoreTypes.java
@@ -25,7 +25,6 @@ import static javax.lang.model.type.TypeKind.TYPEVAR;
import static javax.lang.model.type.TypeKind.WILDCARD;
import com.google.common.base.Equivalence;
-import com.google.common.base.Objects;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
@@ -55,6 +54,7 @@ import javax.lang.model.type.WildcardType;
import javax.lang.model.util.Elements;
import javax.lang.model.util.SimpleTypeVisitor8;
import javax.lang.model.util.Types;
+import org.checkerframework.checker.nullness.qual.Nullable;
/**
* Utilities related to {@link TypeMirror} instances.
@@ -75,6 +75,11 @@ public final class MoreTypes {
protected int doHash(TypeMirror t) {
return MoreTypes.hash(t, ImmutableSet.<Element>of());
}
+
+ @Override
+ public String toString() {
+ return "MoreTypes.equivalence()";
+ }
}
/**
@@ -135,13 +140,11 @@ public final class MoreTypes {
}
@Override
- public boolean equals(Object o) {
+ public boolean equals(@Nullable Object o) {
if (o instanceof ComparedElements) {
ComparedElements that = (ComparedElements) o;
int nArguments = aArguments.size();
- if (!this.a.equals(that.a)
- || !this.b.equals(that.b)
- || nArguments != bArguments.size()) {
+ if (!this.a.equals(that.a) || !this.b.equals(that.b) || nArguments != bArguments.size()) {
// The arguments must be the same size, but we check anyway.
return false;
}
@@ -294,7 +297,14 @@ public final class MoreTypes {
}
@SuppressWarnings("TypeEquals")
- private static boolean equal(TypeMirror a, TypeMirror b, Set<ComparedElements> visiting) {
+ private static boolean equal(
+ @Nullable TypeMirror a, @Nullable TypeMirror b, Set<ComparedElements> visiting) {
+ if (a == b) {
+ return true;
+ }
+ if (a == null || b == null) {
+ return false;
+ }
// TypeMirror.equals is not guaranteed to return true for types that are equal, but we can
// assume that if it does return true then the types are equal. This check also avoids getting
// stuck in infinite recursion when Eclipse decrees that the upper bound of the second K in
@@ -302,13 +312,15 @@ public final class MoreTypes {
// The javac implementation of ExecutableType, at least in some versions, does not take thrown
// exceptions into account in its equals implementation, so avoid this optimization for
// ExecutableType.
- if (Objects.equal(a, b) && !(a instanceof ExecutableType)) {
+ @SuppressWarnings("TypesEquals")
+ boolean equal = a.equals(b);
+ if (equal && !(a instanceof ExecutableType)) {
return true;
}
EqualVisitorParam p = new EqualVisitorParam();
p.type = b;
p.visiting = visiting;
- return (a == b) || (a != null && b != null && a.accept(EqualVisitor.INSTANCE, p));
+ return a.accept(EqualVisitor.INSTANCE, p);
}
/**
@@ -318,7 +330,7 @@ public final class MoreTypes {
* <a href="https://bugs.eclipse.org/bugs/show_bug.cgi?id=508222">this bug</a> whereby
* the Eclipse compiler returns a value for static classes that is not NoType.
*/
- private static TypeMirror enclosingType(DeclaredType t) {
+ private static @Nullable TypeMirror enclosingType(DeclaredType t) {
TypeMirror enclosing = t.getEnclosingType();
if (enclosing.getKind().equals(TypeKind.NONE)
|| t.asElement().getModifiers().contains(Modifier.STATIC)) {
@@ -337,16 +349,14 @@ public final class MoreTypes {
Iterator<? extends TypeMirror> aIterator = a.iterator();
Iterator<? extends TypeMirror> bIterator = b.iterator();
while (aIterator.hasNext()) {
- if (!bIterator.hasNext()) {
- return false;
- }
+ // We checked that the lists have the same size, so we know that bIterator.hasNext() too.
TypeMirror nextMirrorA = aIterator.next();
TypeMirror nextMirrorB = bIterator.next();
if (!equal(nextMirrorA, nextMirrorB, visiting)) {
return false;
}
}
- return !aIterator.hasNext();
+ return true;
}
private static final int HASH_SEED = 17;
@@ -433,7 +443,7 @@ public final class MoreTypes {
public Integer visitUnknown(TypeMirror t, Set<Element> visiting) {
throw new UnsupportedOperationException();
}
- };
+ }
private static int hashList(List<? extends TypeMirror> mirrors, Set<Element> visiting) {
int result = HASH_SEED;
@@ -460,17 +470,17 @@ public final class MoreTypes {
}
private static final class ReferencedTypes
- extends SimpleTypeVisitor8<Void, ImmutableSet.Builder<TypeElement>> {
+ extends SimpleTypeVisitor8<@Nullable Void, ImmutableSet.Builder<TypeElement>> {
private static final ReferencedTypes INSTANCE = new ReferencedTypes();
@Override
- public Void visitArray(ArrayType t, ImmutableSet.Builder<TypeElement> p) {
+ public @Nullable Void visitArray(ArrayType t, ImmutableSet.Builder<TypeElement> p) {
t.getComponentType().accept(this, p);
return null;
}
@Override
- public Void visitDeclared(DeclaredType t, ImmutableSet.Builder<TypeElement> p) {
+ public @Nullable Void visitDeclared(DeclaredType t, ImmutableSet.Builder<TypeElement> p) {
p.add(MoreElements.asType(t.asElement()));
for (TypeMirror typeArgument : t.getTypeArguments()) {
typeArgument.accept(this, p);
@@ -479,14 +489,14 @@ public final class MoreTypes {
}
@Override
- public Void visitTypeVariable(TypeVariable t, ImmutableSet.Builder<TypeElement> p) {
+ public @Nullable Void visitTypeVariable(TypeVariable t, ImmutableSet.Builder<TypeElement> p) {
t.getLowerBound().accept(this, p);
t.getUpperBound().accept(this, p);
return null;
}
@Override
- public Void visitWildcard(WildcardType t, ImmutableSet.Builder<TypeElement> p) {
+ public @Nullable Void visitWildcard(WildcardType t, ImmutableSet.Builder<TypeElement> p) {
TypeMirror extendsBound = t.getExtendsBound();
if (extendsBound != null) {
extendsBound.accept(this, p);
@@ -534,7 +544,8 @@ public final class MoreTypes {
public Element visitTypeVariable(TypeVariable t, Void p) {
return t.asElement();
}
- };
+ }
+ ;
// TODO(gak): consider removing these two methods as they're pretty trivial now
public static TypeElement asTypeElement(TypeMirror mirror) {
@@ -833,6 +844,11 @@ public final class MoreTypes {
}
@Override
+ public Boolean visitError(ErrorType errorType, Void p) {
+ return false;
+ }
+
+ @Override
public Boolean visitPrimitive(PrimitiveType type, Void p) {
switch (type.getKind()) {
case BOOLEAN:
@@ -873,11 +889,11 @@ public final class MoreTypes {
* {@link Optional#absent()} if {@code type} is an interface or {@link Object} or its superclass
* is {@link Object}.
*/
- // TODO(user): Remove unused parameter Elements?
- public static Optional<DeclaredType> nonObjectSuperclass(Types types, Elements elements,
- DeclaredType type) {
+ // TODO(bcorso): Remove unused parameter Elements?
+ public static Optional<DeclaredType> nonObjectSuperclass(
+ Types types, Elements elements, DeclaredType type) {
checkNotNull(types);
- checkNotNull(elements); // This is no longer used, but here to avoid changing the API.
+ checkNotNull(elements); // This is no longer used, but here to avoid changing the API.
checkNotNull(type);
TypeMirror superclassType = asTypeElement(type).getSuperclass();
@@ -885,7 +901,7 @@ public final class MoreTypes {
return Optional.absent();
}
- DeclaredType superclass = asDeclared(superclassType);
+ DeclaredType superclass = asDeclared(superclassType);
if (isObjectType(superclass)) {
return Optional.absent();
}
@@ -912,8 +928,8 @@ public final class MoreTypes {
* {@code container} of type {@code Set<String>}, and a variable corresponding to the {@code E e}
* parameter in the {@code Set.add(E e)} method, this will return a TypeMirror for {@code String}.
*/
- public static TypeMirror asMemberOf(Types types, DeclaredType container,
- VariableElement variable) {
+ public static TypeMirror asMemberOf(
+ Types types, DeclaredType container, VariableElement variable) {
if (variable.getKind().equals(ElementKind.PARAMETER)) {
ExecutableElement methodOrConstructor =
MoreElements.asExecutable(variable.getEnclosingElement());