CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/maven-com-palantir-javapoet--javapoet

JavaPoet is a Java API for generating .java source files programmatically with support for modern Java features including records and sealed types

Overview
Eval results
Files

type-names.mddocs/

Type Names

TypeName is the abstract base class for representing Java types in JavaPoet. It provides a type-safe way to reference primitive types, class types, parameterized types, arrays, wildcards, and type variables.

Capabilities

Primitive Type Constants

Pre-defined constants for Java primitive types and void.

/**
 * Represents the void type
 */
public static final TypeName VOID;

/**
 * Represents the boolean primitive type
 */
public static final TypeName BOOLEAN;

/**
 * Represents the byte primitive type
 */
public static final TypeName BYTE;

/**
 * Represents the short primitive type
 */
public static final TypeName SHORT;

/**
 * Represents the int primitive type
 */
public static final TypeName INT;

/**
 * Represents the long primitive type
 */
public static final TypeName LONG;

/**
 * Represents the char primitive type
 */
public static final TypeName CHAR;

/**
 * Represents the float primitive type
 */
public static final TypeName FLOAT;

/**
 * Represents the double primitive type
 */
public static final TypeName DOUBLE;

Usage Example:

FieldSpec count = FieldSpec.builder(TypeName.INT, "count")
    .addModifiers(Modifier.PRIVATE)
    .build();

MethodSpec calculate = MethodSpec.methodBuilder("calculate")
    .returns(TypeName.DOUBLE)
    .addParameter(TypeName.INT, "value")
    .build();

Creating TypeName from Types

Factory methods for creating TypeName instances from various type representations.

/**
 * Creates a TypeName from a TypeMirror (annotation processing)
 * @param mirror - The type mirror
 * @return TypeName representing the type
 */
static TypeName get(TypeMirror mirror);

/**
 * Creates a TypeName from a reflection Type
 * @param type - The reflection type
 * @return TypeName representing the type
 */
static TypeName get(Type type);

Usage Examples:

// From Class
TypeName stringType = TypeName.get(String.class);
TypeName listType = TypeName.get(List.class);

// From ParameterizedType
Type paramType = ...; // e.g., List<String>
TypeName paramTypeName = TypeName.get(paramType);

// From annotation processing TypeMirror
TypeMirror mirror = element.asType();
TypeName typeName = TypeName.get(mirror);

Working with Annotations

Add or remove type annotations on TypeName instances.

/**
 * Returns the annotations on this type
 * @return List of AnnotationSpec
 */
List<AnnotationSpec> annotations();

/**
 * Returns a copy with additional annotations
 * @param annotations - Annotations to add
 * @return New TypeName with annotations
 */
TypeName annotated(AnnotationSpec... annotations);

/**
 * Returns a copy with additional annotations from a list
 * @param annotations - List of annotations to add
 * @return New TypeName with annotations
 */
TypeName annotated(List<AnnotationSpec> annotations);

/**
 * Returns a copy without any annotations
 * @return New TypeName without annotations
 */
TypeName withoutAnnotations();

/**
 * Checks if this type has any annotations
 * @return true if annotated, false otherwise
 */
boolean isAnnotated();

Usage Examples:

// Add annotation to type
TypeName nonNullString = TypeName.get(String.class)
    .annotated(AnnotationSpec.builder(NotNull.class).build());

// Multiple annotations
TypeName annotatedInt = TypeName.INT
    .annotated(
        AnnotationSpec.builder(Min.class).addMember("value", "$L", 0).build(),
        AnnotationSpec.builder(Max.class).addMember("value", "$L", 100).build()
    );

// Remove annotations
TypeName plain = annotatedInt.withoutAnnotations();

// Check for annotations
if (typeName.isAnnotated()) {
    System.out.println("Type has annotations");
}

Primitive Type Operations

Check and convert between primitive and boxed types.

/**
 * Checks if this is a primitive type
 * @return true if primitive (int, boolean, etc.), false otherwise
 */
boolean isPrimitive();

/**
 * Checks if this is a boxed primitive type
 * @return true if boxed (Integer, Boolean, etc.), false otherwise
 */
boolean isBoxedPrimitive();

/**
 * Returns the boxed version of a primitive type
 * @return Boxed TypeName (int -> Integer)
 * @throws UnsupportedOperationException if not a primitive
 */
TypeName box();

/**
 * Returns the unboxed version of a boxed primitive type
 * @return Unboxed TypeName (Integer -> int)
 * @throws UnsupportedOperationException if not a boxed primitive
 */
TypeName unbox();

Usage Examples:

// Check if primitive
TypeName intType = TypeName.INT;
if (intType.isPrimitive()) {
    System.out.println("Is primitive");
}

// Box a primitive
TypeName integerType = TypeName.INT.box(); // int -> Integer

// Unbox a boxed type
TypeName intType2 = TypeName.get(Integer.class).unbox(); // Integer -> int

// Check if boxed
TypeName integer = TypeName.get(Integer.class);
if (integer.isBoxedPrimitive()) {
    TypeName primitive = integer.unbox();
}

Advanced: Visitor Methods for javax.lang.model Integration

TypeName implements visitor methods for javax.lang.model type processing. These methods are used internally by the get(TypeMirror) factory method and are rarely called directly by users.

/**
 * Visits a primitive type from javax.lang.model
 * @param t - The primitive type
 * @param _p - Unused parameter
 * @return TypeName representing the primitive type
 */
TypeName visitPrimitive(PrimitiveType t, Void _p);

/**
 * Visits a declared type (class or interface) from javax.lang.model
 * @param t - The declared type
 * @param _p - Unused parameter
 * @return TypeName representing the declared type
 */
TypeName visitDeclared(DeclaredType t, Void _p);

/**
 * Visits an error type from javax.lang.model
 * @param t - The error type
 * @param _p - Unused parameter
 * @return TypeName representing the error type
 */
TypeName visitError(ErrorType t, Void _p);

/**
 * Visits an array type from javax.lang.model
 * @param t - The array type
 * @param _p - Unused parameter
 * @return ArrayTypeName representing the array type
 */
ArrayTypeName visitArray(ArrayType t, Void _p);

/**
 * Visits a type variable from javax.lang.model
 * @param t - The type variable
 * @param _p - Unused parameter
 * @return TypeVariableName representing the type variable
 */
TypeName visitTypeVariable(javax.lang.model.type.TypeVariable t, Void _p);

/**
 * Visits a wildcard type from javax.lang.model
 * @param t - The wildcard type
 * @param _p - Unused parameter
 * @return WildcardTypeName representing the wildcard type
 */
TypeName visitWildcard(javax.lang.model.type.WildcardType t, Void _p);

/**
 * Visits a NoType (e.g., void) from javax.lang.model
 * @param t - The no-type
 * @param _p - Unused parameter
 * @return TypeName representing void or the no-type
 */
TypeName visitNoType(NoType t, Void _p);

Note: These methods are part of the SimpleTypeVisitor pattern used for javax.lang.model integration. Normal users should use the TypeName.get(TypeMirror) factory method instead of calling these visitor methods directly.

TypeName Hierarchy

TypeName has several subclasses for different type kinds:

  • ClassName: Represents class and interface names
  • ParameterizedTypeName: Represents generic types (e.g., List<String>)
  • TypeVariableName: Represents type variables (e.g., T, E)
  • ArrayTypeName: Represents array types
  • WildcardTypeName: Represents wildcard types (? extends, ? super)

Common Patterns

Field Declarations

// Primitive field
FieldSpec count = FieldSpec.builder(TypeName.INT, "count")
    .addModifiers(Modifier.PRIVATE)
    .build();

// Object field
FieldSpec name = FieldSpec.builder(TypeName.get(String.class), "name")
    .addModifiers(Modifier.PRIVATE)
    .build();

Method Return Types

// Primitive return
MethodSpec getCount = MethodSpec.methodBuilder("getCount")
    .returns(TypeName.INT)
    .addStatement("return count")
    .build();

// Void return
MethodSpec doSomething = MethodSpec.methodBuilder("doSomething")
    .returns(TypeName.VOID)
    .addStatement("// do something")
    .build();

Parameters

MethodSpec calculate = MethodSpec.methodBuilder("calculate")
    .addParameter(TypeName.INT, "a")
    .addParameter(TypeName.INT, "b")
    .returns(TypeName.LONG)
    .build();

Boxing and Unboxing

// Convert between primitive and boxed
TypeName primitive = TypeName.INT;
TypeName boxed = primitive.box(); // Integer

FieldSpec counter = FieldSpec.builder(boxed, "counter")
    .addModifiers(Modifier.PRIVATE)
    .build();

// Using in collections (primitives must be boxed)
TypeName listOfIntegers = ParameterizedTypeName.get(
    ClassName.get(List.class),
    TypeName.INT.box()
);

Annotated Types

// Annotated parameter type
TypeName nonNullString = TypeName.get(String.class)
    .annotated(AnnotationSpec.builder(NotNull.class).build());

ParameterSpec param = ParameterSpec.builder(nonNullString, "value")
    .build();

// Annotated field type
TypeName nullableInteger = TypeName.get(Integer.class)
    .annotated(AnnotationSpec.builder(Nullable.class).build());

FieldSpec field = FieldSpec.builder(nullableInteger, "optionalValue")
    .addModifiers(Modifier.PRIVATE)
    .build();

Type Checking

TypeName type = ...; // some type

if (type.isPrimitive()) {
    // Handle primitive types
    TypeName boxed = type.box();
} else if (type.isBoxedPrimitive()) {
    // Handle boxed primitives
    TypeName unboxed = type.unbox();
} else if (type instanceof ClassName) {
    // Handle class types
    ClassName className = (ClassName) type;
} else if (type instanceof ParameterizedTypeName) {
    // Handle generic types
    ParameterizedTypeName paramType = (ParameterizedTypeName) type;
}

Primitive Type Mapping

PrimitiveBoxed Type
booleanBoolean
byteByte
shortShort
intInteger
longLong
charCharacter
floatFloat
doubleDouble
voidVoid

Types

abstract class TypeName {
    // Primitive type constants
    static final TypeName VOID;
    static final TypeName BOOLEAN;
    static final TypeName BYTE;
    static final TypeName SHORT;
    static final TypeName INT;
    static final TypeName LONG;
    static final TypeName CHAR;
    static final TypeName FLOAT;
    static final TypeName DOUBLE;

    // Factory methods
    static TypeName get(TypeMirror mirror);
    static TypeName get(Type type);

    // Annotation methods
    List<AnnotationSpec> annotations();
    TypeName annotated(AnnotationSpec... annotations);
    TypeName annotated(List<AnnotationSpec> annotations);
    TypeName withoutAnnotations();
    boolean isAnnotated();

    // Primitive type methods
    boolean isPrimitive();
    boolean isBoxedPrimitive();
    TypeName box();
    TypeName unbox();

    // Standard methods (final)
    final boolean equals(Object o);
    final int hashCode();
    final String toString();

    // Visitor methods for javax.lang.model integration (rarely used directly)
    TypeName visitPrimitive(PrimitiveType t, Void _p);
    TypeName visitDeclared(DeclaredType t, Void _p);
    TypeName visitError(ErrorType t, Void _p);
    ArrayTypeName visitArray(ArrayType t, Void _p);
    TypeName visitTypeVariable(javax.lang.model.type.TypeVariable t, Void _p);
    TypeName visitWildcard(javax.lang.model.type.WildcardType t, Void _p);
    TypeName visitNoType(NoType t, Void _p);
}

Install with Tessl CLI

npx tessl i tessl/maven-com-palantir-javapoet--javapoet@0.11.0

docs

annotation-specifications.md

array-type-names.md

class-names.md

code-blocks.md

field-specifications.md

index.md

java-files.md

method-specifications.md

name-allocator.md

parameter-specifications.md

parameterized-type-names.md

type-names.md

type-specifications.md

type-variable-names.md

wildcard-type-names.md

tile.json