Name

InvocationHandler

Synopsis

This interface defines a single invoke( ) method that is called whenever a method is invoked on a dynamically created Proxy object. Every Proxy object has an associated InvocationHandler object that is specified when the Proxy is instantiated. All method invocations on the proxy object are translated into calls to the invoke( ) method of the InvocationHandler.

The first argument to invoke( ) is the Proxy object through which the method was invoked. The second argument is a Method object that represents the method that was invoked. Call the getDeclaringClass( ) method of this Method object to determine the interface in which the method was declared. This may be a superinterface of one of the specified interfaces or even java.lang.Object when the method invoked is toString( ), hashCode( ), or one of the other Object methods. The third argument to invoke( ) is the array of method arguments. Any primitive type arguments are wrapped in their corresponding object wrappers (e.g., Boolean, Integer, Double).

The value returned by invoke( ) becomes the return value of the proxy object method invocation and must be of an appropriate type. If the proxy object method returns a primitive type, invoke( ) should return an instance of the corresponding wrapper class. invoke( ) can throw any unchecked (i.e., runtime) exceptions or any checked exceptions declared by the proxy object method. If invoke( ) throws a checked exception that is not declared by the proxy object, ...

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.