InvocationHandler.invoke
that do not proxy standard
Object
methods like hashCode()
, equals()
, and toString().
Failing to handle these methods might cause unexpected problems upon calling them on a proxy instance.
Example:
Runnable myProxy = (Runnable) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),
new Class[] {Runnable.class}, (proxy, method, params) -> {
System.out.println("Hello World!");
return null;
});
The code snippet above is designed to only proxy the Runnable.run()
method. However, the calls to Object’s
virtual methods are dispatched as well, which may lead to problems like NullPointerException
on trying
to add myProxy
to a HashSet
.
New in 2020.2