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.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.
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 |
static TypeName |
BYTE |
static TypeName |
CHAR |
static TypeName |
DOUBLE |
static TypeName |
FLOAT |
static TypeName |
INT |
static TypeName |
LONG |
static ClassName |
OBJECT |
static TypeName |
SHORT |
static TypeName |
VOID |
Modifier and Type | Method and Description |
---|---|
TypeName |
annotated(AnnotationSpec... annotations) |
TypeName |
annotated(java.util.List<AnnotationSpec> annotations) |
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) |
boolean |
equals(java.lang.Object o) |
static TypeName |
get(java.lang.reflect.Type type)
Returns a type name equivalent to
type . |
static TypeName |
get(javax.lang.model.type.TypeMirror mirror)
Returns a type name equivalent to
mirror . |
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 . |
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
public final 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
public static TypeName get(javax.lang.model.type.TypeMirror mirror)
mirror
.public static TypeName get(java.lang.reflect.Type type)
type
.Copyright © 2020 Square, Inc.. All Rights Reserved.