Inspectopedia
 
2024.3

Application service assigned to a static final field or immutable property

Warning
Reliability
New
Last modified: 03 December 2024

Reports assignments of application services to static final fields / immutable properties.

Note: Hereinafter, static in Kotlin refers to members of non-anonymous objects or top-level declarations.

Such services' 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. Alternatively, one can wrap it in java.util.function.Supplier (Java, Kotlin) or convert the property to a function (Kotlin).

Example (Java):

While services' assignments to properties without backing fields don't cause the aforementioned problem, using an explicit getInstance() method to retrieve a service is preferred over using a property:

  • It makes it clearer on the call site that it can involve loading the service, which might not be cheap.

  • Loading the service can throw an exception, and having an exception thrown by a method call is less surprising than if it was caused by property access.

  • (Over-)using properties may be error-prone in a way that it might be accidentally changed to a property with an initializer instead of the correct (but more verbose) property with a getter, and that change can easily be overlooked.

  • Using the method instead of a property keeps MyApplicationService.getInstance() calls consistent when used both in Kotlin, and Java.

  • Using the method keeps MyApplicationService.getInstance() consistent with MyProjectService.getInstance(project), both on the declaration and call sites.

For better tooling performance, it is always advised to keep an explicit method return type.

Example:

New in 2023.3