All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.sonar.java.ast.AstScanner Maven / Gradle / Ivy

There is a newer version: 3.2
Show newest version
/*
 * SonarQube Java
 * Copyright (C) 2012 SonarSource
 * [email protected]
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02
 */
package org.sonar.java.ast;

import com.google.common.collect.Lists;
import com.sonar.sslr.api.AstNode;
import com.sonar.sslr.api.AuditListener;
import com.sonar.sslr.api.CommentAnalyser;
import com.sonar.sslr.api.Grammar;
import com.sonar.sslr.api.RecognitionException;
import com.sonar.sslr.impl.Parser;
import com.sonar.sslr.impl.ast.AstWalker;
import com.sonar.sslr.squid.SquidAstVisitor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.resources.InputFile;
import org.sonar.java.ProgressReport;
import org.sonar.java.ast.api.JavaMetric;
import org.sonar.java.ast.visitors.VisitorContext;
import org.sonar.squid.api.AnalysisException;
import org.sonar.squid.api.CodeVisitor;
import org.sonar.squid.api.SourceCodeSearchEngine;
import org.sonar.squid.api.SourceCodeTreeDecorator;
import org.sonar.squid.api.SourceProject;
import org.sonar.squid.indexer.SquidIndex;
import org.sonar.sslr.parser.LexerlessGrammar;

import java.io.File;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * Replacement for {@link com.sonar.sslr.squid.AstScanner}.
 */
public class AstScanner {

  private static final Logger LOG = LoggerFactory.getLogger(AstScanner.class);

  private final SquidIndex index;
  private final List> visitors = Lists.newArrayList();
  private final List auditListeners = Lists.newArrayList();
  private final Parser parser;
  private CommentAnalyser commentAnalyser;

  public AstScanner(Parser parser) {
    this.parser = parser;
    this.index = new SquidIndex();
  }

  /**
   * Takes parser and index from another instance of {@link AstScanner}
   */
  public AstScanner(AstScanner astScanner) {
    this.parser = astScanner.parser;
    this.index = astScanner.index;
  }

  public void scan(Collection files) {
    SourceProject project = new SourceProject("Java Project");
    index.index(project);
    project.setSourceCodeIndexer(index);
    VisitorContext context = new VisitorContext(project);
    context.setCommentAnalyser(commentAnalyser);

    simpleScan(files);

    SourceCodeTreeDecorator decorator = new SourceCodeTreeDecorator(project);
    decorator.decorateWith(JavaMetric.values());
    decorator.decorateWith(org.sonar.squid.measures.Metric.values());
  }

  /**
   * Used to do scan of test files.
   */
  public void simpleScan(Collection files) {
    SourceProject project = (SourceProject) index.search("Java Project");
    VisitorContext context = new VisitorContext(project);
    context.setCommentAnalyser(commentAnalyser);

    for (SquidAstVisitor visitor : visitors) {
      visitor.setContext(context);
      visitor.init();
    }

    AstWalker astWalker = new AstWalker(visitors);

    ProgressReport progressReport = new ProgressReport("Report about progress of Java AST analyzer", TimeUnit.SECONDS.toMillis(10));
    progressReport.start(files.size() + " source files to be analyzed");
    int count = 0;
    for (InputFile inputFile : files) {
      File file = inputFile.getFile();

      progressReport.message(count + "/" + files.size() + " files analyzed, current is " + file.getAbsolutePath());
      count++;

      context.setFile(file);

      try {
        AstNode ast = parser.parse(file);
        astWalker.walkAndVisit(ast);
      } catch (RecognitionException e) {
        LOG.error("Unable to parse source file : " + file.getAbsolutePath());
        LOG.error(e.getMessage());

        parseErrorWalkAndVisit(e, file);
      } catch (Exception e) {
        throw new AnalysisException(getAnalyisExceptionMessage(file), e);
      }
    }
    progressReport.stop(files.size() + "/" + files.size() + " source files analyzed");

    for (SquidAstVisitor visitor : visitors) {
      visitor.destroy();
    }
  }

  private void parseErrorWalkAndVisit(RecognitionException e, File file) {
    try {
      // Process the exception
      for (SquidAstVisitor visitor : visitors) {
        visitor.visitFile(null);
      }

      for (AuditListener auditListener : auditListeners) {
        auditListener.processRecognitionException(e);
      }

      for (SquidAstVisitor visitor : Lists.reverse(visitors)) {
        visitor.leaveFile(null);
      }

    } catch (Exception e2) {
      throw new AnalysisException(getAnalyisExceptionMessage(file), e2);
    }
  }

  private static String getAnalyisExceptionMessage(File file) {
    return "SonarQube is unable to analyze file : '" + file.getAbsolutePath() + "'";
  }

  public void withSquidAstVisitor(SquidAstVisitor visitor) {
    if (visitor instanceof AuditListener) {
      auditListeners.add((AuditListener) visitor);
    }
    this.visitors.add(visitor);
  }

  public SourceCodeSearchEngine getIndex() {
    return index;
  }

  public void setCommentAnalyser(CommentAnalyser commentAnalyser) {
    this.commentAnalyser = commentAnalyser;
  }

  public void accept(CodeVisitor visitor) {
    if (visitor instanceof SquidAstVisitor) {
      withSquidAstVisitor((SquidAstVisitor) visitor);
    }
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy