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

org.sonar.plugins.java.bridges.ResourceIndex Maven / Gradle / Ivy

/*
 * 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.plugins.java.bridges;

import com.google.common.collect.Maps;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.CoreProperties;
import org.sonar.api.batch.SensorContext;
import org.sonar.api.resources.Project;
import org.sonar.api.resources.Resource;
import org.sonar.api.utils.SonarException;
import org.sonar.java.JavaSquid;
import org.sonar.squid.api.SourceCode;
import org.sonar.squid.api.SourceFile;
import org.sonar.squid.api.SourcePackage;
import org.sonar.squid.api.SourceProject;
import org.sonar.squid.indexer.QueryByType;
import org.sonar.squid.indexer.SquidIndex;

import java.io.File;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

public final class ResourceIndex extends HashMap {

  private static final long serialVersionUID = -918346378374943773L;

  private static final Logger LOG = LoggerFactory.getLogger(ResourceIndex.class);
  private final boolean skipPackageDesignAnalysis;

  public ResourceIndex(boolean skipPackageDesignAnalysis) {
    this.skipPackageDesignAnalysis = skipPackageDesignAnalysis;
  }

  public ResourceIndex loadSquidResources(JavaSquid squid, SensorContext context, Project project) {
    loadSquidProject(squid.getIndex(), project);
    loadSquidFilesAndPackages(squid.getIndex(), context, project);
    return this;
  }

  private void loadSquidProject(SquidIndex squid, Project project) {
    put(squid.search(new QueryByType(SourceProject.class)).iterator().next(), project);
  }

  /**
   * @see org.sonar.java.ast.visitors.FileVisitor
   * @see org.sonar.java.ast.visitors.PackageVisitor
   */
  private void loadSquidFilesAndPackages(SquidIndex squid, SensorContext context, Project project) {
    Map directoryReverseMap = Maps.newHashMap();

    Collection files = squid.search(new QueryByType(SourceFile.class));
    for (SourceCode squidFile : files) {
      String filePath = squidFile.getName();

      File file = new File(filePath);
      Resource sonarFile = org.sonar.api.resources.File.fromIOFile(file, project);
      // resource is reloaded to get the id:
      put(squidFile, context.getResource(sonarFile));

      SourceCode squidPackage = squidFile.getParent(SourcePackage.class);
      Resource sonarDirectory = context.getResource(sonarFile.getParent());

      SourceCode previousDirectoryMapping = directoryReverseMap.get(sonarDirectory);
      if (previousDirectoryMapping == null) {
        directoryReverseMap.put(sonarDirectory, squidPackage);
        put(squidPackage, sonarDirectory);
      } else if (!previousDirectoryMapping.equals(squidPackage)) {
        String message = "Directory contains files belonging to different packages";
        String warning = " - some metrics could be reported incorrectly: {}";
        if (skipPackageDesignAnalysis) {
          LOG.warn(message + warning, file.getParentFile());
        } else {
          LOG.error(message + warning, file.getParentFile());
          throw new SonarException(message + " : " + file.getParentFile() +
              " Please fix your source code or use " + CoreProperties.DESIGN_SKIP_PACKAGE_DESIGN_PROPERTY + "=true to continue the analysis.");
        }
      }
    }
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy