The BugPattern element specifies three attributes. Likewise, FindBugs invokes the sawOpcode int method as it analyzes each opcode within the method body. Date formats are not synchronized. So maybe we can verify if we have a multi-threading problem by double-checking the values the formatter returns:. The class annotation is at position 0, and the method annotation is at position 1. So here are the steps: Sign in or register to add and subscribe to comments.

The team decided this practice was the appropriate logging idiom and went about changing the existing code to reflect the new practice. However I remember having seen this message once before, but back then I did not have the time to look into it any‚Ķ. The output looks like this:. 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? I have read this post and if I could I wish to suggest you few interesting things or advice. Nicely formatted time values. The general approach is to write the pattern that you’re looking for in this case write the guard clause in a Java file , save it, and then compile it.

Sharing a single instance across thread boundaries without proper synchronization will result in erratic behavior of the application.

findbugs writing custom detectors

FindBugs, Part 2 Writing custom detectors How to write custom detectors to find application-specific problems. You should then go and download the latest version and run it against you code. Those patterns are divided into categories, such as “Bad practice”, “Correctness”, “Multithreaded correctness”, “Performance” and some more. Check if the field’s type is an object in contrast to a primitive int, for example.

StaticAccess FindBugs detector

We want to make sure that they’re reset before then. Sign in or register to add and subscribe to comments.


In this case, the detector has three bits of state that it’s maintaining:. They contain some meta information about the check like which category it belongs to and the messages that should be printed when the detector finds something. Once we’ve determined that this statement is an if Logger. People’s minds have a somewhat limited capacity, and people have limited time to look for errors – they are needed to get the programming done.

It became evident that the team’s home-grown logging framework like all logging frameworks had grown over time. Or you could write a set of detectors that verify that your J2EE code is following the appropriate restrictions, such as no Thread or Socket creation. Several related detectors may be grouped together by sharing the same abbreviation.

If it is, we report a bug by creating a new bug instance, specifying the type of bug which we’ll discuss in more detail later and its priority. FindBugs, Part 2 https: Anyway, the first thing that I noticed is that there wasn’t really an easy writiing to debug through my detector.

The Worker class in this example simulates some work that takes between 0 and ms. This can be put into the plugins directory of your FindBugs installation.

Another very useful wfiting to use when writing detectors is the perennial javap. Notice the type listed here matches the name used on line 39 of Listing 8. My aim was not to write a detector for detecting System.

java – Adding custom detectors in findbugs – Stack Overflow

The second will be about finding invocations on static instances, which is a little bit trickier. FindBugs can be a useful tool in your arsenal. Calendar or a subclass thereof method calls on static instances of java.


Tracking the problem down will be difficult again. This site uses cookies. This detector was used to find all the places in the code where the message to log was being created outside of the guard clause — also a fairly common problem and one that can be quite expensive if you have fancy toString s.

findbugs writing custom detectors

So it’s clearly good advise not to share the static instance of the formatter across thread boundaries. You might add more test cases to make absolutely sure that there is no bug lurking in this code.

Because the format is going to be the same for all dates you pass to the getFormattedDate method, you employ a static instance of the formatter to save some resources.

Listings 10 and 11 shows the contents of the two XML files:. The team decided this practice was the appropriate logging idiom and went about changing the existing code to reflect the new practice. The team needed a detector that would find the places in their code where exceptions were caught and asked to print their stack traces. The scanning detectors are more work to implement, but they are also more general in the sort of problems they can detect.

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.