Adding Static Analysis tools
Tags: opensource, java, lint
Published: 2023-11-03 16:21:06

In the open source, using static analysis tools is a good way to help us maintain the quality of our source code. So  this time I tried to add them to my [ConvertTxtToHTML](https://github.com/WangGithub0/ConvertTxtToHtml/tree/main) project. I created a CONTRIBUTING file to help other contributors fix formatting issues, spot suspicious coding constructs, and alert the common errors.

**Step 1: I using the [contributing generator]**(https://generator.contributing.md/) to generate a general contributing md file, and add the basic install steps.


**Step 2: Add a Source Code Formatter**
Since my project using Java, I using [google-java-format](
https://github.com/google/google-java-format) which is a program that reformats Java source code to comply with [Google Java Style](https://google.github.io/styleguide/javaguide.html).


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g0onegiioao4i1oka91i.png)


I can run it in my cmd `java -jar google-java-format-1.18.1-all-deps.jar -r ./src/application/*`, and it automatically format and overwrite my java file, here is what I got. There were many lines had been revised, most of them are caused by space.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w00jzl3x5qrvl3avstbo.png)



**Step 3: Add a Linter**
At first, I wanted to add [spotBugs](https://github.com/spotbugs/spotbugs) which is popular used. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ynr81dhv8m0fsovefgzq.png)

But I couldn't find how to run it in cmd in it's github README, so I read it [official website](https://spotbugs.github.io/), still can't find the detail install cmd. At last, I found it's [doc](https://spotbugs.readthedocs.io/en/stable/installing.html) and know I can add the `textui` to run the command line user interface.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/50q9g1td7wuiexk4j3dr.png)

After setting all of these, I can run and get the version, but I met the error "Resource not found: java/lang/Object.class". I found a same error reported before on [github](https://github.com/find-sec-bugs/find-sec-bugs/issues/365), but the resolution didn't work for me...
I have to search other java linter, I searched and many tools only support the IDE. at last, I tried to use [pmd](https://github.com/pmd/pmd) which has a fantastic live demo, I did according to the demo and figure it out.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nbwtt8ryyl0avx5obrz6.png)


At last, I got my pmd result by using `./pmd-bin-7.0.0-rc4/bin/pmd check -R quickstart.xml -d src/ConvertTxtMdToHtml.java -f text --cache pmd.cache --report-file jdk-report.txt` and got the result:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f5nz43sjaali1lxnpd8u.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6e5cy2cf1mn78w8dxy3w.png)

I got many errors which I didn't know before. I corrected my errors:
 

- Position literals first in String comparison：
False: `if (args.length == 0 || args[0].equals("-h") || args[0].equals("--help")) {
    // Code implementation
}`
True: `if (args.length == 0 || "-h".equals(args[0]) || "--help".equals(args[0])) {
    // Code implementation
}`

- ControlStatementBraces: This statement should have braces
False:`if (fileName.endsWith(".md")) title = convertLinks(title); `
True:`if (fileName.endsWith(".md")) {
    title = convertLinks(title); // convert links in MD file
}
`

- UnnecessaryLocalBeforeReturn:   Consider simply returning the val
False:`String newLine = line.replaceAll("---", "<hr>");
return newLine;`
True:`return line.replaceAll("---", "<hr>");`


**Step 4: Editor/IDE Integration**
In order to using formatter and linter while we are writing code, I integrate them with my vscode, so I created `.vscode` folder and add the Java formatting and linting according to the [vscode doc](https://code.visualstudio.com/docs/java/java-linting)

At last, I have a stronger [ConvertTxtToHTML](https://github.com/WangGithub0/ConvertTxtToHtml/tree/main) project now!