Java / GraalVM Interview questions
What happens when you use Java dynamic proxies with Native Image?
java.lang.reflect.Proxy normally generates a new class implementing given interfaces at runtime, backed by bytecode the JVM synthesizes on the fly - which directly conflicts with Native Image's closed-world, build-time-only class generation model.
To make this work, Native Image needs to know at build time exactly which interface combinations will be proxied, so it can pre-generate the proxy classes during the build rather than at runtime. This is declared in a dedicated configuration file:
[ { "interfaces": ["com.example.PaymentGateway", "java.io.Closeable"] } ]
Placed at META-INF/native-image/proxy-config.json, each entry lists one exact set of interfaces that will be proxied together. If your code calls Proxy.newProxyInstance with an interface combination not listed here, it will fail at runtime with an error, since that combination was never generated during the build.
As with reflection and resource config, the native-image-agent can capture these proxy interface combinations automatically by observing real usage.
More Related questions...