Checkstyle类检查

发布于 2024-12-18 13:17:34 字数 1281 浏览 1 评论 0原文

我想使用 Checkstyle 编写一个检查,确定给定的类是否扩展了其他类以及是否有“==”的任何用法。有人可以帮助我了解我需要做什么吗?

这是我到目前为止所拥有的:

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

public final class MyEqualsCheck
        extends Check {

    @Override
    public int[] getDefaultTokens() {
        return new int[] { TokenTypes.EQUAL, TokenTypes.NOT_EQUAL };
    }

    /**
     * Right before we visit the AST, we're allowed to do initialization and setup 
     * here.
     */
    @Override
    public void beginTree(final DetailAST aRootAST) {
    }

    /**
     * Each token that we defined above is visited here.
     */
    @Override
    public void visitToken(final DetailAST ast) {

        final DetailAST lValue = ast.getFirstChild();
        final DetailAST rValue = lValue.getNextSibling();
        //is it possible to determine if lValue and rValue implement a given interface directly or indirectly?
    }

    private DetailAST getLowestParent(final DetailAST aAST) {
        DetailAST lowestParent = aAST;
        while ((lowestParent.getParent() != null)) {
            lowestParent = lowestParent.getParent();
        }
        return lowestParent;
    }

}

I would like to write a Check using Checkstyle that determines if the given class extends some other class AND if there are any usages of '=='. Can someone help me understand what I need to do ?

Here's what I have so far :

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

public final class MyEqualsCheck
        extends Check {

    @Override
    public int[] getDefaultTokens() {
        return new int[] { TokenTypes.EQUAL, TokenTypes.NOT_EQUAL };
    }

    /**
     * Right before we visit the AST, we're allowed to do initialization and setup 
     * here.
     */
    @Override
    public void beginTree(final DetailAST aRootAST) {
    }

    /**
     * Each token that we defined above is visited here.
     */
    @Override
    public void visitToken(final DetailAST ast) {

        final DetailAST lValue = ast.getFirstChild();
        final DetailAST rValue = lValue.getNextSibling();
        //is it possible to determine if lValue and rValue implement a given interface directly or indirectly?
    }

    private DetailAST getLowestParent(final DetailAST aAST) {
        DetailAST lowestParent = aAST;
        while ((lowestParent.getParent() != null)) {
            lowestParent = lowestParent.getParent();
        }
        return lowestParent;
    }

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

梦毁影碎の 2024-12-25 13:17:34

这作为一个开始看起来不错。对于 ==,请查看类 StringEqualsEqualityCheck,检查两个字符串文字之间 == 的使用情况。

为了进行检查,您需要验证您的类是否没有使用 ==。这意味着你的visitToken只会记录你是否扩展了另一个类以及是否使用==。
然后,使用 finishTree 来实际引发错误。

有关详细示例,请参阅 EqualsHashCodeCheck,它检查一个类以查看您是否同时实现了 equals() 和哈希码()。请注意,它使用映射正确管理同一文件中的多个类。

This looks good as a start. For the ==, look at the class StringEqualsEqualityCheck, which checks for usage of == between two string literals.

For your check, you need to verify that for your class, you don't use a ==. This means that your visitToken will just record whether you've extended another class and whether you use ==.
Then, use finishTree to actually raise the error.

For a detailed example, see EqualsHashCodeCheck, which examines a class to see if you have implemented both equals() and hashCode(). Note that it manages multiple classes in the same file correctly, using a map.

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