public class TypeName
extends java.lang.Object
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.
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)
.
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
.
Modifier and Type | Field and 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 |
Modifier | Constructor and Description |
---|---|
(package private) |
TypeName(java.util.List<AnnotationSpec> annotations) |
private |
TypeName(java.lang.String keyword) |
private |
TypeName(java.lang.String keyword,
java.util.List<AnnotationSpec> annotations) |
Modifier and Type | Method and Description |
---|---|
TypeName |
annotated(AnnotationSpec... annotations) |
TypeName |
annotated(java.util.List<AnnotationSpec> annotations) |
(package private) static TypeName |
arrayComponent(TypeName type)
Returns the array component of
type , or null if type is not an array. |
TypeName |
box()
Returns a boxed type if this is a primitive type (like
Integer for int ) or
void . |
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 to
type . |
(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 to
mirror . |
(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 like
Integer . |
boolean |
isPrimitive()
Returns true if this is a primitive type like
int . |
(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 (like
int for Integer ) or Void . |
TypeName |
withoutAnnotations() |
public static final TypeName VOID
public static final TypeName BOOLEAN
public static final TypeName BYTE
public static final TypeName SHORT
public static final TypeName INT
public static final TypeName LONG
public static final TypeName CHAR
public static final TypeName FLOAT
public static final TypeName DOUBLE
public static final ClassName OBJECT
private static final ClassName BOXED_VOID
private static final ClassName BOXED_BOOLEAN
private static final ClassName BOXED_BYTE
private static final ClassName BOXED_SHORT
private static final ClassName BOXED_INT
private static final ClassName BOXED_LONG
private static final ClassName BOXED_CHAR
private static final ClassName BOXED_FLOAT
private static final ClassName BOXED_DOUBLE
private final java.lang.String keyword
public final java.util.List<AnnotationSpec> annotations
private java.lang.String cachedString
private TypeName(java.lang.String keyword)
private TypeName(java.lang.String keyword, java.util.List<AnnotationSpec> annotations)
TypeName(java.util.List<AnnotationSpec> annotations)
public final TypeName annotated(AnnotationSpec... annotations)
public TypeName annotated(java.util.List<AnnotationSpec> annotations)
public TypeName withoutAnnotations()
protected final java.util.List<AnnotationSpec> concatAnnotations(java.util.List<AnnotationSpec> annotations)
public boolean isAnnotated()
public boolean isPrimitive()
int
. Returns false for all other types
types including boxed primitives and void
.public boolean isBoxedPrimitive()
Integer
. Returns false for all
other types types including unboxed primitives and java.lang.Void
.public TypeName box()
Integer
for int
) or
void
. Returns this type if boxing doesn't apply.public TypeName unbox()
int
for Integer
) or Void
. Returns this type if it is already unboxed.java.lang.UnsupportedOperationException
- if this type isn't eligible for unboxing.public final boolean equals(java.lang.Object o)
equals
in class java.lang.Object
public final int hashCode()
hashCode
in class java.lang.Object
public final java.lang.String toString()
toString
in class java.lang.Object
CodeWriter emit(CodeWriter out) throws java.io.IOException
java.io.IOException
CodeWriter emitAnnotations(CodeWriter out) throws java.io.IOException
java.io.IOException
public static TypeName get(javax.lang.model.type.TypeMirror mirror)
mirror
.static TypeName get(javax.lang.model.type.TypeMirror mirror, java.util.Map<javax.lang.model.element.TypeParameterElement,TypeVariableName> typeVariables)
public static TypeName get(java.lang.reflect.Type type)
type
.static TypeName get(java.lang.reflect.Type type, java.util.Map<java.lang.reflect.Type,TypeVariableName> map)
static java.util.List<TypeName> list(java.lang.reflect.Type[] types)
static java.util.List<TypeName> list(java.lang.reflect.Type[] types, java.util.Map<java.lang.reflect.Type,TypeVariableName> map)