1 回答
TA贡献1827条经验 获得超4个赞
为什么不检查Kind父母的?对于实例变量,它应该是一个CLASS.
BLABLA在实例变量中禁止字符串的工作规则看起来像这样。
@Rule(key = "Banned Keyword Rule")
public class BannedKeywordRule extends IssuableSubscriptionVisitor {
// Define the word to ban
private static final String BANNED_KEYWORD = "BLABLA";
@Override
public List<Tree.Kind> nodesToVisit() {
// visit only the variables
return ImmutableList.of(Tree.Kind.VARIABLE);
}
@Override
public void visitNode(Tree tree) {
VariableTree variableTree = (VariableTree) tree;
// check if parent is CLASS aka variable is instance
if(variableTree.parent().is(Tree.Kind.CLASS) && variableTree.simpleName().name().contains(BANNED_KEYWORD)) {
reportIssue(variableTree, "String " + BANNED_KEYWORD + " can not be used as a instance variable.");
}
}
}
添加回答
举报