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.util.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
-
Method Summary
Modifier and TypeMethodDescriptionfinal TypeName
annotated
(AnnotationSpec... annotations) annotated
(List<AnnotationSpec> annotations) box()
Returns a boxed type if this is a primitive type (likeInteger
forint
) orvoid
.protected final List<AnnotationSpec>
concatAnnotations
(List<AnnotationSpec> annotations) final boolean
static TypeName
Returns a type name equivalent totype
.static TypeName
get
(TypeMirror mirror) Returns a type name equivalent tomirror
.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
.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
-
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
-
get
Returns a type name equivalent tomirror
. -
get
Returns a type name equivalent totype
.
-