Over a million developers have joined DZone.

Find your very own bugs


We've talked earlier about static code analysis, a technique used by IntelliJ IDEA as well as some other tools to search through your code looking for well-known bugs. Although the databases of recognized bug patterns of each of the tools are getting bigger and more configurable with every release, sooner or later you're still quite likely to hit the limits.

Human creativity is unbounded and so is the range of possible programmers' errors. Also, company policies, used technologies or target deployment environments might impose additional restrictions on code, violating of which results in improper runtime behavior and so well qualifies for the Bug title. Inevitably, some bugs are specific to the project, problem domain or technology we use.

For example, look at the code below. What do you think, will it work?

        new Thread(new Runnable() {
public void run() {
System.out.println("I'm running!");

The code looks just fine, doesn't it? However, it won't work. It won't work if you intend to deploy in a standard Java EE container or on the Google App Engine, which neither allow your code to create new threads. I'd much prefer getting a friendly early warning about the problem, if I'm violating the rules of my particular deployment environment.

This requirement is however beyond the configurability scope of the inspection engine.

Meet the custom bug patterns

Many of the tools for Static Code Analysis provide ways to allow users to define their own bug patterns. So does IntelliJ IDEA. The ability to define your own bug patterns is based on the Structure Search and Replace capability and hides in the inspection profiles. If you open up the inspection profile, search for the "Structural Search Inspection" inspection rule.

This inspection rule lets you define your custom bug templates. As we've talked earlier, static code analysis works on the Abstract Syntax Tree (AST) representation of your code. When defining the custom bug template using the IDEA's powerful Structural Search and Replace language, you describe the part of the AST, which you consider invalid. Using the same language you can optionally define a quick fix - an AST transformation changing the buggy part of the AST into the desired one.


I'll illustrate the capability on a very simple example. Let's assume we're using a third-party library called FancyLibrary, which provides among other things two quite similar methods:

  • public void buggyMethod(Friend friend)
  • public void correctMethod(String name, int age)

The buggyMethod, as the name suggests, doesn't work correctly and since we cannot fix the method ourselves, having no control over the FancyLibrary source code, we can only do our best to avoid using it. Also, not only we want to eliminate all calls to the buggyMethod from the codebase, in addition we would like to replace all such calls with calls to the correctMethod.

This all can be pretty easily achieved with the Structural Search Inspection. We'll add a new Replace Template first. When defining a new bug template, you may find it useful to pick one of the roughly 80 pre-defined templates.

The method calls template will do just fine for our example, but notice how complex code structure can be searched for as well.

Now it is time to restrict the values of the three variables used in the template - Instance, MethodCall and Parameter, and so we'll pick the Edit variables option.

The Instance variable should be of type "FancyLibrary", the MethodCall variable name should match "buggyMethod" and finally the Parameter variable should be of type "Friend". We're only scratching the surface of the Structural Search and Replace language capabilities. Feel free to find out more about it.

Having defined the invalid code pattern, the time has come to specify the transformation for the intended quick-fix.

We're changing the method name to correctMethod as well as retrieving the name and age properties of the original parameter.

Done! Let's get 'em all!

And we're done. With the template in place, the editor can now warn me, whenever I call the invalid method.

A quick-fix is available to transform the code into a valid one.

And obviously as with all the bug patterns in IntelliJ IDEA, the custom bugs can be also searched for in the on-demand mode, when you get an interactive report summarizing the found bugs, again including the option to quick-fix the problems.

This way you can make sure none of the problems you defined sneak in your codebase in the future.

As for all bugs reported by the IDEA's inspection engine, you can quict-fix multiple occurrences with a single key stroke. Either in the editor's quick-fix pop-up or in the on-demand report choose the Apply Fix to all and get your code back in shape quickly.


Static Code Analysis on top of the usual job to find occurrences of well-known bug patterns can help you find errors specific to your project. Now when you know how to set it, up go ahead and start using it. It can really safe you a lot of pain and effort.

Enjoy finding bugs!



The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}