I have another StackOverflow question on how to install and run a custom checkstyle. I’ve learned how to do this, and I will update that answer shortly with detailed instructions. Now I am having trouble customizing my check. Below is my code. The problem is I would like to see the fully qualified package as a string (e.g com.amir.foo) – but instead when I run getText() or just toString(), I get some obscure result ([checkstyle] package set to : ANNOTATIONS). Does anyone know how to work with this to achieve the desired results?
import com.puppycrawl.tools.checkstyle.api.*;
public class MyCheck extends Check
{
FullIdent packageDeclaration;
public int[] getDefaultTokens() {
return new int[]{TokenTypes.PACKAGE_DEF};
}
public void visitToken(DetailAST ast)
{
switch(ast.getType()) {
case TokenTypes.PACKAGE_DEF:
System.out.println("got package!");
visitPackage(ast);
break;
default:
System.out.println("naughty!");
}
}
private void visitPackage(DetailAST pack) {
packageDeclaration = FullIdent.createFullIdentBelow(pack);
System.out.println("package set to : " +packageDeclaration);
}
}
What your looking for is used by the check for package names, you should use a code similar to the following:
For more details please refer to the source code of PackageNameCheck:
http://checkstyle.hg.sourceforge.net/hgweb/checkstyle/checkstyle/file/cd352660c53a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/naming/PackageNameCheck.java