Sonarqube Custom Rule- String Literal 不应该重复,在记录器的上下文中被忽略

Sonarqube Custom Rule- String Literal should not be duplicated, ignored in context of logger

尝试扩展下面链接的 Sonarqube 规则以忽略记录器方法中字符串文字的出现。

我在尝试提取方法的方法名称时遇到问题(在我的分析中,在基本访问者树的上下文中可能不属于方法的范围。但是幸运地查看 methodInvocation 类型以提取一些方法名字)。

所以我的问题是有没有人有基本访问者树元素的定义列表以及它如何看待不同的陈述?

例如weeLogger.Log(例外,"exception occurred");

例如logger(exception1, "异常发生);

还有没有人做过类似的事情并分享他们如何从 Base Visitor Tree class 中提取方法名称以使用 Sonarqube 进行分析?

https://github.com/SonarSource/sonar-java/blob/master/java-checks/src/main/java/org/sonar/java/checks/StringLiteralDuplicatedCheck.java

获取方法名

    public class SomeClass extends IssuableSubscriptionVisitor {
      @Override
      public List<Tree.Kind> nodesToVisit() {
        return ImmutableList.of(Tree.Kind.METHOD);
      }

      @Override
      public void visitNode(Tree tree) {
        MethodTree methodTree = (MethodTree) tree;
        IdentifierTree methodName = methodTree.simpleName();
       // getName from methodName. 

      }

**get invocation method name**
public class SomeClass extends IssuableSubscriptionVisitor {

    public static IdentifierTree methodName(MethodInvocationTree mit) {
        IdentifierTree id;
        if (mit.methodSelect().is(Tree.Kind.IDENTIFIER)) {
            id = (IdentifierTree) mit.methodSelect();
        } else {
            id = ((MemberSelectExpressionTree) mit.methodSelect()).identifier();
        }
        return id;
    }
@Override
public void visitMethodInvocation(MethodInvocationTree tree) {
    IdentifierTree id;
    if (tree.methodSelect().is(Tree.Kind.IDENTIFIER)){
        id = (IdentifierTree) tree.methodSelect();
    } else {
        id = ((MemberSelectExpressionTree) tree.methodSelect()).identifier();
    }

    if(id.name().matches("(.*)[lL]og(.*)")){
        //Do nothing -> Ignores method with the "log" in them for scanning
    }else {
        scan(tree.methodSelect());
        scan(tree.typeArguments());
        scan(tree.arguments());
    }
}