CompositeDataInvocationHandler
, EventHandler
, MBeanServerInvocationHandler
, RemoteObjectInvocationHandler
public interface InvocationHandler
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.
Modifier and Type | Method | Description |
---|---|---|
Object |
invoke |
Processes a method invocation on a proxy instance and returns the result. |
static Object |
invokeDefault |
Invokes the specified default method on the given proxy instance with the given parameters. |
Object invoke(Object proxy, Method method, Object[] args) throws Throwable
proxy
- the proxy instance that the method was invoked onmethod
- the 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.args
- an array of objects containing the values of the arguments passed in the method invocation on the proxy instance, or 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
.null
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.Throwable
- the exception to throw from the method invocation on the proxy instance. The exception's type must be assignable either to any of the exception types declared in the throws
clause of the interface method or to the unchecked exception types java.lang.RuntimeException
or java.lang.Error
. If a checked exception is thrown by this method that is not assignable to any of the exception types declared in the throws
clause of the interface method, then an UndeclaredThrowableException
containing the exception that was thrown by this method will be thrown by the method invocation on the proxy instance.static Object invokeDefault(Object proxy, Method method, Object... args) throws Throwable
proxy
instance with the given parameters. The given method
must be a default method declared in a proxy interface of the proxy
's class or inherited from its superinterface directly or indirectly. Invoking this method behaves as if invokespecial
instruction executed from the proxy class, targeting the default method in a proxy interface. This is equivalent to the invocation: X.super.m(A* a)
where X
is a proxy interface and the call to X.super::m(A*)
is resolved to the given method
.
Examples: interface A
and B
both declare a default implementation of method m
. Interface C
extends A
and inherits the default method m
from its superinterface A
.
The following creates a proxy instance that implementsinterface A { default T m(A a) { return t1; } } interface B { default T m(A a) { return t2; } } interface C extends A {}
A
and invokes the default method A::m
. If a proxy instance implements bothObject proxy = Proxy.newProxyInstance(loader, new Class<?>[] { A.class }, (o, m, params) -> { if (m.isDefault()) { // if it's a default method, invoke it return InvocationHandler.invokeDefault(o, m, params); } });
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 implementsObject proxy = Proxy.newProxyInstance(loader, new Class<?>[] { A.class, B.class }, (o, m, params) -> { if (m.getName().equals("m")) { // invoke B::m instead of A::m Method bMethod = B.class.getMethod(m.getName(), m.getParameterTypes()); return InvocationHandler.invokeDefault(o, bMethod, params); } });
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 methodObject proxy = Proxy.newProxyInstance(loader, new Class<?>[] { C.class }, (o, m, params) -> { if (m.isDefault()) { // behaves as if calling C.super.m(params) return InvocationHandler.invokeDefault(o, m, params); } });
"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 invokeDefault
to invoke a default method. For example, if C
is modified to implement a default method m
:
The code above that creates proxy instanceinterface C extends A { default T m(A a) { return t3; } }
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 C
and the invocation handler calls the invokeDefault
method to invoke A::m
:
The above code runs successfully with the old version ofC c = (C) Proxy.newProxyInstance(loader, new Class<?>[] { C.class }, (o, m, params) -> { if (m.getName().equals("m")) { // IllegalArgumentException thrown as {@code A::m} is not a method // inherited from its proxy interface C Method aMethod = A.class.getMethod(m.getName(), m.getParameterTypes()); return InvocationHandler.invokeDefault(o, aMethod params); } }); c.m(...);
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.proxy
parameter is of type Object
rather than Proxy
to make it easy for InvocationHandler::invoke
implementation to call directly without the need of casting.proxy
- the Proxy
instance on which the default method to be invokedmethod
- the Method
instance corresponding to a default method declared in a proxy interface of the proxy class or inherited from its superinterface directly or indirectlyargs
- the parameters used for the method invocation; can be null
if the number of formal parameters required by the method is zero.IllegalArgumentException
- if any of the following conditions is true
: proxy
is not a proxy instance; ormethod
is not a default method declared in a proxy interface of the proxy class and not inherited from any of its superinterfaces; ormethod
is overridden directly or indirectly by the proxy interfaces and the method reference to the named method never resolves to the given method
; orargs
array does not match the number of parameters of the method to be invoked; orargs
elements fails the unboxing conversion if the corresponding method parameter type is a primitive type; or if, after possible unboxing, any of the args
elements cannot be assigned to the corresponding method parameter type.IllegalAccessException
- if the declaring class of the specified default method is inaccessible to the caller classNullPointerException
- if proxy
or method
is null
Throwable
- anything thrown by the default method
© 1993, 2023, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/reflect/InvocationHandler.html