Name

Proxy

Synopsis

This class defines a simple but powerful API for dynamically generating a proxy class. A proxy class implements a specified list of interfaces and delegates invocations of the methods defined by those interfaces to a separate invocation handler object.

The static getProxyClass( ) method dynamically creates a new Class object that implements each of the interfaces specified in the supplied Class[ ] array. The newly created class is defined in the context of the specified ClassLoader. The Class returned by getProxyClass( ) is a subclass of Proxy. Every class that is dynamically generated by getProxyClass( ) has a single public constructor, which expects a single argument of type InvocationHandler. You can create an instance of the dynamic proxy class by using the Constructor class to invoke this constructor. Or, more simply, you can combine the call to getProxyClass( ) with the constructor call by calling the static newProxyInstance( ) method, which both defines and instantiates a proxy class.

Every instance of a dynamic proxy class has an associated InvocationHandler object. All method calls made on a proxy class are translated into calls to the invoke( ) method of this InvocationHandler object, which can handle the call in any way it sees fit. The static getInvocationHandler( ) method returns the InvocationHandler object for a given proxy object. The static isProxyClass( ) method returns true if a specified Class object is a dynamically generated proxy class. ...

Get Java in a Nutshell, 5th 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.