Class TypeName
- java.lang.Object
-
- com.squareup.javapoet.TypeName
-
- Direct Known Subclasses:
ArrayTypeName,ClassName,ParameterizedTypeName,TypeVariableName,WildcardTypeName
public class TypeName extends java.lang.ObjectAny type in Java's type system, plusvoid. This class is an identifier for primitive types likeintand raw reference types likeStringandList. 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.util.Listdoesn'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.HelloWorldwithClassName.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>annotationsstatic TypeNameBOOLEANprivate static ClassNameBOXED_BOOLEANprivate static ClassNameBOXED_BYTEprivate static ClassNameBOXED_CHARprivate static ClassNameBOXED_DOUBLEprivate static ClassNameBOXED_FLOATprivate static ClassNameBOXED_INTprivate static ClassNameBOXED_LONGprivate static ClassNameBOXED_SHORTprivate static ClassNameBOXED_VOIDstatic TypeNameBYTEprivate java.lang.StringcachedStringLazily-initialized toString of this type name.static TypeNameCHARstatic TypeNameDOUBLEstatic TypeNameFLOATstatic TypeNameINTprivate java.lang.StringkeywordThe name of this type if it is a keyword, or null.static TypeNameLONGstatic ClassNameOBJECTstatic TypeNameSHORTstatic TypeNameVOID
-
Constructor Summary
Constructors Modifier Constructor Description privateTypeName(java.lang.String keyword)privateTypeName(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 TypeNameannotated(AnnotationSpec... annotations)TypeNameannotated(java.util.List<AnnotationSpec> annotations)(package private) static TypeNamearrayComponent(TypeName type)Returns the array component oftype, or null iftypeis not an array.(package private) static ArrayTypeNameasArray(TypeName type)Returnstypeas an array, or null iftypeis not an array.TypeNamebox()Returns a boxed type if this is a primitive type (likeIntegerforint) orvoid.protected java.util.List<AnnotationSpec>concatAnnotations(java.util.List<AnnotationSpec> annotations)(package private) CodeWriteremit(CodeWriter out)(package private) CodeWriteremitAnnotations(CodeWriter out)booleanequals(java.lang.Object o)static TypeNameget(java.lang.reflect.Type type)Returns a type name equivalent totype.(package private) static TypeNameget(java.lang.reflect.Type type, java.util.Map<java.lang.reflect.Type,TypeVariableName> map)static TypeNameget(javax.lang.model.type.TypeMirror mirror)Returns a type name equivalent tomirror.(package private) static TypeNameget(javax.lang.model.type.TypeMirror mirror, java.util.Map<javax.lang.model.element.TypeParameterElement,TypeVariableName> typeVariables)inthashCode()booleanisAnnotated()booleanisBoxedPrimitive()Returns true if this is a boxed primitive type likeInteger.booleanisPrimitive()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.StringtoString()TypeNameunbox()Returns an unboxed type if this is a boxed primitive type (likeintforInteger) orVoid.TypeNamewithoutAnnotations()
-
-
-
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 (likeIntegerforint) orvoid. Returns this type if boxing doesn't apply.
-
unbox
public TypeName unbox()
Returns an unboxed type if this is a boxed primitive type (likeintforInteger) 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:
equalsin classjava.lang.Object
-
hashCode
public final int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
toString
public final java.lang.String toString()
- Overrides:
toStringin 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)
-
arrayComponent
static TypeName arrayComponent(TypeName type)
Returns the array component oftype, or null iftypeis not an array.
-
asArray
static ArrayTypeName asArray(TypeName type)
Returnstypeas an array, or null iftypeis not an array.
-
-