Search code examples
javaeclipsesonarlint

SonarLint alerting for a java:S1206 issue but hashCode and equals are implemented


I have a small Java project on Eclipse which I like to check code smells using SonarLint, but today I've got this strange java:S1206 issue which says "equals(Object obj)" and "hashCode()" should be overridden in pairs. This issue is being reported on a long-time model object which correctly overrides equals and hashCode methods.

Java class overriding both "hashCode" and "equals".

I've already tried re-run SonarLint a few times, but the results are the same. Last modification I've done is update some dependencies version and imports.

I'm using Eclipse 2019-12 (4.14.0) Build id: 20191212-1212 and SonarLint for Eclipse version 5.0.0.15138. Eclipse is running using JDK 1.8.0_231.

Project code can be found on my ISS-Tracker GitHub repository.


Solution

  • Seems like the problem is regarding the Java virtual machine (JVM) version I was using to execute Eclipse. I modified eclipse.ini file to execute Eclipse using JVM 11 and no issues were reported by SonarLint. To be sure of it, I've removed the configuration, restarted Eclipse, re-executed SonarLint report and the java:S1206 issue reappeared.

    For those who are facing the same problem (or similar, perhaps):

    1. Download a Java Virtual Machine 11;

    2. Install the JVM on your machine and annotate (copy) the installation directory (e. g. /opt/openjdk/jdk-11.0.2);

    3. Open $ECLIPSE_HOME/eclipse.ini file on a text editor;

    4. Add the following lines replacing <JAVA_HOME> by your annotated installation directory;

    -vm
    <JAVA_HOME>/bin
    

    eclipse.ini file should be looking like this:

    eclipse.ini file after JVM configuration lines have been added

    1. Restart Eclipse.

    2. Run SonarLint report again by right-clicking on project folder, menu SonarLint, item Analyze.