Reflect 4 Proxy May 2026
import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; public class LoggingHandler implements InvocationHandler private final Object target; // real object
public interface InvocationHandler public Object invoke(Object proxy, Method method, Object[] args) throws Throwable;
| Feature | JDK Proxy | CGLIB | Byte Buddy | |---------|-----------|-------|-------------| | | Interfaces only | Concrete classes | Both | | Implementation | Reflection | Subclassing (bytecode) | Bytecode generation | | Performance | Medium | High | Highest | | Complexity | Low | Medium | High | | Modern use | Spring AOP (default) | Spring (fallback) | Mocking frameworks | reflect 4 proxy
@Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable // Log before execution System.out.println("[LOG] Calling: " + method.getName()); if (args != null) for (int i = 0; i < args.length; i++) System.out.println("[LOG] Arg " + i + ": " + args[i]); // Invoke the real method via reflection Object result = method.invoke(target, args); // Log after execution System.out.println("[LOG] Returned: " + result); return result;
Cache Method objects in a HashMap inside your handler to avoid repeated method.invoke() resolution. 7. Beyond JDK Proxies: CGLIB and Byte Buddy The JDK's reflect 4 proxy has one major limitation: it can only proxy interfaces . If you need to proxy a concrete class (without interfaces), you must use bytecode generation libraries. import java
public LoggingHandler(Object target) this.target = target;
UserService proxy = (UserService) Proxy.newProxyInstance( UserService.class.getClassLoader(), new Class[]UserService.class, new LoggingHandler(realService) ); // Call methods via proxy String name = proxy.getUserName(42); proxy.updateUser(42, "John Doe"); If you need to proxy a concrete class
Enhancer enhancer = new Enhancer(); enhancer.setSuperclass(RealUserService.class); enhancer.setCallback(new MethodInterceptor() public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable // interceptor logic return proxy.invokeSuper(obj, args); ); RealUserService proxy = (RealUserService) enhancer.create(); The reflect 4 proxy mechanism remains a cornerstone of Java’s dynamic capabilities. Although newer versions of Java introduced features like dynamic proxies via MethodHandles (more lightweight) and inline classes (Project Valhalla), java.lang.reflect.Proxy is still widely used because it is simple, standardized, and deeply integrated into major frameworks.
