Class TypeName

java.lang.Object
io.micronaut.sourcegen.javapoet.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.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 Details

  • Method Details

    • annotated

      public final TypeName annotated(AnnotationSpec... annotations)
    • annotated

      public TypeName annotated(List<AnnotationSpec> annotations)
    • withoutAnnotations

      public TypeName withoutAnnotations()
    • concatAnnotations

      protected final List<AnnotationSpec> concatAnnotations(List<AnnotationSpec> annotations)
    • 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.
    • equals

      public final boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

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

      public final String toString()
      Overrides:
      toString 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.