Skip to content
This repository has been archived by the owner on Oct 10, 2024. It is now read-only.

Fix bug when using javapoet with Eclipse compiler #303

Closed
wants to merge 6 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 5 additions & 33 deletions src/main/java/com/squareup/javapoet/TypeVariableName.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<? extends TypeMirror> 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<? extends TypeMirror>) 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<TypeMirror> 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();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need the first cast actually, and the variable should be renamed as it's not about the upper bounds. This can be simplified to:

return typeVariable.asElement().getBounds();

…and probably inlined then.

return upperBoundElement.getBounds();
}

/** Returns type variable equivalent to {@code type}. */
Expand Down