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

org.sonar.java.JavaSquid 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;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Joiner;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.design.Dependency;
import org.sonar.api.resources.InputFile;
import org.sonar.api.resources.InputFileUtils;
import org.sonar.api.resources.Resource;
import org.sonar.api.utils.TimeProfiler;
import org.sonar.graph.DirectedGraph;
import org.sonar.java.ast.AstScanner;
import org.sonar.java.ast.visitors.FileLinesVisitor;
import org.sonar.java.ast.visitors.SyntaxHighlighterVisitor;
import org.sonar.java.bytecode.BytecodeScanner;
import org.sonar.java.bytecode.visitor.DependenciesVisitor;
import org.sonar.java.model.TestFileVisitorsBridge;
import org.sonar.java.model.VisitorsBridge;
import org.sonar.plugins.java.api.JavaResourceLocator;
import org.sonar.squidbridge.api.CodeVisitor;
import org.sonar.squidbridge.api.Query;
import org.sonar.squidbridge.api.SourceCode;
import org.sonar.squidbridge.api.SourceCodeSearchEngine;
import org.sonar.squidbridge.indexer.SquidIndex;

import javax.annotation.Nullable;
import java.io.File;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

public class JavaSquid implements SourceCodeSearchEngine {

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

  private final SquidIndex squidIndex;
  private final AstScanner astScanner;
  private final AstScanner astScannerForTests;
  private final BytecodeScanner bytecodeScanner;
  private final DirectedGraph graph = new DirectedGraph();

  private boolean bytecodeScanned = false;

  @VisibleForTesting
  public JavaSquid(JavaConfiguration conf, JavaResourceLocator javaResourceLocator, CodeVisitor... visitors) {
    this(conf, null, null, javaResourceLocator, visitors);
  }

  public JavaSquid(JavaConfiguration conf,
                   @Nullable SonarComponents sonarComponents, @Nullable Measurer measurer,
                   JavaResourceLocator javaResourceLocator, CodeVisitor... visitors) {

    astScanner = JavaAstScanner.create(conf);

    Iterable visitorsToBridge = Iterables.concat(Arrays.asList(javaResourceLocator), Arrays.asList(visitors));
    if(measurer != null) {
      Iterable measurers = Arrays.asList((CodeVisitor)measurer);
      visitorsToBridge =  Iterables.concat(visitorsToBridge, measurers);
    }
    if (sonarComponents != null) {
      visitorsToBridge = Iterables.concat(
          sonarComponents.createJavaFileScanners(),
          visitorsToBridge
      );
    }
    VisitorsBridge visitorsBridge = new VisitorsBridge(visitorsToBridge, sonarComponents);
    visitorsBridge.setCharset(conf.getCharset());
    visitorsBridge.setAnalyseAccessors(conf.isAnalysePropertyAccessors());
    astScanner.accept(visitorsBridge);

    if (sonarComponents != null) {
      astScanner.accept(new FileLinesVisitor(sonarComponents, conf.getCharset()));
      astScanner.accept(new SyntaxHighlighterVisitor(sonarComponents, conf.getCharset()));
    }

    // TODO unchecked cast
    squidIndex = (SquidIndex) astScanner.getIndex();

    bytecodeScanner = new BytecodeScanner(squidIndex, javaResourceLocator);
    bytecodeScanner.accept(new DependenciesVisitor(graph));

    // External visitors (typically Check ones):
    for (CodeVisitor visitor : visitors) {
      if (visitor instanceof CharsetAwareVisitor) {
        ((CharsetAwareVisitor) visitor).setCharset(conf.getCharset());
      }
      astScanner.accept(visitor);
      bytecodeScanner.accept(visitor);
    }

    astScannerForTests = new AstScanner(astScanner);
    astScannerForTests.accept(new TestFileVisitorsBridge(javaResourceLocator));
  }

  @VisibleForTesting
  public void scanDirectories(Collection sourceDirectories, Collection bytecodeFilesOrDirectories) {
    List sourceFiles = Lists.newArrayList();
    for (File dir : sourceDirectories) {
      sourceFiles.addAll(InputFileUtils.create(dir, FileUtils.listFiles(dir, new String[]{"java"}, true)));
    }
    scan(sourceFiles, Collections.emptyList(), bytecodeFilesOrDirectories);
  }

  public void scan(Collection sourceFiles, Collection testFiles, Collection bytecodeFilesOrDirectories) {
    scanSources(sourceFiles);
    scanBytecode(bytecodeFilesOrDirectories);
    scanTests(testFiles);
  }

  private void scanSources(Collection sourceFiles) {
    TimeProfiler profiler = new TimeProfiler(getClass()).start("Java Main Files AST scan");
    astScanner.scan(sourceFiles);
    profiler.stop();
  }

  private void scanTests(Collection testFiles) {
    TimeProfiler profiler = new TimeProfiler(getClass()).start("Java Test Files AST scan");
    astScannerForTests.simpleScan(testFiles);
    profiler.stop();
  }

  private void scanBytecode(Collection bytecodeFilesOrDirectories) {
    if (hasBytecode(bytecodeFilesOrDirectories)) {
      TimeProfiler profiler = new TimeProfiler(getClass()).start("Java bytecode scan");

      bytecodeScanner.scan(bytecodeFilesOrDirectories);
      bytecodeScanned = true;
      profiler.stop();
    } else {
      LOG.warn("Java bytecode has not been made available to the analyzer. The " + Joiner.on(", ").join(bytecodeScanner.getVisitors()) + " are disabled.");
      bytecodeScanned = false;
    }
  }

  static boolean hasBytecode(Collection bytecodeFilesOrDirectories) {
    if (bytecodeFilesOrDirectories == null) {
      return false;
    }
    for (File bytecodeFilesOrDirectory : bytecodeFilesOrDirectories) {
      if (bytecodeFilesOrDirectory.exists() &&
          (bytecodeFilesOrDirectory.isFile() ||
              !FileUtils.listFiles(bytecodeFilesOrDirectory, new String[]{"class"}, true).isEmpty())) {
        return true;
      }
    }
    return false;
  }

  public boolean isBytecodeScanned() {
    return bytecodeScanned;
  }

  public SquidIndex getIndex() {
    return squidIndex;
  }

  public DirectedGraph getGraph() {
    return graph;
  }

  @Override
  public SourceCode search(String key) {
    return squidIndex.search(key);
  }

  @Override
  public Collection search(Query... query) {
    return squidIndex.search(query);
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy