As one of the developers of Checkstyle, I am always interesting in articles about Checkstyle and similar tools like PMD. So I read the recent article Improving Project Quality with PMD with some interest. The comment about Checkstyle not being geared towards "identifying latent defects" was disheartening as this has been a major thrust for well over a year.

So for a bit of fun I thought I would rewrite the example given in the article showing how to find classes that are not declared in a package. The resulting Check for Checkstyle is:

package sample;

import com.puppycrawl.tools.checkstyle.api.Check;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;

public class AllClassesMustHaveAPackage extends Check {
    private boolean mNoPackage;

    public int[] getDefaultTokens() {
        return new int[]{TokenTypes.PACKAGE_DEF};
    }

    public void beginTree(DetailAST root) {
        mNoPackage = true;
    }

    public void visitToken(DetailAST ignored) {
        mNoPackage = false;
    }

    public void finishTree(DetailAST root) {
        if (mNoPackage) {
            log(root, "All types must have a package.");
        }
    }
}

This check will only report of files that do not have a declared package. I suspect the example shown by Tom Wheeler in the article will fire for every class declared in a file. It also will not fire for interfaces. I would be interested to hear from people more skilled in PMD than me if I am wrong.