Checkstyle类检查
我想使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这作为一个开始看起来不错。对于 ==,请查看类 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.