Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eclipse compiler setting to catch == on String

Tags:

java

eclipse

Is there a way to configure eclipse to tag an == comparison on String objects as Errors? I just want a way to prevent developers from using == to compare strings accidentally or unknowingly. I searched through eclipse and could not find anything. Any ideas.

like image 498
Gowtham Avatar asked Feb 18 '23 00:02

Gowtham


2 Answers

There is nothing built into Eclipse that you can enable to do this check, afaik. But, you can incorporate a third party static analysis tool to do it for you. FindBugs does have a rule to check for exactly what you've described, and can be incorporated into Eclipse via plugin.

  • FindBugs
  • FindBugs String Equality Rule
  • FindBugs Eclipse Plugin Guide

Other static analysis tools you might want to take a look at include Checkstyle and PMD.

like image 125
Perception Avatar answered Feb 27 '23 05:02

Perception


Use PMD or Findbugs plugins for eclipse to do this. These tools will also let you catch other common programming mistakes.

Direct link to findbug rule to detect this situation: http://findbugs.sourceforge.net/bugDescriptions.html#ES_COMPARING_STRINGS_WITH_EQ

like image 32
Aaron Saarela Avatar answered Feb 27 '23 04:02

Aaron Saarela