Application service assigned to a static final field/property
Reports assignments of application services to static final fields/properties.
Such assignments contribute to global state and make it impossible to tear down an application and set up another one in tests, therefore, repeated tests in the same process may fail. The only exception is an explicit constructor call to store dummy/default instances.
The recommended way to avoid storing services is to retrieve a service locally or to wrap it in java.util.function.Supplier
.
Example:
// Incorrect way
private static final ManagingFS ourInstance = ApplicationManager.getApplication().getService(ManagingFS.class);
// Correct way
private static final Supplier<ManagingFS> ourInstance = CachedSingletonsRegistry.lazy(() -> {
return ApplicationManager.getApplication().getService(ManagingFS.class);
});
// Exception
private static final UniqueVFilePathBuilder DUMMY_BUILDER = new UniqueVFilePathBuilder()
New in 2023.2
Inspection Details | |
---|---|
Available in: | IntelliJ IDEA 2023.3, Qodana for JVM 2023.3 |
Plugin: | Plugin DevKit, 233.SNAPSHOT |
Last modified: 13 July 2023