Back to Intellij Community

SuspiciousInvocationHandlerImplementation

java/java-impl/resources/inspectionDescriptions/SuspiciousInvocationHandlerImplementation.html

2025.3-rc-2821 B
Original Source

Reports implementations of InvocationHandler 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:

InvocationHandler myHandler = (proxy, method, params) -> {
    System.out.println("Hello World!");
    return null;
  };
  Runnable myProxy = (Runnable) Proxy.newProxyInstance(
    Thread.currentThread().getContextClassLoader(),
    new Class[] {Runnable.class}, myHandler
  );

This code snippet is designed to only proxy the Runnable.run() method. However, calls to any Object methods, like hashCode(), are proxied as well. This can lead to problems like a NullPointerException, for example, when adding myProxy to a HashSet.

New in 2020.2