private void saveFilesComplexityDistribution(File sonarFile, SourceFile squidFile) {
   RangeDistributionBuilder complexityDistribution =
       new RangeDistributionBuilder(
           CoreMetrics.FILE_COMPLEXITY_DISTRIBUTION, FILES_DISTRIB_BOTTOM_LIMITS);
   complexityDistribution.add(squidFile.getDouble(FlexMetric.COMPLEXITY));
   context.saveMeasure(
       sonarFile, complexityDistribution.build().setPersistenceMode(PersistenceMode.MEMORY));
 }
Example #2
0
  @Override
  public void scanFile(JavaFileScannerContext context) {
    sonarFile = fs.inputFile(fs.predicates().is(context.getFile()));
    classTrees.clear();
    methods = 0;
    complexityInMethods = 0;
    accessors = 0;
    classes = 0;
    PublicApiChecker publicApiChecker = PublicApiChecker.newInstanceWithAccessorsHandledAsMethods();
    if (separateAccessorsFromMethods) {
      publicApiChecker = PublicApiChecker.newInstanceWithAccessorsSeparatedFromMethods();
    }
    publicApiChecker.scan(context.getTree());
    methodComplexityDistribution =
        new RangeDistributionBuilder(
            CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION, LIMITS_COMPLEXITY_METHODS);
    CommentLinesVisitor commentLinesVisitor = new CommentLinesVisitor();
    commentLinesVisitor.analyzeCommentLines(context.getTree());
    noSonarFilter.addComponent(
        sensorContext.getResource(sonarFile).getEffectiveKey(), commentLinesVisitor.noSonarLines());
    super.scanFile(context);
    // leave file.
    int fileComplexity = context.getComplexityNodes(context.getTree()).size();
    saveMetricOnFile(CoreMetrics.CLASSES, classes);
    saveMetricOnFile(CoreMetrics.FUNCTIONS, methods);
    saveMetricOnFile(CoreMetrics.ACCESSORS, accessors);
    saveMetricOnFile(CoreMetrics.COMPLEXITY_IN_FUNCTIONS, complexityInMethods);
    saveMetricOnFile(CoreMetrics.COMPLEXITY_IN_CLASSES, fileComplexity);
    saveMetricOnFile(CoreMetrics.COMPLEXITY, fileComplexity);
    saveMetricOnFile(CoreMetrics.PUBLIC_API, publicApiChecker.getPublicApi());
    saveMetricOnFile(
        CoreMetrics.PUBLIC_DOCUMENTED_API_DENSITY,
        publicApiChecker.getDocumentedPublicApiDensity());
    saveMetricOnFile(
        CoreMetrics.PUBLIC_UNDOCUMENTED_API, publicApiChecker.getUndocumentedPublicApi());
    saveMetricOnFile(CoreMetrics.COMMENT_LINES, commentLinesVisitor.commentLinesMetric());
    saveMetricOnFile(
        CoreMetrics.STATEMENTS, new StatementVisitor().numberOfStatements(context.getTree()));
    saveMetricOnFile(CoreMetrics.NCLOC, new LinesOfCodeVisitor().linesOfCode(context.getTree()));

    sensorContext.saveMeasure(
        sonarFile,
        methodComplexityDistribution.build(true).setPersistenceMode(PersistenceMode.MEMORY));

    RangeDistributionBuilder fileComplexityDistribution =
        new RangeDistributionBuilder(
            CoreMetrics.FILE_COMPLEXITY_DISTRIBUTION, LIMITS_COMPLEXITY_FILES);
    sensorContext.saveMeasure(
        sonarFile,
        fileComplexityDistribution
            .add(fileComplexity)
            .build(true)
            .setPersistenceMode(PersistenceMode.MEMORY));
    saveLinesMetric();
  }
 private void saveFunctionsComplexityDistribution(File sonarFile, SourceFile squidFile) {
   Collection<SourceCode> squidFunctionsInFile =
       scanner
           .getIndex()
           .search(new QueryByParent(squidFile), new QueryByType(SourceFunction.class));
   RangeDistributionBuilder complexityDistribution =
       new RangeDistributionBuilder(
           CoreMetrics.FUNCTION_COMPLEXITY_DISTRIBUTION, FUNCTIONS_DISTRIB_BOTTOM_LIMITS);
   for (SourceCode squidFunction : squidFunctionsInFile) {
     complexityDistribution.add(squidFunction.getDouble(FlexMetric.COMPLEXITY));
   }
   context.saveMeasure(
       sonarFile, complexityDistribution.build().setPersistenceMode(PersistenceMode.MEMORY));
 }
Example #4
0
 @Override
 public void visitNode(Tree tree) {
   if (isClassTree(tree)) {
     classes++;
     classTrees.push((ClassTree) tree);
   }
   if (tree.is(Tree.Kind.NEW_CLASS) && ((NewClassTree) tree).classBody() != null) {
     classes--;
   }
   if (tree.is(Tree.Kind.METHOD, Tree.Kind.CONSTRUCTOR)
       && classTrees.peek().simpleName() != null) {
     // don't count methods in anonymous classes.
     MethodTree methodTree = (MethodTree) tree;
     if (separateAccessorsFromMethods
         && AccessorsUtils.isAccessor(classTrees.peek(), methodTree)) {
       accessors++;
     } else {
       methods++;
       int methodComplexity =
           context.getMethodComplexityNodes(classTrees.peek(), methodTree).size();
       methodComplexityDistribution.add(methodComplexity);
       complexityInMethods += methodComplexity;
     }
   }
 }