Inspectopedia
 
2024.3

Anonymous type has shorter lambda alternative

Warning
New
Last modified: 03 December 2024

Reports anonymous classes which could be transformed to a constructor or a factory method call with a lambda expression argument.

The following classes are reported by this inspection:

  • Anonymous classes extending ThreadLocal which have an initialValue() method (can be replaced with ThreadLocal.withInitial)

  • Anonymous classes extending Thread which have a run() method (can be replaced with new Thread(Runnable)

Example:

After the quick-fix is applied:

This inspection depends on the Java feature 'ThreadLocal.withInitial()', which is available since Java 8.