Sonarqube 自定义规则 - 字符串文字不应该被复制,在记录器的上下文中被忽略

Jor*_*ith 5 java sonarqube sonarqube-web

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

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

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

例如 weeLogger.Log(exception, "异常发生");

或者

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

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

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

Jos*_*zka 1

获取方法名

    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;
    }
Run Code Online (Sandbox Code Playgroud)