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 398d411a95..89af58bb21 100644 --- a/common/src/main/java/com/google/auto/common/MoreTypes.java +++ b/common/src/main/java/com/google/auto/common/MoreTypes.java @@ -21,6 +21,7 @@ import static javax.lang.model.type.TypeKind.ARRAY; import static javax.lang.model.type.TypeKind.DECLARED; import static javax.lang.model.type.TypeKind.EXECUTABLE; +import static javax.lang.model.type.TypeKind.INTERSECTION; import static javax.lang.model.type.TypeKind.TYPEVAR; import static javax.lang.model.type.TypeKind.WILDCARD; @@ -28,11 +29,9 @@ import com.google.common.base.Objects; import com.google.common.base.Optional; import com.google.common.base.Predicate; -import com.google.common.base.Throwables; import com.google.common.collect.FluentIterable; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; -import java.lang.reflect.Method; import java.util.HashSet; import java.util.Iterator; import java.util.List; @@ -48,6 +47,7 @@ import javax.lang.model.type.DeclaredType; import javax.lang.model.type.ErrorType; import javax.lang.model.type.ExecutableType; +import javax.lang.model.type.IntersectionType; import javax.lang.model.type.NoType; import javax.lang.model.type.NullType; import javax.lang.model.type.PrimitiveType; @@ -224,6 +224,15 @@ && equalLists(a.getThrownTypes(), b.getThrownTypes(), p.visiting) return false; } + @Override + public Boolean visitIntersection(IntersectionType a, EqualVisitorParam p) { + if (p.type.getKind().equals(INTERSECTION)) { + IntersectionType b = (IntersectionType) p.type; + return equalLists(a.getBounds(), b.getBounds(), p.visiting); + } + return false; + } + @Override public Boolean visitTypeVariable(TypeVariable a, EqualVisitorParam p) { if (p.type.getKind().equals(TYPEVAR)) { @@ -286,23 +295,6 @@ private Set visitingSetPlus( } } - private static final Class INTERSECTION_TYPE; - private static final Method GET_BOUNDS; - - static { - Class c; - Method m; - try { - c = Class.forName("javax.lang.model.type.IntersectionType"); - m = c.getMethod("getBounds"); - } catch (Exception e) { - c = null; - m = null; - } - INTERSECTION_TYPE = c; - GET_BOUNDS = m; - } - private static boolean equal(TypeMirror a, TypeMirror b, Set visiting) { // 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 @@ -317,13 +309,6 @@ private static boolean equal(TypeMirror a, TypeMirror b, Set v EqualVisitorParam p = new EqualVisitorParam(); p.type = b; p.visiting = visiting; - if (INTERSECTION_TYPE != null) { - if (isIntersectionType(a)) { - return equalIntersectionTypes(a, b, visiting); - } else if (isIntersectionType(b)) { - return false; - } - } return (a == b) || (a != null && b != null && a.accept(EqualVisitor.INSTANCE, p)); } @@ -343,34 +328,6 @@ private static TypeMirror enclosingType(DeclaredType t) { return enclosing; } - private static boolean isIntersectionType(TypeMirror t) { - return t != null && t.getKind().name().equals("INTERSECTION"); - } - - // The representation of an intersection type, as in >, changed - // between Java 7 and Java 8. In Java 7 it was modeled as a fake DeclaredType, and our logic - // for DeclaredType does the right thing. In Java 8 it is modeled as a new type IntersectionType. - // In order for our code to run on Java 7 (and Java 6) we can't even mention IntersectionType, - // so we can't override visitIntersectionType(IntersectionType). Instead, we discover through - // reflection whether IntersectionType exists, and if it does we extract the bounds of the - // intersection ((Number, Comparable) in the example) and compare them directly. - @SuppressWarnings("unchecked") - private static boolean equalIntersectionTypes( - TypeMirror a, TypeMirror b, Set visiting) { - if (!isIntersectionType(b)) { - return false; - } - List aBounds; - List bBounds; - try { - aBounds = (List) GET_BOUNDS.invoke(a); - bBounds = (List) GET_BOUNDS.invoke(b); - } catch (Exception e) { - throw Throwables.propagate(e); - } - return equalLists(aBounds, bBounds, visiting); - } - private static boolean equalLists( List a, List b, Set visiting) { int size = a.size(); @@ -595,8 +552,8 @@ public static ImmutableSet asTypeElements(Iterable { + private static final IntersectionTypeVisitor INSTANCE = new IntersectionTypeVisitor(); + + IntersectionTypeVisitor() { + super("intersection type"); + } + + @Override + public IntersectionType visitIntersection(IntersectionType type, Void ignore) { + return type; + } + } + /** * Returns a {@link NoType} if the {@link TypeMirror} represents an non-type such as void, or * package, etc. or throws an {@link IllegalArgumentException}. @@ -742,7 +720,7 @@ public PrimitiveType visitPrimitive(PrimitiveType type, Void ignore) { } // - // visitUnionType would go here, but it is a 1.7 API. + // visitUnionType would go here, but isn't relevant for annotation processors // /**