Class TypeName
- java.lang.Object
-
- com.squareup.javapoet.TypeName
-
- Direct Known Subclasses:
ArrayTypeName
,ClassName
,ParameterizedTypeName
,TypeVariableName
,WildcardTypeName
public class TypeName extends Object
Any type in Java's type system, plusvoid
. This class is an identifier for primitive types likeint
and raw reference types likeString
andList
. It also identifies composite types likechar[]
andSet<Long>
.Type names are dumb identifiers only and do not model the values they name. For example, the type name for
java.lang.List
doesn't know about thesize()
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()
andhashCode()
properly.Referencing existing types
Primitives and void are constants that you can reference directly: see
INT
,DOUBLE
, andVOID
.In an annotation processor you can get a type name instance for a type mirror by calling
get(TypeMirror)
. In reflection code, you can useget(Type)
.Defining new types
Create new reference types like
com.example.HelloWorld
withClassName.get(String, String, String...)
. To build composite types likechar[]
andSet<Long>
, use the factory methods onArrayTypeName
,ParameterizedTypeName
,TypeVariableName
, andWildcardTypeName
.
-
-
Field Summary
Fields Modifier and Type Field Description List<AnnotationSpec>
annotations
static TypeName
BOOLEAN
static TypeName
BYTE
static TypeName
CHAR
static TypeName
DOUBLE
static TypeName
FLOAT
static TypeName
INT
static TypeName
LONG
static ClassName
OBJECT
static TypeName
SHORT
static TypeName
VOID
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description TypeName
annotated(AnnotationSpec... annotations)
TypeName
annotated(List<AnnotationSpec> annotations)
TypeName
box()
Returns a boxed type if this is a primitive type (likeInteger
forint
) orvoid
.protected List<AnnotationSpec>
concatAnnotations(List<AnnotationSpec> annotations)
boolean
equals(Object o)
static TypeName
get(Type type)
Returns a type name equivalent totype
.static TypeName
get(TypeMirror mirror)
Returns a type name equivalent tomirror
.int
hashCode()
boolean
isAnnotated()
boolean
isBoxedPrimitive()
Returns true if this is a boxed primitive type likeInteger
.boolean
isPrimitive()
Returns true if this is a primitive type likeint
.String
toString()
TypeName
unbox()
Returns an unboxed type if this is a boxed primitive type (likeint
forInteger
) orVoid
.TypeName
withoutAnnotations()
-
-
-
Field Detail
-
VOID
public static final TypeName VOID
-
BOOLEAN
public static final TypeName BOOLEAN
-
BYTE
public static final TypeName BYTE
-
SHORT
public static final TypeName SHORT
-
INT
public static final TypeName INT
-
LONG
public static final TypeName LONG
-
CHAR
public static final TypeName CHAR
-
FLOAT
public static final TypeName FLOAT
-
DOUBLE
public static final TypeName DOUBLE
-
OBJECT
public static final ClassName OBJECT
-
annotations
public final List<AnnotationSpec> annotations
-
-
Method Detail
-
annotated
public final TypeName annotated(AnnotationSpec... annotations)
-
annotated
public TypeName annotated(List<AnnotationSpec> annotations)
-
withoutAnnotations
public TypeName withoutAnnotations()
-
concatAnnotations
protected final List<AnnotationSpec> concatAnnotations(List<AnnotationSpec> annotations)
-
isAnnotated
public boolean isAnnotated()
-
isPrimitive
public boolean isPrimitive()
Returns true if this is a primitive type likeint
. Returns false for all other types types including boxed primitives andvoid
.
-
isBoxedPrimitive
public boolean isBoxedPrimitive()
Returns true if this is a boxed primitive type likeInteger
. Returns false for all other types types including unboxed primitives andjava.lang.Void
.
-
box
public TypeName box()
Returns a boxed type if this is a primitive type (likeInteger
forint
) orvoid
. Returns this type if boxing doesn't apply.
-
unbox
public TypeName unbox()
Returns an unboxed type if this is a boxed primitive type (likeint
forInteger
) orVoid
. Returns this type if it is already unboxed.- Throws:
UnsupportedOperationException
- if this type isn't eligible for unboxing.
-
get
public static TypeName get(TypeMirror mirror)
Returns a type name equivalent tomirror
.
-
-