* Use language keys instead of objects.
* ProjectFileSystem now BatchComponent, so DefaultProjectFileSystem
uses benefits from injections and some old code was removed from
it to improve immutability.
* Small performance improvement for DefaultInputFile.
}
void analyse(ProjectFileSystem fileSystem, SensorContext context) {
- parseDirs(context, fileSystem.mainFiles(Java.INSTANCE), false, fileSystem.getSourceCharset());
- parseDirs(context, fileSystem.testFiles(Java.INSTANCE), true, fileSystem.getSourceCharset());
+ parseDirs(context, fileSystem.mainFiles(Java.KEY), false, fileSystem.getSourceCharset());
+ parseDirs(context, fileSystem.testFiles(Java.KEY), true, fileSystem.getSourceCharset());
}
void parseDirs(SensorContext context, List<InputFile> inputFiles, boolean unitTest, Charset sourcesEncoding) {
}
private void browseTestSources(Project project, SensorContext context) {
- for (InputFile testFile : project.getFileSystem().testFiles(Java.INSTANCE)) {
+ for (InputFile testFile : project.getFileSystem().testFiles(Java.KEY)) {
context.index(JavaFile.fromRelativePath(testFile.getRelativePath(), true));
}
}
import org.sonar.api.database.DatabaseSession;
import org.sonar.api.database.model.ResourceModel;
import org.sonar.api.database.model.Snapshot;
-import org.sonar.api.resources.DefaultProjectFileSystem;
import org.sonar.api.resources.Java;
import org.sonar.api.resources.Project;
import org.sonar.api.utils.SonarException;
void configure(Project project, Configuration projectConfiguration) {
Date analysisDate = loadAnalysisDate(projectConfiguration);
- DefaultProjectFileSystem fs = new DefaultProjectFileSystem(project);
MavenProject pom = project.getPom();
if (pom != null) {
- fs.setBaseDir(pom.getBasedir());
- fs.setBuildDir(pom.getBuild().getDirectory());
- projectConfiguration.setProperty("project.build.outputDirectory", pom.getBuild().getOutputDirectory());
- if (pom.getReporting() != null) {
- projectConfiguration.setProperty("project.reporting.outputDirectory", pom.getReporting().getOutputDirectory());
- }
projectConfiguration.setProperty("sonar.java.sourceVersion", MavenUtils.getJavaSourceVersion(pom));
projectConfiguration.setProperty("sonar.java.targetVersion", MavenUtils.getJavaVersion(pom));
}
.setLatestAnalysis(isLatestAnalysis(project.getKey(), analysisDate))
.setAnalysisVersion(loadAnalysisVersion(projectConfiguration, pom))
.setAnalysisType(loadAnalysisType(projectConfiguration))
- .setLanguageKey(loadLanguageKey(projectConfiguration))
- .setFileSystem(fs);
+ .setLanguageKey(loadLanguageKey(projectConfiguration));
}
static String[] loadExclusionPatterns(Configuration configuration) {
import org.picocontainer.Characteristics;
import org.picocontainer.MutablePicoContainer;
import org.sonar.api.batch.BatchExtensionDictionnary;
-import org.sonar.api.batch.FileFilter;
import org.sonar.api.batch.ProjectClasspath;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.measures.Metric;
import org.sonar.api.measures.Metrics;
import org.sonar.api.profiles.RulesProfile;
-import org.sonar.api.resources.DefaultProjectFileSystem;
-import org.sonar.api.resources.Language;
-import org.sonar.api.resources.Languages;
-import org.sonar.api.resources.Project;
+import org.sonar.api.resources.*;
import org.sonar.api.rules.DefaultRulesManager;
import org.sonar.api.utils.IocContainer;
import org.sonar.api.utils.SonarException;
import org.sonar.core.components.DefaultModelFinder;
import org.sonar.jpa.dao.*;
-import java.util.List;
-
public class ProjectBatch {
private MutablePicoContainer globalContainer;
batchContainer.as(Characteristics.CACHE).addComponent(project);
batchContainer.as(Characteristics.CACHE).addComponent(project.getPom());
batchContainer.as(Characteristics.CACHE).addComponent(ProjectClasspath.class);
+ batchContainer.as(Characteristics.CACHE).addComponent(DefaultProjectFileSystem.class);
batchContainer.as(Characteristics.CACHE).addComponent(project.getConfiguration());
// need to be registered after the Configuration
batchContainer.as(Characteristics.CACHE).addComponent(RulesDao.class);
// the Snapshot component will be removed when asynchronous measures are improved (required for AsynchronousMeasureSensor)
- batchContainer.as(Characteristics.CACHE).addComponent(globalContainer.getComponent(DefaultResourcePersister.class).getSnapshot(project));
+ batchContainer.as(Characteristics.CACHE)
+ .addComponent(globalContainer.getComponent(DefaultResourcePersister.class).getSnapshot(project));
batchContainer.as(Characteristics.CACHE).addComponent(org.sonar.api.database.daos.RulesDao.class);
batchContainer.as(Characteristics.CACHE).addComponent(org.sonar.api.database.daos.MeasuresDao.class);
throw new SonarException("Language with key '" + project.getLanguageKey() + "' not found");
}
project.setLanguage(language);
- index.setCurrentProject(project, getComponent(ResourceFilters.class), getComponent(ViolationFilters.class), getComponent(RulesProfile.class));
- List<FileFilter> fileFilters = batchContainer.getComponents(FileFilter.class);
- ((DefaultProjectFileSystem) project.getFileSystem()).addFileFilters(fileFilters);
+ index.setCurrentProject(project,
+ getComponent(ResourceFilters.class),
+ getComponent(ViolationFilters.class),
+ getComponent(RulesProfile.class));
+
+ // TODO See http://jira.codehaus.org/browse/SONAR-2126
+ // previously MavenProjectBuilder was responsible for creation of ProjectFileSystem
+ project.setFileSystem(getComponent(ProjectFileSystem.class));
}
private void loadCoreComponents(MutablePicoContainer container) {
}
}
-
public <T> T getComponent(Class<T> clazz) {
if (batchContainer != null) {
return batchContainer.getComponent(clazz);
* For internal use only.
*
* @since 1.10
- * @TODO inject into container
+ * @TODO in fact this class should not be located in sonar-plugin-api
*/
public class DefaultProjectFileSystem implements ProjectFileSystem {
private Project project;
+ private Languages languages;
private List<IOFileFilter> filters = Lists.newArrayList();
- private File basedir;
- private File buildDir;
- private List<File> testDirs = Lists.newArrayList();
-
- public DefaultProjectFileSystem(Project project) {
+ public DefaultProjectFileSystem(Project project, Languages languages) {
this.project = project;
+ this.languages = languages;
+ }
+
+ public DefaultProjectFileSystem(Project project, Languages languages, FileFilter... fileFilters) {
+ this(project, languages);
+ for (FileFilter fileFilter : fileFilters) {
+ filters.add(new DelegateFileFilter(fileFilter));
+ }
}
public Charset getSourceCharset() {
return Charset.defaultCharset();
}
- public DefaultProjectFileSystem addFileFilters(List<FileFilter> l) {
- for (FileFilter fileFilter : l) {
- addFileFilter(fileFilter);
- }
- return this;
- }
-
- public DefaultProjectFileSystem addFileFilter(FileFilter fileFilter) {
- filters.add(new DelegateFileFilter(fileFilter));
- return this;
- }
-
public File getBasedir() {
- // TODO was return project.getPom().getBasedir();
- return basedir;
+ return project.getPom().getBasedir();
}
public File getBuildDir() {
- // TODO was return resolvePath(project.getPom().getBuild().getDirectory());
- return buildDir;
+ return resolvePath(project.getPom().getBuild().getDirectory());
}
public File getBuildOutputDir() {
- // TODO was return resolvePath(project.getPom().getBuild().getOutputDirectory());
- return resolvePath(project.getConfiguration().getString("project.build.outputDirectory"));
+ return resolvePath(project.getPom().getBuild().getOutputDirectory());
}
/**
return resolvePaths(project.getPom().getCompileSourceRoots());
}
+ /**
+ * @deprecated since 2.6, because should be immutable
+ */
+ @Deprecated
public DefaultProjectFileSystem addSourceDir(File dir) {
if (dir == null) {
throw new IllegalArgumentException("Can not add null to project source dirs");
return resolvePaths(project.getPom().getTestCompileSourceRoots());
}
+ /**
+ * @deprecated since 2.6, because should be immutable
+ */
+ @Deprecated
public DefaultProjectFileSystem addTestDir(File dir) {
if (dir == null) {
throw new IllegalArgumentException("Can not add null to project test dirs");
}
public File getReportOutputDir() {
- // TODO was return resolvePath(project.getPom().getReporting().getOutputDirectory());
- return resolvePath(project.getConfiguration().getString("project.reporting.outputDirectory"));
+ return resolvePath(project.getPom().getReporting().getOutputDirectory());
}
public File getSonarWorkingDirectory() {
return file;
}
- // TODO was private
- public List<File> resolvePaths(List<String> paths) {
+ private List<File> resolvePaths(List<String> paths) {
List<File> result = Lists.newArrayList();
if (paths != null) {
for (String path : paths) {
@Deprecated
public List<File> getSourceFiles(Language... langs) {
- return toFiles(mainFiles(langs));
+ return toFiles(mainFiles(getLanguageKeys(langs)));
}
@Deprecated
}
public boolean hasJavaSourceFiles() {
- return !mainFiles(Java.INSTANCE).isEmpty();
+ return !mainFiles(Java.KEY).isEmpty();
}
@Deprecated
public List<File> getTestFiles(Language... langs) {
- return toFiles(testFiles(langs));
+ return toFiles(testFiles(getLanguageKeys(langs)));
}
+ @Deprecated
public boolean hasTestFiles(Language lang) {
- return !testFiles(lang).isEmpty();
+ return !testFiles(lang.getKey()).isEmpty();
}
- private List<InputFile> getFiles(List<File> directories, boolean applyExclusionPatterns, Language... langs) {
+ private List<InputFile> getFiles(List<File> directories, boolean applyExclusionPatterns, String... langs) {
List<InputFile> result = Lists.newArrayList();
if (directories == null) {
return result;
dirFilters.addAll(this.filters);
List<File> files = (List<File>) FileUtils.listFiles(dir, new AndFileFilter(dirFilters), HiddenFileFilter.VISIBLE);
for (File file : files) {
- result.add(new DefaultInputFile(dir, file));
+ String relativePath = DefaultProjectFileSystem.getRelativePath(file, dir);
+ result.add(new DefaultInputFile(dir, relativePath));
}
}
}
return exclusionPatterns;
}
- private IOFileFilter getFileSuffixFilter(Language... langs) {
+ private IOFileFilter getFileSuffixFilter(String... langKeys) {
IOFileFilter suffixFilter = FileFilterUtils.trueFileFilter();
- if (langs != null && langs.length > 0) {
- List<String> suffixes = new ArrayList<String>();
- for (Language lang : langs) {
- if (lang.getFileSuffixes() != null) {
- suffixes.addAll(Arrays.asList(lang.getFileSuffixes()));
- }
- }
+ if (langKeys != null && langKeys.length > 0) {
+ List<String> suffixes = Arrays.asList(languages.getSuffixes(langKeys));
if (!suffixes.isEmpty()) {
suffixFilter = new SuffixFileFilter(suffixes);
}
}
-
return suffixFilter;
}
return false;
}
+ /**
+ * Conversion from Language to key. Allows to provide backward compatibility.
+ */
+ private String[] getLanguageKeys(Language[] langs) {
+ String[] keys = new String[langs.length];
+ for (int i = 0; i < langs.length; i++) {
+ keys[i] = langs[i].getKey();
+ }
+ return keys;
+ }
+
+ /**
+ * Conversion from InputFile to File. Allows to provide backward compatibility.
+ */
private static List<File> toFiles(List<InputFile> files) {
List<File> result = Lists.newArrayList();
for (InputFile file : files) {
/**
* @since 2.6
*/
- public List<InputFile> mainFiles(Language... langs) {
+ public List<InputFile> mainFiles(String... langs) {
return getFiles(getSourceDirs(), true, langs);
}
/**
* @since 2.6
*/
- public List<InputFile> testFiles(Language... langs) {
+ public List<InputFile> testFiles(String... langs) {
return getFiles(getTestDirs(), false /* FIXME should be true? */, langs);
}
private static final class DefaultInputFile implements InputFile {
private File basedir;
- private File file;
+ private String relativePath;
- DefaultInputFile(File basedir, File file) {
+ DefaultInputFile(File basedir, String relativePath) {
this.basedir = basedir;
- this.file = file;
+ this.relativePath = relativePath;
}
public File getFileBaseDir() {
}
public File getFile() {
- return file;
+ return new File(basedir, relativePath);
}
public String getRelativePath() {
- return DefaultProjectFileSystem.getRelativePath(file, basedir);
+ return relativePath;
}
}
-
- /**
- * @since 2.6
- */
- public void setBaseDir(File basedir) {
- this.basedir = basedir;
- }
-
- /**
- * @since 2.6
- */
- public void setBuildDir(String path) {
- this.buildDir = path == null ? resolvePath("target") : resolvePath(path);
- }
}
/**
* @deprecated since 2.6. See http://jira.codehaus.org/browse/SONAR-2126
+ * ProjectFileSystem can be retrieved as an IoC dependency
*/
public ProjectFileSystem getFileSystem() {
return fileSystem;
*/
package org.sonar.api.resources;
-import org.sonar.api.BatchExtension;
+import org.sonar.api.BatchComponent;
import java.io.File;
import java.io.IOException;
/**
* @since 1.10
*/
-public interface ProjectFileSystem extends BatchExtension {
+public interface ProjectFileSystem extends BatchComponent {
/**
* Source encoding.
* Never null, it returns the default platform charset if it is not defined in project.
* Adds a source directory
*
* @return the current object
+ * @deprecated since 2.6 - ProjectFileSystem should be immutable
+ * See http://jira.codehaus.org/browse/SONAR-2126
*/
ProjectFileSystem addSourceDir(File dir);
* Adds a test directory
*
* @return the current object
+ * @deprecated since 2.6 - ProjectFileSystem should be immutable
+ * See http://jira.codehaus.org/browse/SONAR-2126
*/
ProjectFileSystem addTestDir(File dir);
/**
* Check if the project has Java files, excluding unit tests and files matching project exclusion patterns.
+ *
+ * @deprecated since 2.6 - API should be language agnostic
*/
+ @Deprecated
boolean hasJavaSourceFiles();
/**
/**
* Check if the project has unit test files, excluding files matching project exclusion patterns.
+ *
+ * @deprecated since 2.6 - use language key instead of Language object
*/
+ @Deprecated
boolean hasTestFiles(Language lang);
/**
* @param langs language filter. If null or empty, will return empty list
* @since 2.6
*/
- List<InputFile> mainFiles(Language... langs);
+ List<InputFile> mainFiles(String... langs);
/**
* TODO comment me
* @param langs language filter. If null or empty, will return empty list
* @since 2.6
*/
- List<InputFile> testFiles(Language... langs);
+ List<InputFile> testFiles(String... langs);
}
}
@Test
- public void shouldAddExtendedFilters() {
- DefaultProjectFileSystem fs = newDefaultProjectFileSystem(project);
- assertThat(fs.getSourceFiles().size(), is(2));
- assertThat(fs.getSourceFiles(), hasItem(named("Bar.java")));
+ public void shouldUseExtendedFilters() {
+ DefaultProjectFileSystem fsWithoutFilter = newDefaultProjectFileSystem(project);
+ assertThat(fsWithoutFilter.getSourceFiles().size(), is(2));
+ assertThat(fsWithoutFilter.getSourceFiles(), hasItem(named("Bar.java")));
- fs.addFileFilter(new FileFilter() {
+ FileFilter filter = new FileFilter() {
public boolean accept(File file) {
return !StringUtils.equals(file.getName(), "Bar.java");
}
- });
- assertThat(fs.getSourceFiles().size(), is(1));
- assertThat(fs.getSourceFiles(), not(hasItem(named("Bar.java"))));
+ };
+ DefaultProjectFileSystem fsWithFilter = new DefaultProjectFileSystem(project, new Languages(Java.INSTANCE), filter);
+ assertThat(fsWithFilter.getSourceFiles().size(), is(1));
+ assertThat(fsWithFilter.getSourceFiles(), not(hasItem(named("Bar.java"))));
}
private DefaultProjectFileSystem newDefaultProjectFileSystem(Project project) {
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.apache.maven.project.MavenProject;
import org.sonar.api.resources.DefaultProjectFileSystem;
+import org.sonar.api.resources.Java;
+import org.sonar.api.resources.Languages;
import org.sonar.api.resources.Project;
import org.sonar.api.utils.SonarException;
Project project = new Project(pom.getGroupId() + ":" + pom.getArtifactId())
.setPom(pom)
.setConfiguration(new MapConfiguration(pom.getProperties()));
- DefaultProjectFileSystem fs = new DefaultProjectFileSystem(project);
+ DefaultProjectFileSystem fs = new DefaultProjectFileSystem(project, new Languages(Java.INSTANCE));
project.setFileSystem(fs);
- project.getConfiguration().setProperty("project.build.outputDirectory", pom.getBuild().getOutputDirectory());
- if (pom.getReporting() != null) {
- project.getConfiguration().setProperty("project.reporting.outputDirectory", pom.getReporting().getOutputDirectory());
- }
- fs.setBaseDir(project.getPom().getBasedir());
- fs.setBuildDir(project.getPom().getBuild().getDirectory());
return project;
}
*/
package org.sonar.api.test;
-import org.sonar.api.resources.InputFile;
-
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.CharEncoding;
import org.apache.commons.lang.NotImplementedException;
+import org.sonar.api.resources.InputFile;
import org.sonar.api.resources.Language;
import org.sonar.api.resources.ProjectFileSystem;
import org.sonar.api.resources.Resource;
/**
* @since 2.6
*/
- public List<InputFile> mainFiles(Language... lang) {
+ public List<InputFile> mainFiles(String... lang) {
return null;
}
/**
* @since 2.6
*/
- public List<InputFile> testFiles(Language... lang) {
+ public List<InputFile> testFiles(String... lang) {
return null;
}
+
}