Welcome!

AJAX & REA Authors: John Funnell, Bob Little, Kevin Hoffman, Maureen O'Gara, Onkar Singh

Related Topics: Java, Open Source

Java: Article

JavaOne 2008: Uncommon Java Bugs

Detecting them with FOSS tools

Acquiring multiple locks is not a recommended programming practice. However, it’s often required in practice, so when we need to acquire multiple locks, we should ensure that we acquire them in the same order in the code.

Alternatively, we can consider using non-blocking locks when we attempt to acquire multiple locks. The tryLock method in the java.util.concurrent.locks.Lock interface provides this ability. It’s also recommended to release locks quickly and not hold the locks for a long time; so, it’s not recommended to use sleep/wait methods after acquiring a lock; consider using the wait/notify mechanism instead to avoid deadlocks because of holding a lock for a long time waiting for a condition to occur.

The QJ-Pro tool checks for problems like conformance to coding standards, coding best practices, misuse of features, and APIs. It gives lots of violations by default, so you’d have to spend some time selecting the list of rules you want to run for your project. It works on Java source files and is easy-to-use in its standalone GUI version (shown in Figure 5). You can use its plug-ins with popular IDEs like Eclipse, JBuilder, and JDeveloper or use it as an Ant job. You can get QJ-Pro from http://qjpro.sourceforge.net/.

Other Tools
In addition to the four tools covered here – Jlint, FindBugs, PMD, and QJ-Pro – there are many other FOSS tools available. For example, CheckStyle checks for adherence to coding standards such as Sun’s. You can get it from http://checkstyle.sourceforge.net/. JCSC (Java Coding Style Checker) checks for coding style adherence and for common bugs. You can get it at http://jcsc.sourceforge.net/. There are many more useful tools like Classycle, Condenser, DoctorJ, and JarAnalyzer. More information and links on Java tools is provided in the Resource section.

Conclusion
We discussed four specific static analysis tools that can be used to detect not-so-common defects in code. They are free, easy-to-integrate with IDEs, and easy-to-use. It’s highly recommended to use such tools to improve the quality of the software by detecting and fixing bugs early in the software life cycle.

Resources

  • If you’re interested in a list of the available Java FOSS static analysis tools, see http://java-source.net/open-source/code-analyzers.
  • “A Comparison of Bug Finding Tools for Java” by Nick Rutar, Christian B. Almazan, and Jeffrey S. Foster from the University of Maryland provides a detailed technical comparison of Bandera, ESC/Java, FindBugs, JLint and PMD tools. See http://www.cs.umd.edu/~jfoster/papers/issre04.pdf.
  • If you’re using Eclipse, it’s very convenient to use Java tools as plug-ins. Here's the list of available plug-ins for Java.
  • The book Java Puzzlers: Traps, Pitfalls, and Corner Cases by Joshua Bloch and Neal Gafter covers many interesting bugs that can occur in code. Check the link http://www.javapuzzlers.com/.

References
1. Here the word “uncommon” means the kind of defects we talk about are not defects due to usual programming problems like null-pointer access or incorrect casts. The defects we are going to cover are little unusual in that they don’t generally occur in programs, at least, they don’t occur everyday when we program.

2. To err is human: it is only that novices make more mistakes, but experts know common pitfalls, so they write code with fewer defects.

More Stories By S G Ganesh

S G Ganesh is a research engineer in Siemens (Corporate Technology), Bangalore. Prior to Siemens, he worked in Hewlett-Packard for around five years. His area of interest is programming languages and compilers. His latest book is "60 Tips on Object Oriented Programming" (ISBN-13 978-0-07-065670-3) published by Tata McGraw-Hill, New Delhi.

Comments (0)

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.