SonarQube Version: 7.9.1.27448
SonarJava Version: 5.14
Java Version: 1.8
Description:
Variable is set at beginning of a method. The value of the variable will be changed only in listeners.
SonarQube issues will be shown on conditions. The conditions should be changed so that they does not always evaluate to false/true.
Minimum code example:
public class BooleanExpression {
private static boolean checkExpression = true;
public static void main(String[] args) {
// variable must be set else no sonar issue will be show in line 35
checkExpression = false;
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JDialog dialog = new JDialog(frame, "Test Dialog");
dialog.setSize(100, 100);
dialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
// dialog must be modal. Condition should wait until Dialog is closed
dialog.setModalityType(ModalityType.APPLICATION_MODAL);
JButton button = new JButton("set True");
// change value in listener
button.addActionListener(e -> checkExpression = true);
dialog.add(button);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
dialog.setLocationRelativeTo(null);
dialog.setVisible(true);
// Issue: Change this condition so that it does not always evaluate to "false"
if (checkExpression) {
System.out.println("checkExpression is true");
}
}
}