创建自定义检查样式
我还有另一个关于如何安装和运行自定义 checkstyle 的 StackOverflow 问题。我已经学会了如何做到这一点,我将很快用详细说明更新该答案。现在我在定制支票时遇到了麻烦。下面是我的代码。问题是我希望将完全限定的包视为字符串(例如 com.amir.foo
) - 但当我运行 getText()
或只是 toString()
,我得到一些模糊的结果([checkstyle] 包设置为:注释)。有谁知道如何使用它来达到预期的结果?
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);
}
}
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);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您要查找的是用于检查包名称的,您应该使用类似于以下的代码:
更多详细信息请参考PackageNameCheck的源代码:
http://checkstyle.hg.sourceforge.net/hgweb/checkstyle/checkstyle/file/cd352660c53a/src/checkstyle/com/puppycrawl/tools/checkstyle/checks/naming/PackageNameCheck.java
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