Implementing an Interface Using a Proxy

Java 1.3 added the capability to dynamically create a proxy class at runtime that implements one or more interfaces. The Proxy class and the InvocationHandler interface were added to the java.lang.reflect package to support this functionality. With this capability, you can create a proxy that delegates the method calls associated with the interfaces it implements to a class that implements InvocationHandler. This interface defines a single method:

public Object invoke(Object proxy, Method method, Object[] args)
  throws Throwable

You obtain a proxy to work with an InvocationHandler using a static method of the Proxy class:

 public static Object newProxyInstance(ClassLoader loader, Class[] interfaces, InvocationHandler ...

Get Special Edition Using Java 2 Standard Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.