简体   繁体   中英

Access to measures in custom sonarqube rule (java check)

Hi I'm writing my own plugin for Sonarqube with custom rule checks. 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); but in that's a kind of corner case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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