public void analyse(Project project, SensorContext context) {
    this.project = project;
    this.context = context;

    Collection<SquidAstVisitor<LexerlessGrammar>> squidChecks = annotationCheckFactory.getChecks();
    List<SquidAstVisitor<LexerlessGrammar>> visitors = Lists.newArrayList(squidChecks);
    visitors.add(new FileLinesVisitor(project, fileLinesContextFactory));
    this.scanner =
        FlexAstScanner.create(
            createConfiguration(project), visitors.toArray(new SquidAstVisitor[visitors.size()]));
    Collection<java.io.File> files =
        InputFileUtils.toFiles(project.getFileSystem().mainFiles(Flex.KEY));
    files = ImmutableList.copyOf(Collections2.filter(files, Predicates.not(MXML_FILTER)));
    scanner.scanFiles(files);

    Collection<SourceCode> squidSourceFiles =
        scanner.getIndex().search(new QueryByType(SourceFile.class));
    save(squidSourceFiles);

    Collection<SourceCode> squidPackages =
        scanner.getIndex().search(new QueryByType(FlexSquidPackage.class));
    for (SourceCode pkg : squidPackages) {
      String packageName = pkg.getKey();
      if (!"".equals(packageName)) {
        Directory directory = resourceBridge.findDirectory(packageName);
        context.saveMeasure(directory, CoreMetrics.PACKAGES, 1.0);
      }
    }
  }
  @Test
  public void testScanSimpleFile() {
    AstScanner<CSharpGrammar> scanner =
        CSharpAstScanner.create(new CSharpConfiguration(Charset.forName("UTF-8")));
    scanner.scanFile(readFile("/metric/simpleFile.cs"));
    SourceProject project =
        (SourceProject)
            scanner.getIndex().search(new QueryByType(SourceProject.class)).iterator().next();

    assertThat(project.getInt(CSharpMetric.STATEMENTS), is(16));
  }
 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));
 }
  public static AstScanner<CSharpGrammar> create(
      CSharpConfiguration conf, SquidAstVisitor<CSharpGrammar>... visitors) {

    final SquidAstVisitorContextImpl<CSharpGrammar> context =
        new SquidAstVisitorContextImpl<CSharpGrammar>(new SourceProject("C# Project"));
    final Parser<CSharpGrammar> parser = CSharpParser.create(conf);

    AstScanner.Builder<CSharpGrammar> builder =
        AstScanner.<CSharpGrammar>builder(context).setBaseParser(parser);

    /* Metrics */
    builder.withMetrics(CSharpMetric.values());

    /* Comments */
    builder.setCommentAnalyser(
        new CommentAnalyser() {

          @Override
          public boolean isBlank(String line) {
            for (int i = 0; i < line.length(); i++) {
              if (Character.isLetterOrDigit(line.charAt(i))) {
                return false;
              }
            }

            return true;
          }

          @Override
          public String getContents(String comment) {
            return comment.startsWith("//")
                ? comment.substring(2)
                : comment.substring(2, comment.length() - 2);
          }
        });

    /* Files */
    builder.setFilesMetric(CSharpMetric.FILES);

    /* Tree */
    builder.withSquidAstVisitor(new CSharpTypeVisitor());
    builder.withSquidAstVisitor(new CSharpMemberVisitor());

    /* Metrics */
    builder.withSquidAstVisitor(new LinesVisitor<CSharpGrammar>(CSharpMetric.LINES));
    builder.withSquidAstVisitor(new LinesOfCodeVisitor<CSharpGrammar>(CSharpMetric.LINES_OF_CODE));
    builder.withSquidAstVisitor(
        CommentsVisitor.<CSharpGrammar>builder()
            .withCommentMetric(CSharpMetric.COMMENT_LINES)
            .withBlankCommentMetric(CSharpMetric.COMMENT_BLANK_LINES)
            .withNoSonar(true)
            .withIgnoreHeaderComment(conf.getIgnoreHeaderComments())
            .build());
    builder.withSquidAstVisitor(
        CounterVisitor.<CSharpGrammar>builder()
            .setMetricDef(CSharpMetric.STATEMENTS)
            .subscribeTo(
                parser.getGrammar().labeledStatement,
                parser.getGrammar().declarationStatement,
                parser.getGrammar().expressionStatement,
                parser.getGrammar().selectionStatement,
                parser.getGrammar().iterationStatement,
                parser.getGrammar().jumpStatement,
                parser.getGrammar().tryStatement,
                parser.getGrammar().checkedStatement,
                parser.getGrammar().uncheckedStatement,
                parser.getGrammar().lockStatement,
                parser.getGrammar().usingStatement,
                parser.getGrammar().yieldStatement)
            .build());
    builder.withSquidAstVisitor(
        CounterVisitor.<CSharpGrammar>builder()
            .setMetricDef(CSharpMetric.ACCESSORS)
            .subscribeTo(
                parser.getGrammar().getAccessorDeclaration,
                parser.getGrammar().setAccessorDeclaration,
                parser.getGrammar().addAccessorDeclaration,
                parser.getGrammar().removeAccessorDeclaration)
            .build());

    /* Visitors */
    builder.withSquidAstVisitor(new CSharpComplexityVisitor());
    builder.withSquidAstVisitor(new CSharpPublicApiVisitor());

    /* External visitors (typically Check ones) */
    for (SquidAstVisitor<CSharpGrammar> visitor : visitors) {
      builder.withSquidAstVisitor(visitor);
    }

    return builder.build();
  }