TypeNames { public static TypeName forTypeMirror(TypeMirror mirror) { return mirror.accept(new SimpleTypeVisitor6<TypeName, Void>() { @Override protected TypeName defaultAction(TypeMirror e, Void p) { throw new IllegalArgumentException(e.toString()); } @Override public TypeName visitTypeVariable(TypeVariable t, Void p) { return TypeVariableName.fromTypeVariable(t); } @Override public ArrayTypeName visitArray(ArrayType t, Void p) { return new ArrayTypeName(t.getComponentType().accept(this, null)); } @Override public TypeName visitDeclared(DeclaredType t, Void p) { return t.getTypeArguments().isEmpty() ? ClassName.fromTypeElement((TypeElement) t.asElement()) : new ParameterizedTypeName( ClassName.fromTypeElement((TypeElement) t.asElement()), FluentIterable.from(t.getTypeArguments()).transform(FOR_TYPE_MIRROR)); } @Override public PrimitiveName visitPrimitive(PrimitiveType t, Void p) { return PrimitiveName.forTypeMirror(t); } @Override public WildcardName visitWildcard(WildcardType t, Void p) { return WildcardName.forTypeMirror(t); } @Override public NullName visitNull(NullType t, Void p) { return NullName.NULL; } @Override public TypeName visitNoType(NoType t, Void p) { switch (t.getKind()) { case VOID: return VoidName.VOID; case PACKAGE: throw new IllegalArgumentException(); default: throw new IllegalStateException(); } } }, null); } private  TypeNames(); static TypeName forClass(Class<?> clazz); static TypeName forTypeMirror(TypeMirror mirror);  }
@Test public void forTypeMirror_basicTypes() { assertThat(TypeNames.forTypeMirror(getType(Object.class))) .isEqualTo(ClassName.fromClass(Object.class)); assertThat(TypeNames.forTypeMirror(getType(Charset.class))) .isEqualTo(ClassName.fromClass(Charset.class)); assertThat(TypeNames.forTypeMirror(getType(TypeNamesTest.class))) .isEqualTo(ClassName.fromClass(TypeNamesTest.class)); }
@Test public void forTypeMirror_parameterizedType() { DeclaredType setType = compilation.getTypes().getDeclaredType(getElement(Set.class), getType(Object.class)); assertThat(TypeNames.forTypeMirror(setType)) .isEqualTo(ParameterizedTypeName.create(Set.class, ClassName.fromClass(Object.class))); }
@Test public void forTypeMirror_typeVariables() { TypeMirror setType = getType(Set.class); assertThat(TypeNames.forTypeMirror(setType)) .isEqualTo(ParameterizedTypeName.create(Set.class, TypeVariableName.named("E"))); }
@Test public void forTypeMirror_primitive() { assertThat(TypeNames.forTypeMirror(compilation.getTypes().getPrimitiveType(TypeKind.BOOLEAN))) .isEqualTo(PrimitiveName.BOOLEAN); assertThat(TypeNames.forTypeMirror(compilation.getTypes().getPrimitiveType(TypeKind.BYTE))) .isEqualTo(PrimitiveName.BYTE); assertThat(TypeNames.forTypeMirror(compilation.getTypes().getPrimitiveType(TypeKind.SHORT))) .isEqualTo(PrimitiveName.SHORT); assertThat(TypeNames.forTypeMirror(compilation.getTypes().getPrimitiveType(TypeKind.INT))) .isEqualTo(PrimitiveName.INT); assertThat(TypeNames.forTypeMirror(compilation.getTypes().getPrimitiveType(TypeKind.LONG))) .isEqualTo(PrimitiveName.LONG); assertThat(TypeNames.forTypeMirror(compilation.getTypes().getPrimitiveType(TypeKind.CHAR))) .isEqualTo(PrimitiveName.CHAR); assertThat(TypeNames.forTypeMirror(compilation.getTypes().getPrimitiveType(TypeKind.FLOAT))) .isEqualTo(PrimitiveName.FLOAT); assertThat(TypeNames.forTypeMirror(compilation.getTypes().getPrimitiveType(TypeKind.DOUBLE))) .isEqualTo(PrimitiveName.DOUBLE); }
@Test public void forTypeMirror_arrays() { assertThat(TypeNames.forTypeMirror(compilation.getTypes().getArrayType(getType(Object.class)))) .isEqualTo(new ArrayTypeName(ClassName.fromClass(Object.class))); }
@Test public void forTypeMirror_void() { assertThat(TypeNames.forTypeMirror(compilation.getTypes().getNoType(TypeKind.VOID))) .isEqualTo(VoidName.VOID); }
@Test public void forTypeMirror_null() { assertThat(TypeNames.forTypeMirror(compilation.getTypes().getNullType())) .isEqualTo(NullName.NULL); }