explaingit

raphw/byte-buddy

6,824JavaAudience · developerComplexity · 3/5Setup · moderate

TLDR

Java library that generates and modifies Java classes at runtime without compiling anything ahead of time. Used internally by Mockito, Hibernate, and Jackson, downloaded over 75 million times a year.

Mindmap

mindmap
  root((Byte Buddy))
    What it does
      Runtime class creation
      Class modification
      Java agent support
    Use Cases
      Mock objects
      ORM proxies
      AOP frameworks
    Tech Stack
      Java
      ASM library
      JVM bytecode
    Used By
      Mockito
      Hibernate
      Jackson
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 mock objects for Java unit tests by creating subclasses at runtime that intercept method calls without modifying source code.

USE CASE 2

Build a Java agent that rewrites existing classes as they are loaded by the JVM to add logging, metrics, or other behavior transparently.

USE CASE 3

Write a framework that wraps user-provided classes at runtime to add cross-cutting behavior like database access or caching without touching the originals.

USE CASE 4

Generate a Java class that implements a dynamically specified interface at runtime, without writing raw bytecode by hand.

Tech stack

JavaASMMaven

Getting it running

Difficulty · moderate Time to first run · 30min

Designed for library and framework authors, requires understanding of Java class hierarchies and method interception patterns.

In plain English

Byte Buddy is a Java library that creates and modifies Java classes while a program is already running, without needing to compile anything ahead of time. Most code you write gets compiled before it runs, but some libraries need to work with types and classes that are only known at the moment users call them. Byte Buddy solves that by generating new class definitions on the fly, inside the running Java process. You do not need to understand Java bytecode to use it. The API is designed to read like regular Java: you pick a class to extend or modify, describe which methods to intercept, and say what those methods should do instead. The generated classes have no dependency on Byte Buddy itself, so once they are created they can exist on their own without the library present. The library is used inside many well-known Java tools. Mockito uses it to create mock objects for testing. Hibernate uses it to generate database-aware versions of your model classes. Jackson and Google's Bazel build system also rely on it. Byte Buddy is downloaded over 75 million times a year and has been in production use for over a decade. In 2015 it received Oracle's Duke's Choice award for innovation in Java technology. Byte Buddy supports every modern Java version. It can generate class files for Java 25 while itself running on a Java 5 or newer JVM. The only dependency it requires is ASM, a low-level bytecode parsing library. Beyond basic class generation it also supports Java agents, which are small programs that can rewrite other classes as they are loaded by the JVM, without modifying the original source files. In practical terms, Byte Buddy is a tool for library authors rather than typical application developers. If you are writing a framework that needs to wrap or extend user-provided classes at runtime, it provides a straightforward way to do that without writing raw bytecode by hand.

Copy-paste prompts

Prompt 1
Write a Byte Buddy subclass of an existing Java class that intercepts all method calls and logs their method name and arguments before delegating to the original implementation.
Prompt 2
How do I use Byte Buddy to create a Java agent that adds performance timing to every method in a target application at JVM startup?
Prompt 3
Use Byte Buddy to generate a proxy class at runtime that implements a given Java interface and records all method calls to a list for later inspection.
Prompt 4
How do I use Byte Buddy to add a new field and getter method to a class at runtime using a Java agent transformer?
Prompt 5
Create a Byte Buddy subclass that overrides a specific method to return a fixed stub value, useful for replacing a slow external dependency in tests.
Open on GitHub → Explain another repo

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

Verify against the repo before relying on details.