创建自定义检查样式

发布于 2024-11-18 19:15:43 字数 1099 浏览 0 评论 0原文

我还有另一个关于如何安装和运行自定义 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

余罪 2024-11-25 19:15:43

您要查找的是用于检查包名称的,您应该使用类似于以下的代码:

@Override
public void visitToken(DetailAST aAST)
{
    final DetailAST nameAST = aAST.getLastChild().getPreviousSibling();
    final FullIdent full = FullIdent.createFullIdent(nameAST);
    final String package = full.getText();


    // do some fancy stuff with package name
}

更多详细信息请参考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:

@Override
public void visitToken(DetailAST aAST)
{
    final DetailAST nameAST = aAST.getLastChild().getPreviousSibling();
    final FullIdent full = FullIdent.createFullIdent(nameAST);
    final String package = full.getText();


    // do some fancy stuff with package name
}

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文