Skip to content

Commit c16ef66

Browse files
committed
Stop using reflection now that we require javac8.
And also add asIntersection() since it is now available. RELNOTES=Added `MoreTypes.asIntersection()` ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=237120983
1 parent d8e2654 commit c16ef66

File tree

1 file changed

+36
-58
lines changed

1 file changed

+36
-58
lines changed

common/src/main/java/com/google/auto/common/MoreTypes.java

Lines changed: 36 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,17 @@
2121
import static javax.lang.model.type.TypeKind.ARRAY;
2222
import static javax.lang.model.type.TypeKind.DECLARED;
2323
import static javax.lang.model.type.TypeKind.EXECUTABLE;
24+
import static javax.lang.model.type.TypeKind.INTERSECTION;
2425
import static javax.lang.model.type.TypeKind.TYPEVAR;
2526
import static javax.lang.model.type.TypeKind.WILDCARD;
2627

2728
import com.google.common.base.Equivalence;
2829
import com.google.common.base.Objects;
2930
import com.google.common.base.Optional;
3031
import com.google.common.base.Predicate;
31-
import com.google.common.base.Throwables;
3232
import com.google.common.collect.FluentIterable;
3333
import com.google.common.collect.ImmutableList;
3434
import com.google.common.collect.ImmutableSet;
35-
import java.lang.reflect.Method;
3635
import java.util.HashSet;
3736
import java.util.Iterator;
3837
import java.util.List;
@@ -48,6 +47,7 @@
4847
import javax.lang.model.type.DeclaredType;
4948
import javax.lang.model.type.ErrorType;
5049
import javax.lang.model.type.ExecutableType;
50+
import javax.lang.model.type.IntersectionType;
5151
import javax.lang.model.type.NoType;
5252
import javax.lang.model.type.NullType;
5353
import javax.lang.model.type.PrimitiveType;
@@ -224,6 +224,15 @@ && equalLists(a.getThrownTypes(), b.getThrownTypes(), p.visiting)
224224
return false;
225225
}
226226

227+
@Override
228+
public Boolean visitIntersection(IntersectionType a, EqualVisitorParam p) {
229+
if (p.type.getKind().equals(INTERSECTION)) {
230+
IntersectionType b = (IntersectionType) p.type;
231+
return equalLists(a.getBounds(), b.getBounds(), p.visiting);
232+
}
233+
return false;
234+
}
235+
227236
@Override
228237
public Boolean visitTypeVariable(TypeVariable a, EqualVisitorParam p) {
229238
if (p.type.getKind().equals(TYPEVAR)) {
@@ -286,23 +295,6 @@ private Set<ComparedElements> visitingSetPlus(
286295
}
287296
}
288297

289-
private static final Class<?> INTERSECTION_TYPE;
290-
private static final Method GET_BOUNDS;
291-
292-
static {
293-
Class<?> c;
294-
Method m;
295-
try {
296-
c = Class.forName("javax.lang.model.type.IntersectionType");
297-
m = c.getMethod("getBounds");
298-
} catch (Exception e) {
299-
c = null;
300-
m = null;
301-
}
302-
INTERSECTION_TYPE = c;
303-
GET_BOUNDS = m;
304-
}
305-
306298
private static boolean equal(TypeMirror a, TypeMirror b, Set<ComparedElements> visiting) {
307299
// TypeMirror.equals is not guaranteed to return true for types that are equal, but we can
308300
// 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<ComparedElements> v
317309
EqualVisitorParam p = new EqualVisitorParam();
318310
p.type = b;
319311
p.visiting = visiting;
320-
if (INTERSECTION_TYPE != null) {
321-
if (isIntersectionType(a)) {
322-
return equalIntersectionTypes(a, b, visiting);
323-
} else if (isIntersectionType(b)) {
324-
return false;
325-
}
326-
}
327312
return (a == b) || (a != null && b != null && a.accept(EqualVisitor.INSTANCE, p));
328313
}
329314

@@ -343,34 +328,6 @@ private static TypeMirror enclosingType(DeclaredType t) {
343328
return enclosing;
344329
}
345330

346-
private static boolean isIntersectionType(TypeMirror t) {
347-
return t != null && t.getKind().name().equals("INTERSECTION");
348-
}
349-
350-
// The representation of an intersection type, as in <T extends Number & Comparable<T>>, changed
351-
// between Java 7 and Java 8. In Java 7 it was modeled as a fake DeclaredType, and our logic
352-
// for DeclaredType does the right thing. In Java 8 it is modeled as a new type IntersectionType.
353-
// In order for our code to run on Java 7 (and Java 6) we can't even mention IntersectionType,
354-
// so we can't override visitIntersectionType(IntersectionType). Instead, we discover through
355-
// reflection whether IntersectionType exists, and if it does we extract the bounds of the
356-
// intersection ((Number, Comparable<T>) in the example) and compare them directly.
357-
@SuppressWarnings("unchecked")
358-
private static boolean equalIntersectionTypes(
359-
TypeMirror a, TypeMirror b, Set<ComparedElements> visiting) {
360-
if (!isIntersectionType(b)) {
361-
return false;
362-
}
363-
List<? extends TypeMirror> aBounds;
364-
List<? extends TypeMirror> bBounds;
365-
try {
366-
aBounds = (List<? extends TypeMirror>) GET_BOUNDS.invoke(a);
367-
bBounds = (List<? extends TypeMirror>) GET_BOUNDS.invoke(b);
368-
} catch (Exception e) {
369-
throw Throwables.propagate(e);
370-
}
371-
return equalLists(aBounds, bBounds, visiting);
372-
}
373-
374331
private static boolean equalLists(
375332
List<? extends TypeMirror> a, List<? extends TypeMirror> b, Set<ComparedElements> visiting) {
376333
int size = a.size();
@@ -595,8 +552,8 @@ public static ImmutableSet<TypeElement> asTypeElements(Iterable<? extends TypeMi
595552
}
596553

597554
/**
598-
* Returns a {@link ArrayType} if the {@link TypeMirror} represents a primitive array or throws an
599-
* {@link IllegalArgumentException}.
555+
* Returns a {@link ArrayType} if the {@link TypeMirror} represents an array or throws an {@link
556+
* IllegalArgumentException}.
600557
*/
601558
public static ArrayType asArray(TypeMirror maybeArrayType) {
602559
return maybeArrayType.accept(ArrayTypeVisitor.INSTANCE, null);
@@ -606,7 +563,7 @@ private static final class ArrayTypeVisitor extends CastingTypeVisitor<ArrayType
606563
private static final ArrayTypeVisitor INSTANCE = new ArrayTypeVisitor();
607564

608565
ArrayTypeVisitor() {
609-
super("primitive array");
566+
super("array");
610567
}
611568

612569
@Override
@@ -678,6 +635,27 @@ public ExecutableType visitExecutable(ExecutableType type, Void ignore) {
678635
}
679636
}
680637

638+
/**
639+
* Returns an {@link IntersectionType} if the {@link TypeMirror} represents an intersection-type
640+
* or throws an {@link IllegalArgumentException}.
641+
*/
642+
public static IntersectionType asIntersection(TypeMirror maybeIntersectionType) {
643+
return maybeIntersectionType.accept(IntersectionTypeVisitor.INSTANCE, null);
644+
}
645+
646+
private static final class IntersectionTypeVisitor extends CastingTypeVisitor<IntersectionType> {
647+
private static final IntersectionTypeVisitor INSTANCE = new IntersectionTypeVisitor();
648+
649+
IntersectionTypeVisitor() {
650+
super("intersection type");
651+
}
652+
653+
@Override
654+
public IntersectionType visitIntersection(IntersectionType type, Void ignore) {
655+
return type;
656+
}
657+
}
658+
681659
/**
682660
* Returns a {@link NoType} if the {@link TypeMirror} represents an non-type such as void, or
683661
* package, etc. or throws an {@link IllegalArgumentException}.
@@ -742,7 +720,7 @@ public PrimitiveType visitPrimitive(PrimitiveType type, Void ignore) {
742720
}
743721

744722
//
745-
// visitUnionType would go here, but it is a 1.7 API.
723+
// visitUnionType would go here, but isn't relevant for annotation processors
746724
//
747725

748726
/**

0 commit comments

Comments
 (0)