Class TypeName

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

    public class TypeName
    extends 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.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.

    • 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:
        UnsupportedOperationException - if this type isn't eligible for unboxing.
      • hashCode

        public final int hashCode()
        Overrides:
        hashCode in class Object
      • get

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

        public static TypeName get​(Type type)
        Returns a type name equivalent to type.