How to Ensure Correct Behavior for Squid: Rule S1161: The "@Override" annotation must be used for any method
I am developing a Java library targeting JDK5 based applications . The tools used to build applications require JDK7 or more. I am using update version 8:
java version "1.8.0_45"
Java(TM) SE Runtime Environment (build 1.8.0_45-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)
After analysis, I got a lot of false positives in methods implementing the interface specification, for example:
public interface FileScanner {
Collection<File> getFiles(File directory, String[] includes, String[] excludes);
}
(see full sources: https://github.com/gabrysbiz/maven-plugin-utils )
I found that the rule makes decisions based on bytecode (see Jira ticket ). My major version of the class is 49 which is related to JDK5 (see major version numbers )
$ javap -verbose AntFileScanner.class
Classfile /D:/Projects/maven-plugin-utils/sources/plugin-utils/target/classes/biz/gabrys/maven/plugin/util/io/AntFileScanner.class
Last modified 2015-07-16; size 1881 bytes
MD5 checksum 7ea340377469b44df88d5936c2ff4134
Compiled from "AntFileScanner.java"
class biz.gabrys.maven.plugin.util.io.AntFileScanner implements biz.gabrys.maven.plugin.util.io.FileScanner
minor version: 0
major version: 49
flags: ACC_SUPER
I am running analysis using Jenkins 1.619 with SonarQube Plugin 2.2.1. I am using SonarQube 5.1.1 with Java Plugin 3.4.
How can I fix it?
source to share
Some other people have the same problem, as you can see from the link:
http://blog.gmane.org/gmane.comp.java.sonar.general/page=91
I tried to solve using "sonar.java.source" and "sonar.java.target" as suggested:
http://docs.sonarqube.org/display/SONAR/Features+details
But I think it only works for the PMD module now:
http://docs.sonarqube.org/display/PLUG/PMD+Plugin
Then I saw the JIRA ticket. So, there is actually a mistake in this situation. JIRA CR is closed, but the problem still happens on Java 5: https://jira.sonarsource.com/browse/SONARJAVA-249
"We really can't implement this rule right now. @Override annotations have a source delay and are removed by the compiler. So our only chance of detecting this is from ACT. But we can determine if a method is actually overriding another just from bytecode in Thus, this rule depends on which symbol table is available from the checks.
There is a new unresolved CRC to address this issue, https://jira.sonarsource.com/browse/SONARJAVA-818 .
When fixed, it will use the sonar.java.source property I mentioned earlier.
So, to resolve this while the fix is not ready, we have two options:
1) Mark it as "false positive" on the server,
2) Use the @SuppressWarnings annotation using the rule reference (S1161):
http://docs.sonarqube.org/display/PLUG/Java+FAQ
In this case, your code will look like this:
public class FileScannerImpl implements FileScanner {
@SuppressWarnings("squid:S1161")
Collection<File> getFiles(File directory, String[] includes, String[] excludes) {
[...]
}
}
source to share