Reflection and resource configuration
What you will learn
Reflection, resources, JNI and serialization may depend on choices not statically visible to Native Image. Reachability metadata registers needed program elements or resources, but it does not grant authorization or make arbitrary inputs safe.
- Use finite selection for dynamic behavior.
- Identify reflection and resource metadata needed by a reviewed application.
- Test both successful and missing-resource cases.
Plain Java examples: save as Main.java in a fresh directory, then run javac --release 21 Main.java and java Main. Standalone Java 21 functional examples. GraalVM 25, Spring AOT and HotSpot 25 AOT cache recipes are source-reviewed, not executed. Companion AppCDS evidence is separate.
Invoke a known public method reflectively
public class Main { public static class Formatter { public static String label(String s) { return "Item: "+s; } } public static void main(String[] args) { try { System.out.println(Formatter.class.getMethod("label",String.class).invoke(null,"pen")); } catch(ReflectiveOperationException e) { throw new IllegalStateException(e); } }}Expected output:
Item: penJava 21 reflection succeeds. A native build must preserve the required member where not inferred.
Declare only needed metadata
For GraalVM 25, place reachability-metadata.json under META-INF/native-image/<group>/<artifact>/. The companion includes a specific reflection method and resource glob. Constant calls may be inferred; dynamic ones may need explicit registration. The tracing agent can record observed accesses, but unvisited paths remain untested. Review generated metadata and avoid broad registrations.
Spring hints and initialization
Spring RuntimeHints can register reflection, resource, serialization and proxy needs. Prefer framework-supplied hints and narrow custom hints. Some conditional bean decisions occur during AOT processing, so build profiles and runtime properties cannot always be swapped freely. Keep sensitive or deployment-specific initialization at runtime. Test with missing resources, each allowed dispatch branch, and failure cases.
Distinguish missing resources from empty content
public class Main {
public static void main(String[] args) { try(var in=Main.class.getResourceAsStream("/absent-v52.txt")) { System.out.println(in==null); } catch(java.io.IOException e) { throw new java.io.UncheckedIOException(e); } }}Expected output:
trueMissing classpath resources return null. Do not dereference without checking.
Constrain runtime method selection
public class Main { static String method(String key) { if(!key.equals("label")) throw new IllegalArgumentException("unknown method"); return key; } public static void main(String[] args) { System.out.println(method("label")); }}Expected output:
labelAn allowlist is a security policy distinct from reachability metadata.
Common mistakes
Do not pass untrusted class or method names to reflection. Do not treat registration as access control, copy all resources, or assume an agent run covers all branches.
Practice
Write a resource-name allowlist accepting only catalog.txt; return /catalog.txt and reject other inputs.
Expected output:
/catalog.txtCompare with the manual review criteria.
Further reading: Native metadata · Native agent · Spring native. Independent editorial review remains pending.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Reflection and resource configuration — knowledge check
AOT and deployment reasoning.
Where is unified metadata placed?
What does resource registration do?
What is an agent trace missing?
What can happen with missing registration?
What is safest for dynamic dispatch?