use of javax.lang.model.element.TypeParameterElement in project javapoet by square.
the class TypeVariableName method get.
/**
* Make a TypeVariableName for the given TypeMirror. This form is used internally to avoid
* infinite recursion in cases like {@code Enum<E extends Enum<E>>}. When we encounter such a
* thing, we will make a TypeVariableName without bounds and add that to the {@code typeVariables}
* map before looking up the bounds. Then if we encounter this TypeVariable again while
* constructing the bounds, we can just return it from the map. And, the code that put the entry
* in {@code variables} will make sure that the bounds are filled in before returning.
*/
static TypeVariableName get(TypeVariable mirror, Map<TypeParameterElement, TypeVariableName> typeVariables) {
TypeParameterElement element = (TypeParameterElement) mirror.asElement();
TypeVariableName typeVariableName = typeVariables.get(element);
if (typeVariableName == null) {
// Since the bounds field is public, we need to make it an unmodifiableList. But we control
// the List that that wraps, which means we can change it before returning.
List<TypeName> bounds = new ArrayList<>();
List<TypeName> visibleBounds = Collections.unmodifiableList(bounds);
typeVariableName = new TypeVariableName(element.getSimpleName().toString(), visibleBounds);
typeVariables.put(element, typeVariableName);
for (TypeMirror typeMirror : element.getBounds()) {
bounds.add(TypeName.get(typeMirror, typeVariables));
}
bounds.remove(OBJECT);
}
return typeVariableName;
}
use of javax.lang.model.element.TypeParameterElement in project auto by google.
the class MoreTypesTest method asElement.
@Test
public void asElement() {
Elements elements = compilationRule.getElements();
TypeElement stringElement = elements.getTypeElement("java.lang.String");
assertThat(MoreTypes.asElement(stringElement.asType())).isEqualTo(stringElement);
TypeParameterElement setParameterElement = Iterables.getOnlyElement(compilationRule.getElements().getTypeElement("java.util.Set").getTypeParameters());
assertThat(MoreTypes.asElement(setParameterElement.asType())).isEqualTo(setParameterElement);
// we don't test error types because those are very hard to get predictably
}
use of javax.lang.model.element.TypeParameterElement in project auto by google.
the class TypeSimplifierTest method testTypeMirrorSetWildcardCapture.
@Test
public void testTypeMirrorSetWildcardCapture() {
// TODO(emcmanus): this test should really be in MoreTypesTest.
// This test checks the assumption made by MoreTypes that you can find the
// upper bounds of a TypeVariable tv like this:
// TypeParameterElement tpe = (TypeParameterElement) tv.asElement();
// List<? extends TypeMirror> bounds = tpe.getBounds();
// There was some doubt as to whether this would be true in exotic cases involving
// wildcard capture, but apparently it is.
// The methods one and two here have identical signatures:
// abstract <T extends V, U extends T, V> Map<? extends T, ? super U> name();
// Their return types should be considered equal by TypeMirrorSet. The capture of
// each return type is different from the original return type, but the two captures
// should compare equal to each other. We also add various other types like ? super U
// to the set to ensure that the implied calls to the equals and hashCode visitors
// don't cause a ClassCastException for TypeParameterElement.
TypeElement wildcardsElement = typeElementOf(Wildcards.class);
List<? extends ExecutableElement> methods = ElementFilter.methodsIn(wildcardsElement.getEnclosedElements());
assertThat(methods).hasSize(2);
ExecutableElement one = methods.get(0);
ExecutableElement two = methods.get(1);
assertThat(one.getSimpleName().toString()).isEqualTo("one");
assertThat(two.getSimpleName().toString()).isEqualTo("two");
TypeMirrorSet typeMirrorSet = new TypeMirrorSet();
assertThat(typeMirrorSet.add(one.getReturnType())).isTrue();
assertThat(typeMirrorSet.add(two.getReturnType())).isFalse();
DeclaredType captureOne = (DeclaredType) typeUtils.capture(one.getReturnType());
assertThat(typeMirrorSet.add(captureOne)).isTrue();
DeclaredType captureTwo = (DeclaredType) typeUtils.capture(two.getReturnType());
assertThat(typeMirrorSet.add(captureTwo)).isFalse();
// Reminder: captureOne is Map<?#123 extends T, ?#456 super U>
TypeVariable extendsT = (TypeVariable) captureOne.getTypeArguments().get(0);
assertThat(typeMirrorSet.add(extendsT)).isTrue();
// NoType
assertThat(typeMirrorSet.add(extendsT.getLowerBound())).isTrue();
for (TypeMirror bound : ((TypeParameterElement) extendsT.asElement()).getBounds()) {
assertThat(typeMirrorSet.add(bound)).isTrue();
}
TypeVariable superU = (TypeVariable) captureOne.getTypeArguments().get(1);
assertThat(typeMirrorSet.add(superU)).isTrue();
assertThat(typeMirrorSet.add(superU.getLowerBound())).isTrue();
}
use of javax.lang.model.element.TypeParameterElement in project j2objc by google.
the class JdtExecutableElement method getTypeParameters.
@Override
public List<? extends TypeParameterElement> getTypeParameters() {
List<TypeParameterElement> typeParams = new ArrayList<>();
for (ITypeBinding tp : ((IMethodBinding) binding).getTypeParameters()) {
Element tpe = BindingConverter.getElement(tp);
assert tpe instanceof TypeParameterElement;
typeParams.add((TypeParameterElement) tpe);
}
return typeParams;
}
use of javax.lang.model.element.TypeParameterElement in project j2objc by google.
the class JdtTypeElement method getTypeParameters.
@Override
public List<? extends TypeParameterElement> getTypeParameters() {
List<TypeParameterElement> typeParams = new ArrayList<>();
for (ITypeBinding typeParam : ((ITypeBinding) binding).getTypeParameters()) {
TypeParameterElement tpe = (TypeParameterElement) BindingConverter.getElement(typeParam);
typeParams.add(tpe);
}
return typeParams;
}
Aggregations