Java code quality fix - String literals should be placed on the left side when checking for equality
We have a huge base of legacy codes for which we are trying to fix all the sonar problems.
There is a problem that says string literals should be placed on the left side when checking for equality. And the score for this problem is 12k +.
testFlag.equalsIgnoreCase("Y") - non-complaint
"Y".equalsIgnoreCase(testFlag) - complaint
I am trying to use the Intelliji IDE Inspection utility to fix this using Force Search Inspection . However, it didn't work.
I saved the search pattern as
$instanceVariable$(equalsIgnoreCase)\(\"$StringLiteral$
and replace the template like
$StringLiteral$(equalsIgnoreCase)\(\"$instanceVariable$
But it won't work, is it right to do it / any better options available in intelliji for this.
I am open to other IDEs like Eclipse. So if there is an option in any open source environment, I can try.
source to share
In Intellij, you will be able to do this with the Analyze options .
Probably the simplest option: do validation by name to restrict validation to only one
Then the required check expression.equals("literal")
, put this in the box that appears and select it, then select the scope to start the check.
Then you can fix each case in each case, or press the equal button to do everything at once.
source to share
I used $Instance$.$MethodCall$("$Parameter$")
both my search pattern and "$Parameter$".$MethodCall$($Instance$)
my replacement pattern. In the "Edit Variables" section you can select MethodCall
on the left and in the "Text Constraints" section, type equalsIgnoreCase
in the "Text / Regular Expression" box.
Verified to work in IDEA Community 2017.1.5.
source to share