Reflection and Configuration
Handle dynamic features.
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());
}
}All lessons in this course
- What Is GraalVM
- Building a Native Image
- Reflection and Configuration
- Trade-offs of Native Image