Quick revision
The short answer
Frameworks generate or change bytecode to add behaviour we did not write by hand. Spring wraps beans in proxies for transactions and AOP, using JDK dynamic proxies for interfaces or CGLIB subclasses; Hibernate and Mockito use Byte Buddy, built on ASM, to create lazy-loading proxies, enhance entities and build mocks; and Java agents can transform any class as it loads through java.lang.instrument. Because those third-party libraries parse class files themselves, each new class file version forces them to update, which has often delayed framework support for new JDKs. The Class-File API, final in Java 24 through JEP 484, gives a JDK-maintained API that always understands the current class file version.
What to remember
In this answer, we saw that frameworks generate bytecode to add behaviour around our code: Spring creates JDK dynamic proxies or CGLIB subclasses for transactions and AOP, Hibernate and Mockito use Byte Buddy on top of ASM for lazy-loading proxies, entity enhancement and mocks, and Java agents rewrite classes as they load through java.lang.instrument. Because these libraries parse class files themselves, each new class file version required updates and delayed framework support for new JDKs; the Class-File API, final in Java 24 through JEP 484, gives a JDK-maintained parser and generator that always matches the current version.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
Frameworks generate or change bytecode to add behaviour we did not write by hand. Spring wraps beans in proxies for transactions and AOP, using JDK dynamic proxies for interfaces or CGLIB subclasses; Hibernate and Mockito use Byte Buddy, built on ASM, to create lazy-loading proxies, enhance entities and build mocks; and Java agents can transform any class as it loads through java.lang.instrument. Because those third-party libraries parse class files themselves, each new class file version forces them to update, which has often delayed framework support for new JDKs. The Class-File API, final in Java 24 through JEP 484, gives a JDK-maintained API that always understands the current class file version.
Why this question is asked
Much of what feels like magic in the Java ecosystem, @Transactional working without us calling a transaction manager, a lazy association loading on first access, a mock answering calls, is bytecode generated at runtime. When it works, we never think about it; when it breaks, the stack traces are full of $$SpringCGLIB$$ and ByteBuddy names, and upgrading the JDK can fail with 'Unsupported class file major version'. Interviewers ask this to see whether we understand the mechanism and its operational consequences. In this answer, we will see why frameworks generate code, look at the techniques Spring, Hibernate and Mockito use, then Java agents, then the upgrade problem and what the Class-File API changes.
1Why Frameworks Generate Code at Runtime
Frameworks want to add behaviour around our code: start a transaction before a method, load data when a field is first touched, record calls for a test. Java offers two ways to do that without the user writing it: wrap the object in another object that intercepts calls, a proxy, or change the class's bytecode itself. Both require creating or modifying classes while the program runs, because the framework only learns which classes need it when it sees our annotations and configuration at startup.
2Spring: JDK Dynamic Proxies and CGLIB Subclasses
When a Spring bean needs extra behaviour, such as @Transactional, @Async or an aspect, Spring creates a proxy and injects it instead of the original object. If the bean implements interfaces and Spring is configured to proxy interfaces, it can use a JDK dynamic proxy: java.lang.reflect.Proxy generates, at runtime, a class implementing those interfaces whose methods all go to an InvocationHandler. Otherwise Spring uses CGLIB, which it ships repackaged inside spring-core, to generate a subclass of our class that overrides each public method to run the interceptors and then call the original. Spring Boot proxies classes with CGLIB by default.
| Framework or tool | Technique | Used for |
|---|---|---|
| Spring (interfaces) | JDK dynamic proxy, java.lang.reflect.Proxy | AOP, transactions on interface-based beans |
| Spring (classes) | CGLIB subclass, repackaged in spring-core | Default class proxies in Spring Boot |
| Hibernate | Byte Buddy proxies, optional bytecode enhancement | Lazy loading, dirty tracking |
| Mockito | Byte Buddy, inline mock maker with an agent | Mocks of classes, final and static methods |
| Java agents | java.lang.instrument ClassFileTransformer | APM tracing, profilers, coverage |
Notice that the proxy is a subclass, which is why final classes and final methods cannot be proxied this way, and why calls from inside the object, on this, skip the proxy. For native images, Spring's AOT processing generates these proxy classes at build time instead, since a native image cannot define new classes at runtime.
3Hibernate and Mockito: Byte Buddy on Top of ASM
Hibernate needs proxies for lazy associations: when we load an Order, its customer can be a generated subclass of Customer that only queries the database when a method is first called. It can also enhance entity classes, changing their bytecode, usually at build time with a Maven or Gradle plugin, so that field access is tracked for dirty checking and lazy loading of individual attributes. Mockito creates mock objects whose methods record calls and return stubbed answers; since Mockito 5 its default inline mock maker changes the bytecode of the mocked class itself, which is how it can mock final classes and static methods.
Both use Byte Buddy, a library with a fluent Java API for creating and changing classes, which in turn uses ASM, the low-level library that reads and writes class files byte by byte.
4Java Agents: Changing Classes as They Load
A Java agent is a JAR with a premain method, started with -javaagent:agent.jar, or attached to a running JVM. Through java.lang.instrument it registers a ClassFileTransformer, and the JVM then passes the bytes of every class to the transformer as it loads, letting the agent return modified bytes. Observability agents use this to add tracing to HTTP clients and JDBC drivers without code changes, and coverage tools such as JaCoCo add counters to every method.
Since Java 21, loading an agent dynamically into a running JVM prints a warning, as a step towards disallowing it by default (JEP 451). This affects tools that self-attach, including Mockito's inline mock maker, which now recommends configuring it as a -javaagent in the test setup; check your Mockito version's documentation.
5The Upgrade Problem and the Class-File API
ASM, Byte Buddy and CGLIB all parse class files themselves, so each must know every class file version it might meet. When a new JDK is released, its class files carry a new major version, 69 for Java 25, and an older ASM refuses to read them, typically with an IllegalArgumentException such as 'Unsupported class file major version 69'. Every framework that embeds or depends on these libraries then needs a release with an updated copy before it fully supports the new JDK. For years this has been a main reason why teams wait weeks or months before adopting a new Java release.
The Class-File API, finalised in Java 24 by JEP 484 in the package java.lang.classfile, addresses this at the root. It is a standard API for parsing, generating and transforming class files, maintained as part of the JDK, so it always supports exactly the class file version of the JDK it ships with. The JDK itself has been moving its internal uses of ASM to it. Libraries that adopt it can read and write class files for each new release on day one, although they still need to target a JDK that includes the API.
- New JDK releasedclass files carry a new major version
- Does the framework's ASM know the version?YesFramework works on day oneNoUnsupported class file major version
- Wait for ASM, then Byte Buddy, then the frameworkweeks to months
- With the Class-File APIthe JDK's own parser always matches its version
4 steps in order. Play them, or step through them one at a time.
6Follow-up questions
Why does Spring Boot default to CGLIB class proxies instead of JDK interface proxies?
Because class proxies work whether or not the bean implements interfaces, and because injecting a bean by its class type, which is common, fails with a JDK proxy that only implements the interfaces. Defaulting to class proxies avoids those surprises. The cost is that final classes and final methods cannot be proxied, which matters for Kotlin, where classes are final by default and the Spring compiler plugin opens them.
Will the Class-File API replace Byte Buddy and ASM?
Not immediately. Byte Buddy offers a high-level API for proxies and interception that the Class-File API does not try to provide, and libraries must support older JDKs that lack the API. More likely, libraries will use the Class-File API underneath when running on new JDKs, removing the version lag while keeping their higher-level features.
7Conclusion
In this answer, we saw that frameworks generate bytecode to add behaviour around our code: Spring creates JDK dynamic proxies or CGLIB subclasses for transactions and AOP, Hibernate and Mockito use Byte Buddy on top of ASM for lazy-loading proxies, entity enhancement and mocks, and Java agents rewrite classes as they load through java.lang.instrument. Because these libraries parse class files themselves, each new class file version required updates and delayed framework support for new JDKs; the Class-File API, final in Java 24 through JEP 484, gives a JDK-maintained parser and generator that always matches the current version.
Open with
Frameworks generate bytecode because they want to add behaviour around our code without us writing it, and they only know where at startup, from annotations and configuration. Spring does it with proxies: for @Transactional, @Async or aspects it injects a proxy instead of the bean, either a JDK dynamic proxy that implements the bean's interfaces and sends every call to an InvocationHandler, or, by default in Spring Boot, a CGLIB subclass generated at runtime, which shows up as a class name with $$SpringCGLIB$$.
Then, if they want more
That is why final methods and self-calls are not intercepted. Hibernate uses Byte Buddy to create lazy-loading proxies for associations, and can enhance entity classes, usually at build time, for dirty tracking and lazy attributes. Mockito uses Byte Buddy too, and its inline mock maker rewrites the mocked class so it can mock final classes and static methods.
Byte Buddy itself sits on ASM, which reads and writes raw class files. Separately, Java agents use java.lang.instrument to register a ClassFileTransformer that can rewrite every class as it loads, which is how APM and coverage tools work.
The catch is that ASM and friends parse class files themselves, so when a new JDK brings a new major version, older versions fail with Unsupported class file major version, and frameworks wait for updated libraries.
The Class-File API, final in Java 24 via JEP 484, is a JDK-maintained API in java.lang.classfile that always understands the current class file version, so libraries built on it can support new JDKs from day one.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 24, Java 25, Java 21
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Frameworks like Spring, Hibernate and Mockito generate or modify bytecode at runtime....": https://scrivio-interview-library.netlify.app/java/jvm-architecture/714eab6f3f/