diff --git a/src/main/java/com/squareup/javapoet/TypeVariableName.java b/src/main/java/com/squareup/javapoet/TypeVariableName.java index 910f61f42..e6e8874ba 100644 --- a/src/main/java/com/squareup/javapoet/TypeVariableName.java +++ b/src/main/java/com/squareup/javapoet/TypeVariableName.java @@ -16,17 +16,13 @@ package com.squareup.javapoet; import java.io.IOException; -import java.lang.reflect.Method; import java.lang.reflect.Type; import java.lang.reflect.TypeVariable; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; -import javax.lang.model.element.NestingKind; -import javax.lang.model.element.TypeElement; -import javax.lang.model.type.DeclaredType; -import javax.lang.model.type.TypeKind; +import javax.lang.model.element.TypeParameterElement; import javax.lang.model.type.TypeMirror; import static com.squareup.javapoet.Util.checkArgument; @@ -92,37 +88,13 @@ public static TypeVariableName get(javax.lang.model.type.TypeVariable mirror) { } /** - * Returns a list of type mirrors representing the unpacked bounds of {@code typeVariable}. This - * is made gnarly by the need to unpack Java 8's new IntersectionType with reflection. We don't - * have that type in Java 7, and {@link TypeVariable}'s array of bounds is sufficient anyway. + * Returns a list of type mirrors representing the unpacked bounds of {@code typeVariable}. */ - @SuppressWarnings("unchecked") // Gross things in support of Java 7 and Java 8. private static List typeVariableBounds( javax.lang.model.type.TypeVariable typeVariable) { - TypeMirror upperBound = typeVariable.getUpperBound(); - - // On Java 8, unwrap an intersection type into its component bounds. - if ("INTERSECTION".equals(upperBound.getKind().name())) { - try { - Method method = upperBound.getClass().getMethod("getBounds"); - return (List) method.invoke(upperBound); - } catch (Exception e) { - throw new RuntimeException(e); - } - } - - // On Java 7, intersection types exist but without explicit API. Use a (clumsy) heuristic. - if (upperBound.getKind() == TypeKind.DECLARED) { - TypeElement upperBoundElement = (TypeElement) ((DeclaredType) upperBound).asElement(); - if (upperBoundElement.getNestingKind() == NestingKind.ANONYMOUS) { - List result = new ArrayList<>(); - result.add(upperBoundElement.getSuperclass()); - result.addAll(upperBoundElement.getInterfaces()); - return result; - } - } - - return Collections.singletonList(upperBound); + TypeParameterElement upperBoundElement = + (TypeParameterElement) ((javax.lang.model.type.TypeVariable) typeVariable).asElement(); + return upperBoundElement.getBounds(); } /** Returns type variable equivalent to {@code type}. */