Open-Awesome
CategoriesAlternativesStacksSelf-HostedExplore
Open-Awesome

© 2026 Open-Awesome. Curated for the developer elite.

TermsPrivacyAboutGitHubRSS
  1. Home
  2. Java
  3. JavaPoet

JavaPoet

Apache-2.0Java

A Java API for generating .java source files programmatically, useful for annotation processing and code generation.

GitHubGitHub
10.9k stars1.4k forks0 contributors

What is JavaPoet?

JavaPoet is a Java library that enables programmatic generation of `.java` source files. It provides a fluent API to create classes, methods, fields, and other code structures, making it ideal for scenarios like annotation processing or generating code from metadata. By automating boilerplate, it helps maintain consistency and reduces manual errors.

Target Audience

Java developers working on annotation processors, code generation tools, or projects that need to produce Java source files dynamically from schemas, protocols, or other metadata.

Value Proposition

JavaPoet offers a type-safe, readable, and composable API that handles formatting and imports automatically, unlike string concatenation or templating. It generates clean, reviewable source code and is designed as a successor to JavaWriter with better type modeling and import management.

Overview

A Java API for generating .java source files.

Use Cases

Best For

  • Building annotation processors that generate supporting classes
  • Creating code from database schemas or protocol buffer definitions
  • Generating boilerplate code for libraries or frameworks
  • Dynamically producing Java source files in build tools
  • Implementing metaprogramming patterns in Java projects
  • Replacing handwritten repetitive code with automated generation

Not Ideal For

  • Projects requiring support for the latest Java language features (e.g., records, sealed classes)
  • Teams that prioritize long-term maintenance and active community support
  • Applications needing bytecode generation or direct AST manipulation for performance or obfuscation

Pros & Cons

Pros

Fluent Builder API

Uses method chaining and immutable objects like MethodSpec and TypeSpec builders, making code generation intuitive and less error-prone than string concatenation, as demonstrated in the HelloWorld example.

Automated Import Management

Supports $T placeholders for types with automatic import handling, simplifying the generation of correct Java files and reducing manual import errors.

Control Flow Simplification

Provides beginControlFlow() and endControlFlow() methods to easily generate loops, conditionals, and try-catch blocks, as shown in examples for if/else and for loops.

Comprehensive Model Support

Covers a wide range of Java constructs including classes, interfaces, enums, annotations, and Javadoc, allowing for diverse code generation needs.

Cons

Deprecated Status

Square has officially deprecated the project since 2020, meaning no updates, bug fixes, or support for newer Java versions, forcing users to switch to forks like Palantir's.

String-Based Code Blocks

Relies on raw strings for method bodies and expressions, which can lead to syntax errors and lacks the type safety of a structured abstract syntax tree, as admitted in the 'Code & Control Flow' section.

Migration Overhead

Switching to the recommended Palantir fork requires changes in Maven coordinates, imports, and API usage (e.g., packageName to packageName()), adding complexity for existing projects.

Frequently Asked Questions

Quick Stats

Stars10,856
Forks1,387
Contributors0
Open Issues92
Last commit1 year ago
CreatedSince 2013

Tags

#metaprogramming#build-tools#annotation-processing#java#fluent-api#source-code#code-generation#square

Built With

J
Java

Included in

Java47.5kJVM2.2k
Auto-fetched 1 day ago

Related Projects

JHipsterJHipster

JHipster is a development platform to quickly generate, develop, & deploy modern web applications & microservice architectures.

Stars22,393
Forks4,170
Last commit1 day ago
TruffleTruffle

GraalVM compiles applications into native executables that start instantly, scale fast, and use fewer compute resources 🚀

Stars21,557
Forks1,790
Last commit1 day ago
autoauto

A collection of source code generators for Java.

Stars10,554
Forks1,211
Last commit8 days ago
error-proneerror-prone

Catch common Java mistakes as compile-time errors

Stars7,158
Forks789
Last commit2 days ago
Community-curated · Updated weekly · 100% open source

Found a gem we're missing?

Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.

Submit a projectStar on GitHub