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 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.lang.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 List<AnnotationSpec>annotationsstatic TypeNameBOOLEANstatic TypeNameBYTEstatic TypeNameCHARstatic TypeNameDOUBLEstatic TypeNameFLOATstatic TypeNameINTstatic TypeNameLONGstatic ClassNameOBJECTstatic TypeNameSHORTstatic TypeNameVOID
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description TypeNameannotated(AnnotationSpec... annotations)TypeNameannotated(List<AnnotationSpec> annotations)TypeNamebox()Returns a boxed type if this is a primitive type (likeIntegerforint) orvoid.protected List<AnnotationSpec>concatAnnotations(List<AnnotationSpec> annotations)booleanequals(Object o)static TypeNameget(Type type)Returns a type name equivalent totype.static TypeNameget(TypeMirror mirror)Returns a type name equivalent tomirror.inthashCode()booleanisAnnotated()booleanisBoxedPrimitive()Returns true if this is a boxed primitive type likeInteger.booleanisPrimitive()Returns true if this is a primitive type likeint.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
-
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 (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:
UnsupportedOperationException- if this type isn't eligible for unboxing.
-
get
public static TypeName get(TypeMirror mirror)
Returns a type name equivalent tomirror.
-
-