explaingit

square/javapoet

10,849JavaAudience · developerComplexity · 2/5Setup · easy

TLDR

JavaPoet is a deprecated Java library from Square that lets you write code which generates other Java code, automatically handling formatting details like imports, indentation, and semicolons.

Mindmap

mindmap
  root((JavaPoet))
    What it does
      Generates Java code
      Formats source files
      Manages imports
    Building blocks
      Classes and interfaces
      Methods and fields
      Annotations
    Status
      Deprecated 2020
      Palantir fork available
    Use cases
      Annotation processing
      Code generation
      Android projects
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

Things people build with this

USE CASE 1

Generate repetitive Java boilerplate classes automatically using annotation processing instead of hand-writing them.

USE CASE 2

Build a code generation tool that produces readable, well-formatted Java source files programmers can inspect and verify.

USE CASE 3

Replace error-prone string concatenation in code generators with JavaPoet's type-safe builder API.

Tech stack

Java

Getting it running

Difficulty · easy Time to first run · 30min

Project is officially deprecated since October 2020, use the Palantir fork for new Java language feature support.

No license information provided in the explanation, check the repository directly.

In plain English

JavaPoet is a Java library that lets you write programs that generate other Java programs. Instead of manually typing out repetitive Java code, you write Java code that describes what you want to produce, and JavaPoet assembles the final source file for you. This is useful in situations like annotation processing, where tools need to create helper classes automatically based on your project's structure. The library gives you building blocks for every part of a Java file: classes, interfaces, methods, constructors, fields, parameters, and annotations. You assemble these pieces with a builder-style API, then ask JavaPoet to render everything into a properly formatted .java file. It handles indentation, semicolons, import statements, and other formatting details so you do not have to. A small but important note: Square officially deprecated this project in October 2020. They no longer maintain it. The README points to a fork maintained by Palantir as an alternative for anyone who needs support for newer Java language features. Switching requires updating your build coordinates and running a short find-and-replace on your import statements. Before deprecation, the library had wide adoption for code generation tasks in Android and Java projects. The core concepts are straightforward: you describe a method or class in Java, and JavaPoet serializes that description into valid source text you can inspect, test, or write to disk. Because it produces readable source code rather than compiled bytecode, you can open the output files and verify they look exactly as intended. The README is detailed and covers the full API including type references, generics, annotations, Javadoc comments, and anonymous inner classes, with code examples for each feature.

Copy-paste prompts

Prompt 1
Using JavaPoet, write code that generates a Java class called UserRepository with a findById(long id) method that returns Optional<User>.
Prompt 2
Show me how to use JavaPoet's MethodSpec.methodBuilder to generate a method with a Javadoc comment and a generic return type.
Prompt 3
How do I wire JavaPoet into a Java annotation processor so it writes generated .java files to the correct source output folder?
Prompt 4
I'm migrating from square/javapoet to the Palantir fork. What import names do I need to find-and-replace and what build coordinates change?
Open on GitHub → Explain another repo

← square on gitmyhub — every repo by this author, as a profile.

Verify against the repo before relying on details.