Top Description Fields Constructors Methods
com.squareup.javapoet

public Class TypeName

extends Object
Class Inheritance
Known Direct Subclasses
com.squareup.javapoet.TypeVariableName, com.squareup.javapoet.WildcardTypeName, com.squareup.javapoet.ArrayTypeName, com.squareup.javapoet.ClassName, com.squareup.javapoet.ParameterizedTypeName
Imports
java.io.IOException, java.lang.reflect.GenericArrayType, .ParameterizedType, .Type, .TypeVariable, .WildcardType, java.util.ArrayList, .Arrays, .LinkedHashMap, .List, .Map, javax.lang.model.element.Modifier, .TypeElement, .TypeParameterElement, javax.lang.model.type.ArrayType, .DeclaredType, .ErrorType, .NoType, .PrimitiveType, .TypeKind, .TypeMirror, javax.lang.model.util.SimpleTypeVisitor8

Any type in Java's type system, plus void. This class is an identifier for primitive types like int and raw reference types like String and List. It also identifies composite types like char[] and Set<Long>.

Type names are dumb identifiers only and do not model the values they name. For example, the type name for java.util.List doesn't know about the size() method, the fact that lists are collections, or even that it accepts a single type parameter.

Instances of this class are immutable value objects that implement equals() and hashCode() properly.

Referencing existing types

Primitives and void are constants that you can reference directly: see INT, DOUBLE, and VOID.

In an annotation processor you can get a type name instance for a type mirror by calling get(TypeMirror). In reflection code, you can use get(Type).

Defining new types

Create new reference types like com.example.HelloWorld with ClassName#get(String, String, String...). To build composite types like char[] and Set<Long>, use the factory methods on ArrayTypeName, ParameterizedTypeName, TypeVariableName, and WildcardTypeName.

Field Summary

Modifier and TypeField and Description
public final List<AnnotationSpec>
public static final TypeName
private static final ClassName
private static final ClassName
private static final ClassName
private static final ClassName
private static final ClassName
private static final ClassName
private static final ClassName
private static final ClassName
private static final ClassName
public static final TypeName
private String
cachedString

Lazily-initialized toString of this type name.

public static final TypeName
public static final TypeName
public static final TypeName
public static final TypeName
private final String
keyword

The name of this type if it is a keyword, or null.

public static final TypeName
public static final ClassName
public static final TypeName
public static final TypeName

Constructor Summary

AccessConstructor and Description
private
TypeName(String keyword)

private
TypeName(String keyword, List<AnnotationSpec> annotations)

pack-priv

Method Summary

Modifier and TypeMethod and Description
public final TypeName
annotated(AnnotationSpec... annotations)

public TypeName
pack-priv static TypeName
arrayComponent(TypeName type)

Returns the array component of type, or null if type is not an array.

pack-priv static ArrayTypeName
asArray(TypeName type)

Returns type as an array, or null if type is not an array.

public TypeName
box()

Returns a boxed type if this is a primitive type (like Integer for int) or void.

protected final List<AnnotationSpec>
pack-priv CodeWriter
pack-priv CodeWriter
public final boolean
equals(Object
the reference object with which to compare.
o
)

Overrides java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.
public static TypeName
get(TypeMirror mirror)

Returns a type name equivalent to mirror.

pack-priv static TypeName
get(TypeMirror mirror, final Map<TypeParameterElement, TypeVariableName> typeVariables)

public static TypeName
get(Type type)

Returns a type name equivalent to type.

pack-priv static TypeName
public final int
hashCode()

Overrides java.lang.Object.hashCode.

Returns a hash code value for the object.
public boolean
public boolean
isBoxedPrimitive()

Returns true if this is a boxed primitive type like Integer.

public boolean
isPrimitive()

Returns true if this is a primitive type like int.

pack-priv static List<TypeName>
list(Type[] types)

Converts an array of types to a list of type names.

pack-priv static List<TypeName>
list(Type[] types, Map<Type, TypeVariableName> map)

public final String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.
public TypeName
unbox()

Returns an unboxed type if this is a boxed primitive type (like int for Integer) or Void.

public TypeName
Inherited from java.lang.Object:
clonefinalizegetClassnotifynotifyAllwaitwaitwait

Field Detail

annotationsback to summary
public final List<AnnotationSpec> annotations
BOOLEANback to summary
public static final TypeName BOOLEAN
BOXED_BOOLEANback to summary
private static final ClassName BOXED_BOOLEAN
BOXED_BYTEback to summary
private static final ClassName BOXED_BYTE
BOXED_CHARback to summary
private static final ClassName BOXED_CHAR
BOXED_DOUBLEback to summary
private static final ClassName BOXED_DOUBLE
BOXED_FLOATback to summary
private static final ClassName BOXED_FLOAT
BOXED_INTback to summary
private static final ClassName BOXED_INT
BOXED_LONGback to summary
private static final ClassName BOXED_LONG
BOXED_SHORTback to summary
private static final ClassName BOXED_SHORT
BOXED_VOIDback to summary
private static final ClassName BOXED_VOID
BYTEback to summary
public static final TypeName BYTE
cachedStringback to summary
private String cachedString

Lazily-initialized toString of this type name.

CHARback to summary
public static final TypeName CHAR
DOUBLEback to summary
public static final TypeName DOUBLE
FLOATback to summary
public static final TypeName FLOAT
INTback to summary
public static final TypeName INT
keywordback to summary
private final String keyword

The name of this type if it is a keyword, or null.

LONGback to summary
public static final TypeName LONG
OBJECTback to summary
public static final ClassName OBJECT
SHORTback to summary
public static final TypeName SHORT
VOIDback to summary
public static final TypeName VOID

Constructor Detail

TypeNameback to summary
private TypeName(String keyword)
TypeNameback to summary
private TypeName(String keyword, List<AnnotationSpec> annotations)
TypeNameback to summary
pack-priv TypeName(List<AnnotationSpec> annotations)

Method Detail

annotatedback to summary
public final TypeName annotated(AnnotationSpec... annotations)
annotatedback to summary
public TypeName annotated(List<AnnotationSpec> annotations)
arrayComponentback to summary
pack-priv static TypeName arrayComponent(TypeName type)

Returns the array component of type, or null if type is not an array.

asArrayback to summary
pack-priv static ArrayTypeName asArray(TypeName type)

Returns type as an array, or null if type is not an array.

boxback to summary
public TypeName box()

Returns a boxed type if this is a primitive type (like Integer for int) or void. Returns this type if boxing doesn't apply.

concatAnnotationsback to summary
protected final List<AnnotationSpec> concatAnnotations(List<AnnotationSpec> annotations)
emitback to summary
pack-priv CodeWriter emit(CodeWriter out) throws IOException
emitAnnotationsback to summary
pack-priv CodeWriter emitAnnotations(CodeWriter out) throws IOException
equalsback to summary
public final boolean equals(Object o)

Overrides java.lang.Object.equals.

Doc from java.lang.Object.equals.

Indicates whether some other object is "equal to" this one.

The equals method implements an equivalence relation on non-null object references:

  • It is reflexive: for any non-null reference value x, x.equals(x) should return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.

Parameters
o:Object

the reference object with which to compare.

Returns:boolean

true if this object is the same as the obj argument; false otherwise.

Annotations
@Override
getback to summary
public static TypeName get(TypeMirror mirror)

Returns a type name equivalent to mirror.

getback to summary
pack-priv static TypeName get(TypeMirror mirror, final Map<TypeParameterElement, TypeVariableName> typeVariables)
getback to summary
public static TypeName get(Type type)

Returns a type name equivalent to type.

getback to summary
pack-priv static TypeName get(Type type, Map<Type, TypeVariableName> map)
hashCodeback to summary
public final int hashCode()

Overrides java.lang.Object.hashCode.

Doc from java.lang.Object.hashCode.

Returns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided by java.util.HashMap.

The general contract of hashCode is:

  • Whenever it is invoked on the same object more than once during an execution of a Java application, the hashCode method must consistently return the same integer, provided no information used in equals comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application.
  • If two objects are equal according to the equals method, then calling the hashCode method on each of the two objects must produce the same integer result.
  • It is not required that if two objects are unequal according to the equals method, then calling the hashCode method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
Returns:int

a hash code value for this object.

Annotations
@Override
isAnnotatedback to summary
public boolean isAnnotated()
isBoxedPrimitiveback to summary
public boolean isBoxedPrimitive()

Returns true if this is a boxed primitive type like Integer. Returns false for all other types types including unboxed primitives and java.lang.Void.

isPrimitiveback to summary
public boolean isPrimitive()

Returns true if this is a primitive type like int. Returns false for all other types types including boxed primitives and void.

listback to summary
pack-priv static List<TypeName> list(Type[] types)

Converts an array of types to a list of type names.

listback to summary
pack-priv static List<TypeName> list(Type[] types, Map<Type, TypeVariableName> map)
toStringback to summary
public final String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object.

Returns:String

a string representation of the object.

Annotations
@Override
unboxback to summary
public TypeName unbox()

Returns an unboxed type if this is a boxed primitive type (like int for Integer) or Void. Returns this type if it is already unboxed.

Exceptions
UnsupportedOperationException:
if this type isn't eligible for unboxing.
withoutAnnotationsback to summary
public TypeName withoutAnnotations()