Inspectopedia Help

Unsafe call to 'Class.newInstance()'

Reports calls to java.lang.Class.newInstance().

This method propagates exceptions thrown by the no-arguments constructor, including checked exceptions. Usages of this method effectively bypass the compile-time exception checking that would otherwise be performed by the compiler.

A quick-fix is suggested to replace the call with a call to the java.lang.reflect.Constructor.newInstance() method, which avoids this problem by wrapping any exception thrown by the constructor in a (checked) java.lang.reflect.InvocationTargetException.

Example:

clazz.newInstance()

After the quick-fix is applied:

clazz.getConstructor().newInstance();

Locating this inspection

By ID

Can be used to locate inspection in e.g. Qodana configuration files, where you can quickly enable or disable it, or adjust its settings.

ClassNewInstance
Via Settings dialog

Path to the inspection settings via IntelliJ Platform IDE Settings dialog, when you need to adjust inspection settings directly from your IDE.

Settings or Preferences | Editor | Inspections | Java | Probable bugs

Availability

By default bundled with

IntelliJ IDEA 2024.1, Qodana for JVM 2024.1,

Can be installed with plugin

Java, 241.18072

Last modified: 18 June 2024