@Test public void getBasicTypeMirror() { assertThat(TypeName.get(getMirror(Object.class))).isEqualTo(ClassName.get(Object.class)); assertThat(TypeName.get(getMirror(Charset.class))).isEqualTo(ClassName.get(Charset.class)); assertThat(TypeName.get(getMirror(AbstractTypesTest.class))) .isEqualTo(ClassName.get(AbstractTypesTest.class)); }
@Test public void getParameterizedTypeMirror() { DeclaredType setType = getTypes().getDeclaredType(getElement(Set.class), getMirror(Object.class)); assertThat(TypeName.get(setType)) .isEqualTo(ParameterizedTypeName.get(ClassName.get(Set.class), ClassName.OBJECT)); }
@Test public void getNullTypeMirror() { try { TypeName.get(getTypes().getNullType()); fail(); } catch (IllegalArgumentException expected) { } }
@Test public void getTypeVariableTypeMirror() { List<? extends TypeParameterElement> typeVariables = getElement(Parameterized.class).getTypeParameters(); // Members of converted types use ClassName and not Class<?>. ClassName number = ClassName.get(Number.class); ClassName runnable = ClassName.get(Runnable.class); ClassName serializable = ClassName.get(Serializable.class); assertThat(TypeName.get(typeVariables.get(0).asType())) .isEqualTo(TypeVariableName.get("Simple")); assertThat(TypeName.get(typeVariables.get(1).asType())) .isEqualTo(TypeVariableName.get("ExtendsClass", number)); assertThat(TypeName.get(typeVariables.get(2).asType())) .isEqualTo(TypeVariableName.get("ExtendsInterface", runnable)); assertThat(TypeName.get(typeVariables.get(3).asType())) .isEqualTo(TypeVariableName.get("ExtendsTypeVariable", TypeVariableName.get("Simple"))); assertThat(TypeName.get(typeVariables.get(4).asType())) .isEqualTo(TypeVariableName.get("Intersection", number, runnable)); assertThat(TypeName.get(typeVariables.get(5).asType())) .isEqualTo(TypeVariableName.get("IntersectionOfInterfaces", runnable, serializable)); assertThat(((TypeVariableName) TypeName.get(typeVariables.get(4).asType())).bounds) .containsExactly(number, runnable); }
@Test public void wildcardMirrorSuperType() throws Exception { Types types = getTypes(); Elements elements = getElements(); TypeMirror string = elements.getTypeElement(String.class.getName()).asType(); WildcardType wildcard = types.getWildcardType(null, string); TypeName type = TypeName.get(wildcard); assertThat(type.toString()).isEqualTo("? super java.lang.String"); }
@Test public void wildcardMirrorExtendsType() throws Exception { Types types = getTypes(); Elements elements = getElements(); TypeMirror charSequence = elements.getTypeElement(CharSequence.class.getName()).asType(); WildcardType wildcard = types.getWildcardType(charSequence, null); TypeName type = TypeName.get(wildcard); assertThat(type.toString()).isEqualTo("? extends java.lang.CharSequence"); }
@Test public void getPrimitiveTypeMirror() { assertThat(TypeName.get(getTypes().getPrimitiveType(TypeKind.BOOLEAN))) .isEqualTo(TypeName.BOOLEAN); assertThat(TypeName.get(getTypes().getPrimitiveType(TypeKind.BYTE))).isEqualTo(TypeName.BYTE); assertThat(TypeName.get(getTypes().getPrimitiveType(TypeKind.SHORT))).isEqualTo(TypeName.SHORT); assertThat(TypeName.get(getTypes().getPrimitiveType(TypeKind.INT))).isEqualTo(TypeName.INT); assertThat(TypeName.get(getTypes().getPrimitiveType(TypeKind.LONG))).isEqualTo(TypeName.LONG); assertThat(TypeName.get(getTypes().getPrimitiveType(TypeKind.CHAR))).isEqualTo(TypeName.CHAR); assertThat(TypeName.get(getTypes().getPrimitiveType(TypeKind.FLOAT))).isEqualTo(TypeName.FLOAT); assertThat(TypeName.get(getTypes().getPrimitiveType(TypeKind.DOUBLE))) .isEqualTo(TypeName.DOUBLE); }
@Test public void getTypeVariableTypeMirrorRecursive() { TypeMirror typeMirror = getElement(Recursive.class).asType(); ParameterizedTypeName typeName = (ParameterizedTypeName) TypeName.get(typeMirror); String className = Recursive.class.getCanonicalName(); assertThat(typeName.toString()).isEqualTo(className + "<T>"); TypeVariableName typeVariableName = (TypeVariableName) typeName.typeArguments.get(0); try { typeVariableName.bounds.set(0, null); fail("Expected UnsupportedOperationException"); } catch (UnsupportedOperationException expected) { } assertThat(typeVariableName.toString()).isEqualTo("T"); assertThat(typeVariableName.bounds.toString()) .isEqualTo("[java.util.Map<java.util.List<T>, java.util.Set<T[]>>]"); }
@Test public void getErrorType() { ErrorType errorType = new DeclaredTypeAsErrorType(getTypes().getDeclaredType(getElement(Set.class))); assertThat(TypeName.get(errorType)).isEqualTo(ClassName.get(Set.class)); }
@Test public void wildcardMirrorNoBounds() throws Exception { WildcardType wildcard = getTypes().getWildcardType(null, null); TypeName type = TypeName.get(wildcard); assertThat(type.toString()).isEqualTo("?"); }
@Test public void getVoidTypeMirror() { assertThat(TypeName.get(getTypes().getNoType(TypeKind.VOID))).isEqualTo(TypeName.VOID); }
@Test public void getArrayTypeMirror() { assertThat(TypeName.get(getTypes().getArrayType(getMirror(Object.class)))) .isEqualTo(ArrayTypeName.of(ClassName.OBJECT)); }