Files
openide/java/java-impl/resources/inspectionDescriptions/SuspiciousInvocationHandlerImplementation.html
2025-02-05 04:43:28 +00:00

25 lines
1.0 KiB
HTML

<html>
<body>
Reports implementations of <code>InvocationHandler</code> that do not proxy standard
<code>Object</code> methods like <code>hashCode()</code>, <code>equals()</code>, and <code>toString()</code>.
<p>Failing to handle these methods might cause unexpected problems upon calling them on a proxy instance.</p>
<p><b>Example:</b></p>
<pre><code>
InvocationHandler myHandler = (proxy, method, params) -&gt; {
System.out.println("Hello World!");
<b>return</b> null;
};
Runnable myProxy = (Runnable) Proxy.newProxyInstance(
Thread.currentThread().getContextClassLoader(),
<b>new</b> Class[] {Runnable.class}, myHandler
);
</code></pre>
<p>
This code snippet is designed to only proxy the <code>Runnable.run()</code> method.
However, calls to any <code>Object</code> methods, like <code>hashCode()</code>, are proxied as well.
This can lead to problems like a <code>NullPointerException</code>, for example, when adding <code>myProxy</code> to a <code>HashSet</code>.
</p>
<!-- tooltip end -->
<p><small>New in 2020.2</small>
</body>
</html>