简体   繁体   English

访问自定义声纳法则中的度量(Java检查)

[英]Access to measures in custom sonarqube rule (java check)

Hi I'm writing my own plugin for Sonarqube with custom rule checks. 嗨,我正在用自定义规则检查为Sonarqube编写自己的插件。 The rules should report issues based on some measurements on methods or classes. 规则应基于对方法或类的一些度量来报告问题。 The key of this approach is accessing measures of related metrics. 该方法的关键是访问相关指标的度量。 Here is my sample check class : 这是我的示例检查类:

@Rule(key = "BrainMethod", name = "Brain method"
public class BrainMethod extends BaseTreeVisitor implements JavaFileScanner {
     private JavaFileScannerContext context;
/* (non-Javadoc)
 * @see org.sonar.plugins.java.api.JavaFileScanner#scanFile(org.sonar.plugins.java.api.JavaFileScannerContext)
 */
@Override
public void scanFile(JavaFileScannerContext context) {
    this.context = context;
   //scan the java file tree
    scan(context.getTree());
}

  /* (non-Javadoc)
 * @see org.sonar.plugins.java.api.tree.BaseTreeVisitor#visitMethod(org.sonar.plugins.java.api.tree.MethodTree)
 */
@Override
  public void visitMethod(MethodTree tree) {
    // here is the place where I want to acces to the measurements
    context.reportIssue(this, tree, String.format("Potentional brain method"));
    super.visitMethod(tree);
  }

Is there any way how to get the measured values of metrics such as lines of code, complexity or others in this rule? 在此规则中,有什么方法可以获取度量的测量值,例如代码行,复杂性或其他? Or am I using a wrong approach for doing that? 还是我使用了错误的方法?

No there is no such feature, it's the responsibility of your custom rules to compute what is required. 没有,没有这样的功能,您的自定义规则负责计算所需的内容。 You can find some utility methods like context.getComplexityNodes(methodTree); 您可以找到一些实用程序方法,例如context.getComplexityNodes(methodTree); but in that's a kind of corner case. 但是在那种情况下

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM