Post Jobs


Sign up or log in Sign up using Google. Also see the follow-up post about some progress Today I was again facing a log file from a machine that had for some reason not been able to start a temporary MySQL daemon during the night to prepare for a streaming MySQL slave installation. Laudably, they did not simply ignore these exceptions; instead, they asked them to print their stack trace, which is nice when you’re building and debugging your application, but not ideal when you deploy it — especially when there may be thousands of exceptions. Once that is done, some reply is put together and sent back to the client. Sign up using Facebook. So it’s clearly good advise not to share the static instance of the formatter across thread boundaries.

The team needed a better way to identify the places that had been missed. Of course, I failed to remember that FindBugs analyzes class files, and so I would need to provide the location of the target directory. Format classes, before you start getting strange errors. You won’t be surprised to hear that as the deadline approached on this very large project, there were a lot of places in the code that were missed. Notice the type listed here matches the name used on line 39 of Listing 8. For example, you could write a set of detectors that find newbie style problems.

StaticAccess FindBugs detector

Searching code is not very practical, especially if you do it manually. For my first stab, I decided to try creating a method that would take my detector class and a test class file to run the detector against.

The type attribute is a unique identifier that is used for two purposes. So far, so good. Email Required, but never shown. Writing a detector means implementing one or more of those methods, building up an idea about what the class is intended to do.


Calendar or a subclass thereof method calls on static instances of java. Maybe you’re building a framework and need to make sure that all of the classes in a package have a zero-arg constructor or that all fields prefixed with an underscore have a getter but not a setter.

findbugs writing custom detectors

Josh Cummings August 2, at 1: The class annotation is at position 0, and the method annotation is at position 1. Check out BetterVisitor and its subclasses for more detail.

findbugs writing custom detectors

Hi, nice tutorial, but can you explain to me how the findbugs engine decides to invoke a visit X method before or after another visit Y one? For the impatient, you can go grab the latest trunk revision wriitng the FindBugs repository on Google Code.

Writing custom detectors

They contain some meta information about the check like which category it belongs to and the messages that should cutsom printed when the detector finds something. This method does three things:.

This is everything that needs to be coded to implement the detection of static fields of one of the types we are interested in. The vast majority of the detectors are currently written as scanning detectors, although I think that’s just because the developers haven’t had enough time to move many of them over to ByteCodePatternDetector.

The abbrev attribute defines a short abbreviation for the detector. You may in fact be perfectly comfortable that this class is bug free in itself. I understand that visitClassContext or visit JavaClass class is invoked first, but I fetectors understand what determines the invokation of the other visit methods and in what order?

findbugs writing custom detectors

Because I had some problems finding a good howto on writing bug detectors, I decided to write one myself. The problem at hand could be described like this: Now, as we now know that the formatting works, let’s put it to some use: The github project page is at: Recently I wrote about multi-threading problems with java. The Detector’s class attribute should specify the class name of the detector. Today I had to look at a piece of code a colleague had written, using my XPathAccessor class.


First of all, that was pretty fun getting down into the byte code, it made me feel like a Java super-villain. Hi, Unfortunately it wasn’t available for you at the time, but I have created a small library utility which allows test-driving a FindBugs custom detector.

I created an interesting variant of the unguarded logging detector. In the meantime, I hope this article has provided you with a clear example of how custom detectors can be implemented and has inspired you to apply these ideas to your team’s unique situation.

FindBugs, Part 2: Writing custom detectors

The code is packaged as a jar which you can writiny to your test classpath to assert the behaviour of a new detector in a unit test without having to run the whole FindBugs executable to see the results.

Especially when developers started to get familiar with the audio and video capabilities of the machine, RAM became a limiting factor in what you could do. To find out more, including how to control cookies, see here: This description is used by the UI, as shown in Figure When reading through the code for the out-of-the-box detectors, one of the things that will jump out is the focus on whether the detector needs to build up state during the analysis.