Scrivio Interview library
ScenarioIntermediate6 min read · updated 12 November 2026

Your company has an internal library that uses reflection and dynamic proxies and is used by many services that want to build native images. How would you add native support to the library?

I would ship the native metadata inside the library, so every service that depends on it gets native support without doing anything. For Spring users, that means RuntimeHintsRegistrar implementations registered through META-INF/spring/aot.factories; for any consumer, it means GraalVM metadata files under META-INF/native-image/<groupId>/<artifactId>/ in the jar. The hints must cover proxies and resources as well as reflection, the library's own CI must build and run its tests as a native image, and over time we should replace reflection with generated code where we can.

Why this question is asked

Internal libraries are often the reason a native image effort stalls. Each service team finds the same missing reflection entries, copies config files around and then breaks when the library changes. Interviewers ask this scenario to see whether we would fix the problem once, at the source, in a way that stays correct as the library evolves. In this answer, we will see why the library breaks in native images, ship hints in two ways, cover proxies and resources, test the library natively in its own CI, and reduce reflection where possible.

1Why the Library Breaks in a Native Image

Imagine an audit library that services use by annotating methods. At startup it scans for @Audited, reads annotation attributes and fields by reflection, wraps service interfaces in JDK dynamic proxies to record calls, and loads a mapping file from the classpath. On the JVM all of this just works.

A native image is built with a closed-world assumption: only code reachable by static analysis is included, and reflection, dynamic proxies and resources work only if they are declared at build time in reachability metadata. None of the library's dynamic behaviour is visible to that analysis, so each service sees missing classes, a proxy that cannot be created, or a resource that is not found. If every service writes its own metadata, the work is repeated dozens of times and goes stale whenever the library changes.

2Option 1: Spring Hints Registered Through aot.factories

If the library is used from Spring applications, the most natural way is a RuntimeHintsRegistrar inside the library. Spring's AOT processing discovers registrars listed in META-INF/spring/aot.factories in every jar on the classpath, so consuming services need no annotation or import. The registrar is code, so it can compute the list of types, for example from the library's own enum of event types, and it is checked by the compiler when classes are renamed.

Java
// In the library: com.acme.audit.aot.AuditRuntimeHints
public class AuditRuntimeHints implements RuntimeHintsRegistrar {
@Override
public void registerHints(RuntimeHints hints, ClassLoader cl) {
hints.reflection().registerType(AuditRecord.class, MemberCategory.values());
hints.reflection().registerType(Audited.class, MemberCategory.INVOKE_PUBLIC_METHODS); // read attributes
hints.proxies().registerJdkProxy(AuditSink.class); // dynamic proxy interface
hints.resources().registerPattern("acme-audit/mappings/*.yml"); // classpath resources
}
}
# In the library jar: META-INF/spring/aot.factories
org.springframework.aot.hint.RuntimeHintsRegistrar=\
com.acme.audit.aot.AuditRuntimeHints

Notice the proxy and resource registrations. Reflection is the part people remember, but proxies and resources fail the same way and are easy to miss. When the hints depend on the application's own beans, for example proxies for interfaces the application declares and the library discovers, the library can implement a BeanRegistrationAotProcessor or BeanFactoryInitializationAotProcessor, which sees the bean definitions during AOT and contributes hints for exactly those.

3Option 2: GraalVM Metadata in the Jar

If the library is also used outside Spring, or we prefer a framework-neutral approach, we ship GraalVM reachability metadata as resources in the jar under META-INF/native-image/<groupId>/<artifactId>/. native-image reads these files automatically from every jar on the classpath. The format has changed across GraalVM releases (separate reflect-config.json, proxy-config.json and resource-config.json files in older versions, a single reachability-metadata.json in newer ones), so check which your GraalVM version expects. A good way to produce a first version is to run the library's tests on the JVM with the native-image tracing agent and then review and trim the output.

Java
acme-audit-2.4.0.jar
com/acme/audit/... # classes
META-INF/spring/aot.factories # option 1, Spring users
META-INF/native-image/com.acme/acme-audit/
reachability-metadata.json # option 2, any native-image user
{ "reflection": [ { "type": "com.acme.audit.AuditRecord", "allDeclaredFields": true, ... } ],
"resources": [ { "glob": "acme-audit/mappings/*.yml" } ] }
Two ways to ship native support
Aspectaot.factories + registrarMETA-INF/native-image files
ConsumersSpring applicationsany native-image build
FormatJava code, type-checkedJSON, version-specific format
Computed typesyesno, static list
Hints from app beansyes, via AOT processorsno
Created byhand, compiler-checkedtracing agent, then reviewed
Many libraries use the registrar for Spring users and add JSON only if non-Spring users exist.

4Testing the Library Natively in Its Own CI

Hints that are not tested drift. The library's build adds the GraalVM native build tools plugin and a native test run, so its own test suite is compiled into a native executable and executed in CI. The tests must exercise the dynamic paths: create a proxy, read the mapping resource, serialise an AuditRecord. A cheap JVM unit test with RuntimeHintsPredicates also checks that the registrar registers what it should, and gives fast feedback on every commit, while the slower native test can run on the main branch and before each release.

Java
// Library build (Gradle)
plugins { `java-library`; id("org.graalvm.buildtools.native") version "0.10.6" } // check current version
$ ./gradlew test nativeTest
> Task :test 42 tests completed
> Task :nativeTest JUnit Platform on Native Image: 42 tests successful, 0 failed
Step throughRelease pipeline of the library
  1. Change library codemaybe new reflective types
  2. JVM tests + hint predicatesfast, every commit
  3. Native tests pass?
    YesPublish new versionNoAdd or fix hints, rerun
  4. Services upgradenative support comes with the jar

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

5Reducing Reflection at the Source

The best hint is one we do not need. Much of the library's reflection exists to read annotations and copy fields at run time. An annotation processor can do that work at compile time and generate plain Java classes, the way MapStruct generates mappers. Instead of JDK proxies for every interface, the library can generate implementations or ask users to register a small adapter bean. Generated code is visible to static analysis, needs no metadata, starts faster on the JVM too and fails at compile time rather than at run time. This is a larger change, so we do it gradually, starting with the hottest or most fragile reflective paths.

6Follow-up questions

Should the hints live in the library or in each service?

In the library. The library knows what it reflects on, its hints change together with its code, and a single version serves every consumer. Hints copied into services go stale silently when the library is upgraded.

What if the library is used by services on different GraalVM versions?

Code-based Spring hints are translated by each service's own Spring version, so they adapt. Static JSON files are tied to a format, so we test with the oldest and newest GraalVM versions we support, or we generate the JSON in the library build.

How do we know the hints are complete?

We cannot prove it, but native tests that cover every public feature, plus the tracing agent run over the full test suite to compare against our hints, catch most gaps. Reports from consuming services go back into the library's tests.

7Conclusion

In this answer, we added native support to an internal library at the source. We shipped hints in the jar, as a RuntimeHintsRegistrar registered through META-INF/spring/aot.factories for Spring users and as GraalVM metadata under META-INF/native-image/<groupId>/<artifactId>/ for any consumer, covered proxies and resources as well as reflection, ran the library's own tests as a native image in CI, and planned to replace reflection with generated code over time.

What to say in the interviewAbout 80 seconds aloud

Open with

I'd fix it once, in the library, so services get native support just by upgrading the dependency. For Spring consumers, I'd write a RuntimeHintsRegistrar in the library that registers the reflective types, the interfaces it proxies with hints.proxies().registerJdkProxy, and the resources it loads, and list it in META-INF/spring/aot.factories so Spring's AOT step picks it up automatically from the jar.

Then, if they want more

If hints depend on the application's beans, like interfaces discovered by scanning, I'd add an AOT processor that contributes hints for exactly those beans.

If we also have non-Spring users, I'd ship GraalVM metadata in META-INF/native-image/groupId/artifactId inside the jar, generated first with the tracing agent over the library's test suite and then reviewed, keeping in mind the format depends on the GraalVM version.

The important part is testing: the library's CI should run a fast unit test with RuntimeHintsPredicates on every commit, and the full test suite as a native image with nativeTest before release, with tests that actually exercise the proxies, resources and reflection.

Longer term, I'd reduce reflection itself, for example with an annotation processor that generates code at compile time, because generated code needs no metadata and fails at compile time instead of in production.

About this answer

Last updated
12 November 2026
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 has an internal library that uses reflection and dynamic proxies and is...": https://scrivio-interview-library.netlify.app/spring-boot/native-and-aot/1bfa5bcbf7/