Class TypeName
- Direct Known Subclasses:
ArrayTypeName
,ClassName
,ParameterizedTypeName
,TypeVariableName
,WildcardTypeName
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.lang.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
FieldsModifier and TypeFieldDescriptionfinal List
<AnnotationSpec> 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
static final TypeName
private String
Lazily-initialized toString of this type name.static final TypeName
static final TypeName
static final TypeName
static final TypeName
private final String
The name of this type if it is a keyword, or null.static final TypeName
static final ClassName
static final TypeName
static final TypeName
-
Constructor Summary
ConstructorsModifierConstructorDescriptionprivate
private
TypeName
(String keyword, List<AnnotationSpec> annotations) (package private)
TypeName
(List<AnnotationSpec> annotations) -
Method Summary
Modifier and TypeMethodDescriptionfinal TypeName
annotated
(AnnotationSpec... annotations) annotated
(List<AnnotationSpec> annotations) (package private) static TypeName
arrayComponent
(TypeName type) Returns the array component oftype
, or null iftype
is not an array.box()
Returns a boxed type if this is a primitive type (likeInteger
forint
) orvoid
.protected final List
<AnnotationSpec> concatAnnotations
(List<AnnotationSpec> annotations) (package private) CodeWriter
emit
(CodeWriter out) (package private) CodeWriter
final boolean
static TypeName
Returns a type name equivalent totype
.(package private) static TypeName
get
(Type type, Map<Type, TypeVariableName> map) static TypeName
get
(TypeMirror mirror) Returns a type name equivalent tomirror
.(package private) static TypeName
get
(TypeMirror mirror, Map<TypeParameterElement, TypeVariableName> typeVariables) final int
hashCode()
boolean
boolean
Returns true if this is a boxed primitive type likeInteger
.boolean
Returns true if this is a primitive type likeint
.Converts an array of types to a list of type names.list
(Type[] types, Map<Type, TypeVariableName> map) final String
toString()
unbox()
Returns an unboxed type if this is a boxed primitive type (likeint
forInteger
) orVoid
.
-
Field Details
-
VOID
-
BOOLEAN
-
BYTE
-
SHORT
-
INT
-
LONG
-
CHAR
-
FLOAT
-
DOUBLE
-
OBJECT
-
BOXED_VOID
-
BOXED_BOOLEAN
-
BOXED_BYTE
-
BOXED_SHORT
-
BOXED_INT
-
BOXED_LONG
-
BOXED_CHAR
-
BOXED_FLOAT
-
BOXED_DOUBLE
-
keyword
The name of this type if it is a keyword, or null. -
annotations
-
cachedString
Lazily-initialized toString of this type name.
-
-
Constructor Details
-
TypeName
-
TypeName
-
TypeName
TypeName(List<AnnotationSpec> annotations)
-
-
Method Details
-
annotated
-
annotated
-
withoutAnnotations
-
concatAnnotations
-
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
Returns a boxed type if this is a primitive type (likeInteger
forint
) orvoid
. Returns this type if boxing doesn't apply. -
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.
-
equals
-
hashCode
public final int hashCode() -
toString
-
emit
- Throws:
IOException
-
emitAnnotations
- Throws:
IOException
-
get
Returns a type name equivalent tomirror
. -
get
-
get
Returns a type name equivalent totype
. -
get
-
list
Converts an array of types to a list of type names. -
list
-
arrayComponent
Returns the array component oftype
, or null iftype
is not an array.
-