Sonar and Sonarlint report a false positive [S1258] when the JavaFX @FXML
tag is used in the code and nothing is done in the constructor. However, according to the tag documentation:
The @FXML
annotation enables an FXMLLoader
to inject values defined in an FXML file into references in the controller class.
Classes and enums with private members should have a constructor
Code smell
Major
java:S1258Non-abstract classes and enums with non-static, private members should explicitly initialize those members, either in a constructor or with a default value.
Versions used:
- InteliJ: IntelliJ IDEA 2024.1 (Ultimate Edition) Build #IU-241.14494.240
- SonarQube 9.9.2 (build 77730)
- SonarLint plugin for InteliJ: 10.5.0.78339
- JDK 11 (AdoptOpenJDK 11.0.7+10)
- JavaFX 17.0.6
Minimal code sample to reproduce:
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
import java.net.URL;
import java.util.ResourceBundle;
public class Demo implements Initializable
{ //[S1258] SonarLint: Add a constructor to the class, or provide default values.
@FXML
private Label label;
@Override
public void initialize(URL url, ResourceBundle resourceBundle)
{}
}