Class.newInstance() to constructor reflection
Replace deprecated Class.newInstance() with explicit constructor lookup and invocation.
Plugin plugin = pluginClass.newInstance();
Plugin plugin = pluginClass
.getDeclaredConstructor()
.newInstance();
Supported API
Removes use of deprecated Class.newInstance().
Explicit constructor
The requested signature is visible and can accept arguments.
Honest failures
Constructor exceptions are represented through InvocationTargetException.
Recommended since Class.newInstance() was deprecated in JDK 9 (September 2017).
Class.newInstance() is deprecated because it can bypass compile-time checking for exceptions thrown by the constructor and only supports a zero-argument constructor. Explicit Constructor lookup makes the selected signature visible and reports reflective invocation failures through the appropriate reflection exceptions. Module and access rules still apply; this pattern does not recommend bypassing encapsulation.