Java API for generating .java source files programmatically with fluent builder interfaces.
—
Type representation system for Java including primitive types, class names, and basic type operations. This forms the foundation for all type references in JavaPoet.
Base class for all type representations in JavaPoet. Provides common type operations and constants for primitive types.
/**
* Base class for all type representations
*/
public class TypeName {
// Primitive type constants
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 fields
public final List<AnnotationSpec> annotations;
// Factory methods
public static TypeName get(TypeMirror mirror);
public static TypeName get(Type type);
// Annotation methods
public TypeName annotated(AnnotationSpec... annotations);
public TypeName annotated(List<AnnotationSpec> annotations);
public TypeName withoutAnnotations();
public boolean isAnnotated();
// Type classification methods
public boolean isPrimitive();
public boolean isBoxedPrimitive();
// Boxing/unboxing operations
public TypeName box();
public TypeName unbox();
// Standard object methods
public boolean equals(Object o);
public int hashCode();
public String toString();
}Usage Examples:
// Using primitive type constants
MethodSpec getter = MethodSpec.methodBuilder("getCount")
.returns(TypeName.INT)
.addStatement("return count")
.build();
// Boxing and unboxing
TypeName primitiveInt = TypeName.INT;
TypeName boxedInt = primitiveInt.box(); // Integer
TypeName backToPrimitive = boxedInt.unbox(); // int
// Type classification
if (someType.isPrimitive()) {
// Handle primitive type
}
if (someType.isBoxedPrimitive()) {
// Handle boxed primitive (Integer, Boolean, etc.)
}
// Creating types from reflection
TypeName stringType = TypeName.get(String.class);
TypeName listType = TypeName.get(List.class);
// Annotated types
TypeName annotatedString = TypeName.get(String.class)
.annotated(AnnotationSpec.builder(NonNull.class).build());Represents declared class, interface, or annotation type names. Provides methods for working with package names, nested classes, and type hierarchies.
/**
* Represents declared class, interface, or annotation type names
*/
public final class ClassName extends TypeName implements Comparable<ClassName> {
// Static constants
public static final ClassName OBJECT;
// Factory methods
public static ClassName get(Class<?> clazz);
public static ClassName bestGuess(String classNameString);
public static ClassName get(String packageName, String simpleName, String... simpleNames);
public static ClassName get(TypeElement element);
// Annotation methods (override parent)
public ClassName annotated(List<AnnotationSpec> annotations);
public ClassName withoutAnnotations();
// Name component methods
public String packageName();
public String simpleName();
public List<String> simpleNames();
public String canonicalName();
public String reflectionName();
// Hierarchy methods
public ClassName enclosingClassName();
public ClassName topLevelClassName();
// Related class methods
public ClassName peerClass(String name);
public ClassName nestedClass(String name);
// Comparable implementation
public int compareTo(ClassName o);
}Usage Examples:
// Creating class names from Class objects
ClassName stringClass = ClassName.get(String.class);
ClassName listClass = ClassName.get(List.class);
ClassName mapEntryClass = ClassName.get(Map.Entry.class);
// Creating class names from package and name components
ClassName myClass = ClassName.get("com.example", "MyClass");
ClassName nestedClass = ClassName.get("com.example", "Outer", "Inner");
// Best guess parsing (use with caution)
ClassName guessedClass = ClassName.bestGuess("com.example.MyClass");
// Working with nested classes
ClassName outerClass = ClassName.get("com.example", "Outer");
ClassName innerClass = outerClass.nestedClass("Inner"); // com.example.Outer.Inner
ClassName peerClass = innerClass.peerClass("Sibling"); // com.example.Sibling
// Extracting name components
System.out.println(mapEntryClass.packageName()); // "java.util"
System.out.println(mapEntryClass.simpleName()); // "Entry"
System.out.println(mapEntryClass.canonicalName()); // "java.util.Map.Entry"
System.out.println(mapEntryClass.reflectionName()); // "java.util.Map$Entry"
// Hierarchy navigation
ClassName entry = ClassName.get(Map.Entry.class);
ClassName map = entry.enclosingClassName(); // java.util.Map
ClassName topLevel = entry.topLevelClassName(); // java.util.MapJavaPoet can create type representations from Java reflection types:
// From Class objects
TypeName stringType = TypeName.get(String.class);
TypeName intType = TypeName.get(int.class);
TypeName voidType = TypeName.get(void.class);
// From generic reflection types
Method method = MyClass.class.getMethod("process", List.class);
Type returnType = method.getGenericReturnType();
TypeName returnTypeName = TypeName.get(returnType);
// From field types
Field field = MyClass.class.getField("items");
TypeName fieldType = TypeName.get(field.getGenericType());For annotation processing, JavaPoet integrates with javax.lang.model types:
// From TypeMirror (annotation processing)
@Override
public boolean process(Set<? extends TypeElement> annotations,
RoundEnvironment roundEnv) {
for (Element element : roundEnv.getElementsAnnotatedWith(MyAnnotation.class)) {
TypeMirror typeMirror = element.asType();
TypeName typeName = TypeName.get(typeMirror);
// Use typeName in code generation...
}
return true;
}
// From TypeElement
TypeElement classElement = processingEnv.getElementUtils()
.getTypeElement("com.example.MyClass");
ClassName className = ClassName.get(classElement);All TypeName instances support type annotations:
// Single annotation
TypeName annotatedString = ClassName.get(String.class)
.annotated(AnnotationSpec.builder(NonNull.class).build());
// Multiple annotations
TypeName multiAnnotated = TypeName.INT
.annotated(
AnnotationSpec.builder(Range.class)
.addMember("min", "0")
.addMember("max", "100")
.build(),
AnnotationSpec.builder(NonNegative.class).build()
);
// Remove annotations
TypeName withoutAnnotations = annotatedString.withoutAnnotations();
// Check for annotations
if (someType.isAnnotated()) {
List<AnnotationSpec> annotations = someType.annotations;
// Process annotations...
}// Type equality
TypeName type1 = TypeName.get(String.class);
TypeName type2 = ClassName.get(String.class);
boolean equal = type1.equals(type2); // true
// ClassName comparison (implements Comparable)
ClassName class1 = ClassName.get("com.example", "A");
ClassName class2 = ClassName.get("com.example", "B");
int comparison = class1.compareTo(class2); // negative value
// Sorting class names
List<ClassName> classNames = Arrays.asList(
ClassName.get("z.example", "Last"),
ClassName.get("a.example", "First")
);
Collections.sort(classNames); // Sorts by canonical name// The universal Object type
ClassName objectType = ClassName.OBJECT; // java.lang.Object
TypeName objectTypeName = TypeName.OBJECT; // Same as above
// Checking if a type extends Object (all reference types do)
boolean extendsObject = !someType.isPrimitive(); // true for all reference types
// Using Object in method signatures
MethodSpec method = MethodSpec.methodBuilder("process")
.addParameter(TypeName.OBJECT, "item")
.returns(TypeName.OBJECT)
.addStatement("return processItem(item)")
.build();Install with Tessl CLI
npx tessl i tessl/maven-com-squareup--javapoet