Scrivio Interview library
DesignAdvanced8 min read · updated 12 November 2026

Your company is considering native images (GraalVM) and AOT for faster startup. Many internal libraries rely on run-time reflection and proxies. What changes do you require and how do you assess cost?

A GraalVM native image is compiled ahead of time under a closed-world assumption, so every class used through reflection, every dynamic proxy and every resource must be declared at build time as reachability metadata. Our libraries must either ship that metadata or move to build-time code generation; Spring AOT handles beans and its own proxies, but our custom reflection still needs RuntimeHints. I would inventory reflective code, build and test each library natively, fix or replace what fails, and weigh startup and memory gains against longer builds and a second runtime mode to test.

Why this question is asked

Fast startup and low memory matter for scale-to-zero services, command-line tools and functions, and native images can turn a three-second start into a fiftieth of that. But the step is not free for a company whose internal libraries were written for a JVM that can load anything at run time. Interviewers ask this design question to see whether we understand why reflection breaks in a native image, what Spring does for us, and how we would decide whether the move is worth it. In this answer, we will recap the closed-world model, look at what breaks, see the kinds of changes libraries need, plan the assessment, and weigh the costs, including a lighter alternative.

1A Quick Recap: Why Native Images Need to Know Everything

A normal JVM loads classes when they are first used, and reflection can ask for any class by name at any moment. A GraalVM native image works differently. At build time, the native-image tool starts from main and follows every reachable call, then compiles only the reachable code into one executable. This is the closed-world assumption: anything the analysis cannot see is left out.

Reflection, dynamic proxies and resource loading are exactly what static analysis cannot see, because the class or file name is often just a string computed at run time. So we must tell the build about them. That information is called reachability metadata, and it lives in JSON files under META-INF/native-image in each JAR (recent GraalVM versions use one reachability-metadata.json; older ones use reflect-config.json, proxy-config.json and resource-config.json, so check your version).

Step throughHow a native image is built
  1. Start from mainand from the declared metadata
  2. Points-to analysisfollow every reachable call
  3. Is this class reached by a call or declared?
    Yesinclude it in the imageNoleave it out
  4. Run build-time initialiserswhere configured
  5. Compile to machine codeone executable, no JIT warm-up

5 steps in order. Play them, or step through them one at a time.

Code reached only through a string name is left out unless metadata names it.

2What Breaks in Our Libraries

The failures show up at run time, not at build time, which is why testing matters so much. A library that loads a plugin class by name gets ClassNotFoundException. One that calls getDeclaredMethods to find handlers gets an empty array or a missing-registration error, depending on the GraalVM version and options. A JDK dynamic proxy for an interface that was not declared fails when created, and a template read from the class path comes back null.

Java
// Works on the JVM, fails in a native image without metadata
Class<?> c = Class.forName(config.get("handler.class")); // name known only at run time
Object h = c.getDeclaredConstructor().newInstance();
Object p = Proxy.newProxyInstance(loader, new Class<?>[]{AuditApi.class}, handler); // proxy must be declared
InputStream in = getClass().getResourceAsStream("/templates/mail.txt"); // resource must be declared

Bytecode generation at run time, as done by CGLIB or ByteBuddy, cannot work at all, because a native image has no class loader that can define new classes. Libraries that rely on it must generate those classes at build time instead.

3Two Ways to Make a Library Native-Friendly

The first way is to declare what the library uses. We add the metadata files to its JAR, written by hand or produced with the GraalVM tracing agent (-agentlib:native-image-agent), which records every reflective access while we run the tests on a normal JVM. Popular open-source libraries often have metadata in the shared GraalVM Reachability Metadata Repository. The agent only sees what the tests exercise, so rarely used paths can still be missing.

The second way is better in the long run: remove the run-time discovery. A library that scans for annotated handlers at startup can use an annotation processor to generate a registry class at compile time. A library that builds proxies can generate the implementation classes during the build. Generated code is visible to the static analysis, needs no metadata, and also starts faster on the normal JVM.

4What Spring AOT Does and Does Not Cover

Spring Boot runs an AOT step (processAot in the build) that starts the application context at build time, evaluates which beans exist, and writes Java source code that registers them directly. It also generates the CGLIB subclasses for @Configuration classes and other proxies ahead of time, and records hints for the reflection Spring itself needs. Because this happens at build time, conditions such as @Profile and @ConditionalOnProperty are fixed when we build, which is a behaviour change worth telling teams about.

But Spring cannot see inside our own code. If a library calls Class.forName or reads fields by name, we must declare it through Spring's RuntimeHints API, which Spring turns into the GraalVM metadata.

Java
public class AuditHints implements RuntimeHintsRegistrar {
@Override public void registerHints(RuntimeHints hints, ClassLoader cl) {
hints.reflection().registerType(AuditEvent.class, MemberCategory.INVOKE_PUBLIC_METHODS,
MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
hints.proxies().registerJdkProxy(AuditApi.class);
hints.resources().registerPattern("templates/*.txt");
}
}
@Configuration
@ImportRuntimeHints(AuditHints.class)
class AuditConfig { }

For types we only serialise with Jackson, @RegisterReflectionForBinding(AuditEvent.class) is a shorter way to say the same thing. Spring also offers a test utility, RuntimeHintsPredicates, so a plain unit test can check that the hints are registered without a native build.

5Assessing the Libraries Step by Step

I would run the assessment as a short programme rather than a big switch. First, inventory: search the internal libraries for Class.forName, getDeclared, Proxy.newProxyInstance, getResource and dependencies on CGLIB, ByteBuddy or Javassist, and list the external dependencies with their native support. Second, build one representative service natively in CI and run its integration tests against the native executable, because that is where missing metadata shows itself. Third, for each failing library, decide whether to add hints, move to code generation, or replace it with a dependency that already supports native images.

The result is a table of libraries with a status for each, which tells us the real cost before we commit any team.

Example library inventory
LibraryRun-time techniqueNative statusAction
audit-clientJDK proxy, Jacksonfails: proxy not declaredadd RuntimeHints
rules-engineClass.forName from configfails at startupgenerate registry at compile time
legacy-cacheCGLIB at run timecannot workreplace with Caffeine-based module
http-commonsplain codeworksnone
Each row is an owner and a decision, not just a test result.

6Weighing the Cost

The gains are large where startup and memory dominate: a service that starts in 50 milliseconds with a resident memory of 80 MB instead of 2 seconds and 300 MB fits scale-to-zero and dense hosting. The costs are just as concrete. A native build can take several minutes and several gigabytes of memory, which slows CI and local feedback. Peak throughput for long-running services may be lower than a warmed-up JIT unless we use profile-guided optimisation, and debugging and profiling tools differ.

The biggest hidden cost is a second runtime mode. Developers run tests on the JVM, but production runs native, so each service needs a native test stage, and a bug can exist in one mode only. For long-running services, a lighter option is the AOT cache from Project Leyden in recent JDKs (JDK 24 and 25), which speeds up startup by reusing class loading and linking from a training run while keeping the normal JVM; check your version for what it covers. I would usually choose native images for a few services where startup is the product requirement, and the JVM, perhaps with the AOT cache, for the rest.

7Follow-up questions

Can the tracing agent produce all the metadata for us?

It produces a good starting point, but only for the code paths the run actually exercised. Error handling, rarely used options and configuration-driven class names are often missing. So the agent output must be reviewed, kept with the library, and backed by tests that run against the native executable.

Why do @Profile and @ConditionalOnProperty behave differently with Spring AOT?

The AOT step evaluates the bean definitions at build time and writes code for the beans it found. A condition that depends on a property or profile is therefore decided during the build, not at startup. Changing the profile at run time does not add beans that were excluded. Teams that switch behaviour per environment with profiles need to build per environment or use run-time configuration values inside beans instead.

How do you keep metadata from going stale?

By treating it as code. The hints live next to the reflective code in the same library, a unit test checks them with RuntimeHintsPredicates, and a CI job builds and tests a sample application natively on every change. When a library adds a new reflective path without a hint, the native test fails before release.

8Conclusion

In this answer, we saw that a native image includes only what its build-time analysis can reach, so reflection, proxies and resources must be declared as reachability metadata. Libraries either ship that metadata, ideally generated and tested, or move to build-time code generation, which also helps the normal JVM. Spring AOT covers beans and Spring's own proxies, but custom reflection still needs RuntimeHints. We assess the move by inventorying reflective code, building and testing natively, and fixing or replacing failing libraries, and we weigh the startup and memory gains against slower builds and a second runtime mode, with the Leyden AOT cache as a lighter alternative.

What to say in the interviewAbout 90 seconds aloud

Open with

A native image is built under a closed-world assumption: the build follows every reachable call from main and leaves out anything it cannot see. Reflection, dynamic proxies and resources loaded by name are invisible to that analysis, so they have to be declared at build time as reachability metadata in META-INF/native-image, and run-time bytecode generation does not work at all.

Then, if they want more

For our libraries that means two kinds of change: either they ship correct metadata, which we can start from the GraalVM tracing agent and then test, or, better, they move discovery to build time with annotation processors and generated code.

Spring AOT helps a lot, because it generates the bean registration code and the configuration proxies at build time, but it cannot see our own Class.forName calls, so those need RuntimeHints registrars, which I would test with RuntimeHintsPredicates.

To assess the cost I would inventory reflective code and bytecode libraries, build one representative service natively in CI, run its integration tests against the executable, and record a decision for each failing library.

Then I would compare the startup and memory gains against build times of several minutes, a second runtime mode that needs its own tests, and possibly lower peak throughput. For many long-running services, the JVM with the Leyden AOT cache may be the better trade.

About this answer

Last updated
12 November 2026
Versions it names
Java 24
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 "Your company is considering native images (GraalVM) and AOT for faster startup. Many...": https://scrivio-interview-library.netlify.app/java/reflection-and-method-handles/b5525a53ce/