Class TypeName

  • Direct Known Subclasses:
    ArrayTypeName, ClassName, ParameterizedTypeName, TypeVariableName, WildcardTypeName

    public class TypeName
    extends java.lang.Object
    Any type in Java's type system, plus 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 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
      • 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
      • BOXED_VOID

        private static final ClassName BOXED_VOID
      • BOXED_BOOLEAN

        private static final ClassName BOXED_BOOLEAN
      • BOXED_BYTE

        private static final ClassName BOXED_BYTE
      • BOXED_SHORT

        private static final ClassName BOXED_SHORT
      • BOXED_INT

        private static final ClassName BOXED_INT
      • BOXED_LONG

        private static final ClassName BOXED_LONG
      • BOXED_CHAR

        private static final ClassName BOXED_CHAR
      • BOXED_FLOAT

        private static final ClassName BOXED_FLOAT
      • BOXED_DOUBLE

        private static final ClassName BOXED_DOUBLE
      • keyword

        private final java.lang.String keyword
        The name of this type if it is a keyword, or null.
      • annotations

        public final java.util.List<AnnotationSpec> annotations
      • cachedString

        private java.lang.String cachedString
        Lazily-initialized toString of this type name.
    • Constructor Detail

      • TypeName

        private TypeName​(java.lang.String keyword)
      • TypeName

        private TypeName​(java.lang.String keyword,
                         java.util.List<AnnotationSpec> annotations)
    • Method Detail

      • withoutAnnotations

        public TypeName withoutAnnotations()
      • isAnnotated

        public boolean isAnnotated()
      • isPrimitive

        public boolean isPrimitive()
        Returns true if this is a primitive type like int. Returns false for all other types types including boxed primitives and void.
      • isBoxedPrimitive

        public boolean isBoxedPrimitive()
        Returns true if this is a boxed primitive type like Integer. Returns false for all other types types including unboxed primitives and java.lang.Void.
      • box

        public TypeName box()
        Returns a boxed type if this is a primitive type (like Integer for int) or void. Returns this type if boxing doesn't apply.
      • unbox

        public TypeName unbox()
        Returns an unboxed type if this is a boxed primitive type (like int for Integer) or Void. Returns this type if it is already unboxed.
        Throws:
        java.lang.UnsupportedOperationException - if this type isn't eligible for unboxing.
      • equals

        public final boolean equals​(java.lang.Object o)
        Overrides:
        equals in class java.lang.Object
      • hashCode

        public final int hashCode()
        Overrides:
        hashCode in class java.lang.Object
      • toString

        public final java.lang.String toString()
        Overrides:
        toString in class java.lang.Object
      • emitAnnotations

        CodeWriter emitAnnotations​(CodeWriter out)
                            throws java.io.IOException
        Throws:
        java.io.IOException
      • get

        public static TypeName get​(javax.lang.model.type.TypeMirror mirror)
        Returns a type name equivalent to mirror.
      • get

        static TypeName get​(javax.lang.model.type.TypeMirror mirror,
                            java.util.Map<javax.lang.model.element.TypeParameterElement,​TypeVariableName> typeVariables)
      • get

        public static TypeName get​(java.lang.reflect.Type type)
        Returns a type name equivalent to type.
      • get

        static TypeName get​(java.lang.reflect.Type type,
                            java.util.Map<java.lang.reflect.Type,​TypeVariableName> map)
      • list

        static java.util.List<TypeName> list​(java.lang.reflect.Type[] types)
        Converts an array of types to a list of type names.
      • list

        static java.util.List<TypeName> list​(java.lang.reflect.Type[] types,
                                             java.util.Map<java.lang.reflect.Type,​TypeVariableName> map)
      • arrayComponent

        static TypeName arrayComponent​(TypeName type)
        Returns the array component of type, or null if type is not an array.
      • asArray

        static ArrayTypeName asArray​(TypeName type)
        Returns type as an array, or null if type is not an array.