CompareObjectsWithEquals False Positive comparing boolean (primitive) values
A source code analyzer
Brought to you by:
adangel,
juansotuyo
Using Eclipse PMD plugin 4.0.1-v20130811-0001 and this also happens in Sonar v.3.5.
There is a particular situation in which CompareObjectsWithEquals violation is showing when it shouldn't. To show this I have done some dummy classes that show this clearly.
public class SomeClass { protected SomeObject object; } public class SomeObject { private boolean configuration; public SomeObject() { super(); } public boolean isConfigurationEnabled() { return configuration; } public void setConfiguration(boolean configuration) { this.configuration = configuration; } } public class SonOfSomeClass extends SomeClass { protected javax.swing.JCheckBox checkBox; public class SomeEmbeddedClass { public boolean someNotWorkingMethod(boolean valid){ // This line presents a CompareObjectsWithEquals violation valid |= SonOfSomeClass.this.object.isConfigurationEnabled() != SonOfSomeClass.this.checkBox.isSelected(); return valid; } public boolean someWorkingMethod(boolean valid){ // This line does not present any violation valid |= (SonOfSomeClass.this.object.isConfigurationEnabled()) != SonOfSomeClass.this.checkBox.isSelected(); return valid; } } }
Will be fixed with the next version (5.2.2).