Reflection, proxies and method handles
Definition:
A dynamic proxy redirects interface method calls to an invocation handler.
Explanation:
A JDK dynamic proxy implements interfaces and invokes InvocationHandler for calls.
Illustration image:
Example:
var proxy = (Runnable) java.lang.reflect.Proxy.newProxyInstance(
Runnable.class.getClassLoader(), new Class<?>[]{Runnable.class},
(receiver, method, args) -> { System.out.println(method.getName()); return null; });
proxy.run();Expected behavior. Prints run. A real handler should define behavior for equals, hashCode and toString too.
Internal working:
A handler can inspect Method and arguments, but must explicitly call the target to continue. Method handles can represent typed invocations with a lookup-derived access right. Reflection access and performance depend on release; measure actual paths.
Interview questions:
Does a proxy automatically call the implementation?
No. The handler must explicitly delegate or implement the operation itself.