Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What static analyzers do you make a point of running on Java code, and why?

I have experimented with several different static analyzers for Java, notably Findbugs and PMD.

I am looking for examples of other static analyzers that may be worth running on Java code.

like image 627
Finbarr Avatar asked May 13 '10 14:05

Finbarr


4 Answers

Next to FindBugs and PMD, there are also Bandera, ESC/Java and JLint. You can find a comparision of them here (PDF). Here's an extract of relevance:

Bug Category - Example                            | ESC | FindBugs | JLint | PMD
--------------------------------------------------+-----+----------+-------+-----
General - Null dereference                        |  V  |    V     |   V   |  V
Concurrency - Possible deadlock                   |  V  |    V     |   V   |  V
Exceptions - Possible unexpexted exception        |  V  |          |       |
Array - Length may be less than zero              |  V  |          |   V   |   
Mathematics - Division by zero                    |  V  |          |   V   |  
Conditional, loop - Unreachable code              |     |    V     |       |  V 
String - Checking equality using == or !=         |     |    V     |   V   |  V
Object overriding - Equal objects/equal hashcodes |     |    V     |   V   |  V
I/O stream - Stream not closed on all paths       |     |    V     |       |  
Unused or duplicate statement - Unused local      |     |    V     |       |  V
Design - Should be a static inner class           |     |    V     |       |  
Unnecessary statement - Unnecessary return        |     |          |       |  V

Note: The article is from 2004. The tools may have been improved in the meanwhile.

As you see, FindBugs and PMD finds pretty much and are also the most popular static analyser tools. However, some points are also covered by a smart IDE nowadays, like null deference, unused locals and unreachable code. Eclipse for example can warn on them.

like image 170
BalusC Avatar answered Nov 15 '22 20:11

BalusC


Findbugs is pretty much the standard, as it's very robust (for a tool that started out of research), regularly maintained, and with recent versions really covers most of the bases. It also has great Eclipse integration and a variety of filtering and sorting options to let you achieve your preferred signal-to-noise ratio.

My only wish is that it could provide a workflow for recommendations, so I could choose to ignore specific instances (e.g., for code I have no control on) and only see changes. Continuous analysis would also be nice, when I have spare cores.

I'm familiar with several very promising research tools that use static analysis for things like checking API conformance or threading analysis. Unfortunately, none of them is truly production-quality and they demand some investment on the side of the developer.

like image 21
Uri Avatar answered Nov 15 '22 22:11

Uri


Theres a list of java static analysis tools here: http://en.wikipedia.org/wiki/List_of_tools_for_static_code_analysis#Java

Findbugs is my personal favourite because of its easy to use interface and comprehensive analysis, plus there are plugins for eclipse and idea.

like image 29
Dimitar Avatar answered Nov 15 '22 21:11

Dimitar


I would suggest you use the code analyser in IntelliJ. It has over 600 checks which are easy to turn on and off, however the main reason is that many checks have quick fixes.

If you just run a report, you might find 1000s or 10,000s of flagged issues. This can be tedious to address when each issue often has very little value but has the real risk that you will introduce a bug. However IntelliJ lets you select and fix 1000s of issues in a matter of minutes, with a much lower risk of introducing a bug.

The IntelliJ CE is open source and free and has this feature.

like image 31
Peter Lawrey Avatar answered Nov 15 '22 20:11

Peter Lawrey