This howto is inspired by the stackoverflow question: How do I create Custom bug detector for any know Static code review of tool?

PMD already has a rule, that does this check for some default SQL resources like java.sql.Connection. This rule is however customizable, so it can verify also custom resources to prevent resource leaks.

Like in the stackoverflow question, let’s start with the resource, we want to protect. This is the DataService interface:

public interface DataService {
    public void discard();
}

The discard method needs to be called after using the service, otherwise you’ll create a resource leak. That’s similar like closing a stream after usage.

The sample code, that uses the DataService, looks like this (again copied from the stackoverflow question, but slightly modified):

public class Test {
    private static class ServiceProvider {
        public DataService getInternalDataService() { return null; }
    }
    private static ServiceProvider service = new ServiceProvider();

public static void main(String[] args) {
    DataService internalDataService = service.getInternalDataService();
    
    try {
        // do something with the services
        
    } finally {
        // Developer must invoke this method before leaving this block.
        // If he forgets then it should be reported as bug like 'resource leaking or something'. 
        internalDataService.discard();
        }
    }
}

So, now that we know the example, we can start using PMD. PMD already provides the rule CloseResource in the Design ruleset: CloseResource. It can be configured via the properties types and closeTargets. In order to specify the properties, you’ll need to create a custom ruleset as file custom-ruleset.xml:

<?xml version="1.0"?>
<ruleset name="ExampleRuleset"
         xmlns="http://pmd.sourceforge.net/ruleset/2.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://pmd.sourceforge.net/ruleset/2.0.0 http://pmd.sourceforge.net/ruleset_2_0_0.xsd">

    <description>
        discard as close resource target
    </description>

    <rule ref="rulesets/java/design.xml/CloseResource">
        <properties>
            <property name="types" value="DataService"/>
            <property name="closeTargets" value="discard"/>
        </properties>
    </rule>
</ruleset>

This sets the types to be DataService and the closeTargets to be discard. The syntax of this xml file is described at How to make a custom ruleset.

Run PMD on the command line with the following options:

bin/run.sh pmd -d sourceDirectory -f text -R custom-ruleset.xml

And you should see the violations, if you don’t execute discard:

src/Test.java:8:   Ensure that resources like this DataService object are closed after use

That’s all.