0Pricing
Java Academy · Lesson

Reflection and Configuration

Handle dynamic features.

Reflection and Configuration is a free Java Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Reflection and Configuration

Native-image's closed-world analysis can only see code it can trace statically. Dynamic features — reflection, proxies, resources, and serialization — hide their targets from the analyzer. This lesson explains how to make them work in a native binary.

The Problem with Reflection

When you look up a class by name from a string, the analyzer cannot know which class you mean. If that class is otherwise unreachable, it will be excluded from the image and the lookup fails at runtime.

public class Main {
    public static void main(String[] args) throws Exception {
        String name = "java.util.ArrayList"; // could come from config
        Class<?> c = Class.forName(name);
        System.out.println("Loaded: " + c.getSimpleName());
    }
}

Reachability Metadata

The fix is reachability metadata: JSON files that tell native-image which classes, methods, and fields are accessed dynamically. The build then keeps them and records their members.

reflect-config.json

A reflection entry names the class and which members to retain. This snippet is JSON, not Java — it lives under META-INF/native-image/.

[
  {
    "name": "com.example.Person",
    "allDeclaredConstructors": true,
    "allDeclaredMethods": true,
    "fields": [ { "name": "age" } ]
  }
]

The Tracing Agent

Writing metadata by hand is tedious. Run your app on the JVM with the tracing agent (-agentlib:native-image-agent=config-output-dir=...). It records every dynamic access during a real run and emits the config files automatically.

Exercise Your Code Paths

The agent only records what actually executes, so you must drive the application through all its features — run your tests under it. Any reflective path you miss will be absent from the metadata and break in the native build.

Dynamic Proxies

JDK dynamic proxies need a proxy-config.json listing the interface sets that get proxied. The analyzer cannot infer these because the interface list is assembled at runtime.

Resources

Files loaded through getResourceAsStream are not automatically embedded. A resource-config.json with glob patterns tells native-image which resources to bundle into the executable.

public class Main {
    public static void main(String[] args) {
        var url = Main.class.getResource("/banner.txt");
        System.out.println("Resource present: " + (url != null));
    }
}

Serialization and JNI

Java serialization and native (JNI) calls are also dynamic. Each has its own config file (serialization-config.json, jni-config.json). The tracing agent generates these too when those paths execute.

Programmatic Registration

Library authors can register metadata in code using a Feature via the RuntimeReflection API, executed at build time. This ships correct config inside the library so users need no manual setup.

Shared Metadata Repository

GraalVM maintains a community reachability metadata repository with prebuilt config for popular libraries. The native build tools can pull it automatically, so common frameworks work out of the box.

Quick Check

Test your understanding of dynamic features in native image.

Recap

You learned to handle dynamic features:

  • Reflection, proxies, resources, serialization, and JNI are invisible to static analysis.
  • Reachability metadata JSON files declare these to native-image.
  • The tracing agent auto-generates config from a real JVM run.
  • Exercise all code paths (especially tests) so nothing is missed.
  • Use the shared metadata repository for popular libraries.

Frequently asked questions

Is the “Reflection and Configuration” lesson free?

Yes — the full text of “Reflection and Configuration” is free to read here on the web, and the Java Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.

What will I learn in “Reflection and Configuration”?

Handle dynamic features. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Java Academy?

No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Reflection and Configuration” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Java Academy lesson?

Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. What Is GraalVM
  2. Building a Native Image
  3. Reflection and Configuration
  4. Trade-offs of Native Image
← Back to Java Academy