InvocationHandler
is the interface implemented by
the invocation handler of a proxy instance.
Each proxy instance has an associated invocation handler.
When a method is invoked on a proxy instance, the method
invocation is encoded and dispatched to the invoke
method of its invocation handler.
Proxy
Modifier and Type | Method and Description |
---|---|
public Object | Returns: the value to return from the method invocation on the proxy instance. If the declared return type of the interface method is a primitive type, then the value returned by this method must be an instance of the corresponding primitive wrapper class; otherwise, it must be a type assignable to the declared return type. If the value returned by this method isnull and the interface method's return type is
primitive, then a NullPointerException will be
thrown by the method invocation on the proxy instance. If the
value returned by this method is otherwise not compatible with
the interface method's declared return type as described above,
a ClassCastException will be thrown by the method
invocation on the proxy instance.the proxy instance that the method was invoked on proxy, Method the method, Object[] Method instance corresponding to
the interface method invoked on the proxy instance. The declaring
class of the Method object will be the interface that
the method was declared in, which may be a superinterface of the
proxy interface that the proxy class inherits the method through.an array of objects containing the values of the
arguments passed in the method invocation on the proxy instance,
or args)null if interface method takes no arguments.
Arguments of primitive types are wrapped in instances of the
appropriate primitive wrapper class, such as
java.lang.Integer or java.lang.Boolean .Processes a method invocation on a proxy instance and returns the result. |
public static Object | Returns: the value returned from the method invocationthe proxy, Method Proxy instance on which the default method to be invokedthe method, Object... Method instance corresponding to a default method
declared in a proxy interface of the proxy class or inherited
from its superinterface directly or indirectlythe parameters used for the method invocation; can be args)null
if the number of formal parameters required by the method is zero.Invokes the specified default method on the given |
invoke | back to summary |
---|---|
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable Processes a method invocation on a proxy instance and returns the result. This method will be invoked on an invocation handler when a method is invoked on a proxy instance that it is associated with.
|
invokeDefault | back to summary |
---|---|
public static Object invokeDefault(Object proxy, Method method, Object... args) throws Throwable Invokes the specified default method on the given
Invoking this method behaves as if
Examples: interface The following creates a proxy instance that implements A
and invokes the default method A::m .
If a proxy instance implements both A and B , both
of which provides the default implementation of method m ,
the invocation handler can dispatch the method invocation to
A::m or B::m via the invokeDefault method.
For example, the following code delegates the method invocation
to B::m .
If a proxy instance implements C that inherits the default
method m from its superinterface A , then
the interface method invocation on "m" is dispatched to
the invocation handler's invoke
method with the Method object argument representing the
default method A::m .
The invocation of method "m" on this proxy will behave
as if C.super::m is called and that is resolved to invoking
A::m .
Adding a default method, or changing a method from abstract to default
may cause an exception if an existing code attempts to call The code above that creates proxy instance proxy with
the modified C will run with no exception and it will result in
calling C::m instead of A::m .
The following is another example that creates a proxy instance of The above code runs successfully with the old version of C and
A::m is invoked. When running with the new version of C ,
the above code will fail with IllegalArgumentException because
C overrides the implementation of the same method and
A::m is not accessible by a proxy instance.
API Note The
|