Class TypeName
- java.lang.Object
-
- com.squareup.javapoet.TypeName
-
- Direct Known Subclasses:
ArrayTypeName
,ClassName
,ParameterizedTypeName
,TypeVariableName
,WildcardTypeName
public class TypeName extends java.lang.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 java.util.List<AnnotationSpec>
annotations
static TypeName
BOOLEAN
private static ClassName
BOXED_BOOLEAN
private static ClassName
BOXED_BYTE
private static ClassName
BOXED_CHAR
private static ClassName
BOXED_DOUBLE
private static ClassName
BOXED_FLOAT
private static ClassName
BOXED_INT
private static ClassName
BOXED_LONG
private static ClassName
BOXED_SHORT
private static ClassName
BOXED_VOID
static TypeName
BYTE
private java.lang.String
cachedString
Lazily-initialized toString of this type name.static TypeName
CHAR
static TypeName
DOUBLE
static TypeName
FLOAT
static TypeName
INT
private java.lang.String
keyword
The name of this type if it is a keyword, or null.static TypeName
LONG
static ClassName
OBJECT
static TypeName
SHORT
static TypeName
VOID
-
Constructor Summary
Constructors Modifier Constructor Description private
TypeName(java.lang.String keyword)
private
TypeName(java.lang.String keyword, java.util.List<AnnotationSpec> annotations)
(package private)
TypeName(java.util.List<AnnotationSpec> annotations)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description TypeName
annotated(AnnotationSpec... annotations)
TypeName
annotated(java.util.List<AnnotationSpec> annotations)
(package private) static TypeName
arrayComponent(TypeName type)
Returns the array component oftype
, or null iftype
is not an array.TypeName
box()
Returns a boxed type if this is a primitive type (likeInteger
forint
) orvoid
.protected java.util.List<AnnotationSpec>
concatAnnotations(java.util.List<AnnotationSpec> annotations)
(package private) CodeWriter
emit(CodeWriter out)
(package private) CodeWriter
emitAnnotations(CodeWriter out)
boolean
equals(java.lang.Object o)
static TypeName
get(java.lang.reflect.Type type)
Returns a type name equivalent totype
.(package private) static TypeName
get(java.lang.reflect.Type type, java.util.Map<java.lang.reflect.Type,TypeVariableName> map)
static TypeName
get(javax.lang.model.type.TypeMirror mirror)
Returns a type name equivalent tomirror
.(package private) static TypeName
get(javax.lang.model.type.TypeMirror mirror, java.util.Map<javax.lang.model.element.TypeParameterElement,TypeVariableName> typeVariables)
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
.(package private) static java.util.List<TypeName>
list(java.lang.reflect.Type[] types)
Converts an array of types to a list of type names.(package private) static java.util.List<TypeName>
list(java.lang.reflect.Type[] types, java.util.Map<java.lang.reflect.Type,TypeVariableName> map)
java.lang.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
-
BOXED_VOID
private static final ClassName BOXED_VOID
-
BOXED_BOOLEAN
private static final ClassName BOXED_BOOLEAN
-
BOXED_BYTE
private static final ClassName BOXED_BYTE
-
BOXED_SHORT
private static final ClassName BOXED_SHORT
-
BOXED_INT
private static final ClassName BOXED_INT
-
BOXED_LONG
private static final ClassName BOXED_LONG
-
BOXED_CHAR
private static final ClassName BOXED_CHAR
-
BOXED_FLOAT
private static final ClassName BOXED_FLOAT
-
BOXED_DOUBLE
private static final ClassName BOXED_DOUBLE
-
keyword
private final java.lang.String keyword
The name of this type if it is a keyword, or null.
-
annotations
public final java.util.List<AnnotationSpec> annotations
-
cachedString
private java.lang.String cachedString
Lazily-initialized toString of this type name.
-
-
Constructor Detail
-
TypeName
private TypeName(java.lang.String keyword)
-
TypeName
private TypeName(java.lang.String keyword, java.util.List<AnnotationSpec> annotations)
-
TypeName
TypeName(java.util.List<AnnotationSpec> annotations)
-
-
Method Detail
-
annotated
public final TypeName annotated(AnnotationSpec... annotations)
-
annotated
public TypeName annotated(java.util.List<AnnotationSpec> annotations)
-
withoutAnnotations
public TypeName withoutAnnotations()
-
concatAnnotations
protected final java.util.List<AnnotationSpec> concatAnnotations(java.util.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:
java.lang.UnsupportedOperationException
- if this type isn't eligible for unboxing.
-
equals
public final boolean equals(java.lang.Object o)
- Overrides:
equals
in classjava.lang.Object
-
hashCode
public final int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
toString
public final java.lang.String toString()
- Overrides:
toString
in classjava.lang.Object
-
emit
CodeWriter emit(CodeWriter out) throws java.io.IOException
- Throws:
java.io.IOException
-
emitAnnotations
CodeWriter emitAnnotations(CodeWriter out) throws java.io.IOException
- Throws:
java.io.IOException
-
get
public static TypeName get(javax.lang.model.type.TypeMirror mirror)
Returns a type name equivalent tomirror
.
-
get
static TypeName get(javax.lang.model.type.TypeMirror mirror, java.util.Map<javax.lang.model.element.TypeParameterElement,TypeVariableName> typeVariables)
-
get
public static TypeName get(java.lang.reflect.Type type)
Returns a type name equivalent totype
.
-
get
static TypeName get(java.lang.reflect.Type type, java.util.Map<java.lang.reflect.Type,TypeVariableName> map)
-
list
static java.util.List<TypeName> list(java.lang.reflect.Type[] types)
Converts an array of types to a list of type names.
-
list
static java.util.List<TypeName> list(java.lang.reflect.Type[] types, java.util.Map<java.lang.reflect.Type,TypeVariableName> map)
-
-