Generate mock objects for Java unit tests by creating subclasses at runtime that intercept method calls without modifying source code.
Build a Java agent that rewrites existing classes as they are loaded by the JVM to add logging, metrics, or other behavior transparently.
Write a framework that wraps user-provided classes at runtime to add cross-cutting behavior like database access or caching without touching the originals.
Generate a Java class that implements a dynamically specified interface at runtime, without writing raw bytecode by hand.
Designed for library and framework authors, requires understanding of Java class hierarchies and method interception patterns.
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.
← raphw on gitmyhub — every repo by this author, as a profile.
Verify against the repo before relying on details.