aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api
diff options
context:
space:
mode:
authorJanos Gyerik <janos.gyerik@sonarsource.com>2017-03-02 17:32:20 +0100
committerGitHub <noreply@github.com>2017-03-02 17:32:20 +0100
commitbc298f995099795d8b280baa80eb579096dfcecb (patch)
treed488d7e774500a149325bd744085be425b223e23 /sonar-plugin-api
parent96b5ef41e280b775442388dfd49dad73e29d6a7e (diff)
downloadsonarqube-bc298f995099795d8b280baa80eb579096dfcecb.tar.gz
sonarqube-bc298f995099795d8b280baa80eb579096dfcecb.zip
SONAR-8832 Support project-level sensors (#1720)
Diffstat (limited to 'sonar-plugin-api')
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultFileSystem.java29
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/TestInputFileBuilder.java8
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/SensorDescriptor.java5
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/internal/DefaultSensorDescriptor.java11
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/DefaultFileSystemTest.java6
5 files changed, 40 insertions, 19 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultFileSystem.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultFileSystem.java
index b3f160872d4..dda12ece0fd 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultFileSystem.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultFileSystem.java
@@ -28,7 +28,6 @@ import java.nio.charset.Charset;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.util.ArrayList;
-import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
@@ -53,7 +52,6 @@ import org.sonar.api.utils.PathUtils;
public class DefaultFileSystem implements FileSystem {
private final Cache cache;
- private final SortedSet<String> languages = new TreeSet<>();
private final Path baseDir;
private Path workDir;
private Charset encoding;
@@ -192,10 +190,6 @@ public class DefaultFileSystem implements FileSystem {
public DefaultFileSystem add(InputFile inputFile) {
cache.add(inputFile);
- String language = inputFile.language();
- if (language != null) {
- languages.add(language);
- }
return this;
}
@@ -204,20 +198,10 @@ public class DefaultFileSystem implements FileSystem {
return this;
}
- /**
- * Adds a language to the list. To be used only for unit tests that need to use {@link #languages()} without
- * using {@link #add(InputFile)}.
- */
- public DefaultFileSystem addLanguages(String language, String... others) {
- languages.add(language);
- Collections.addAll(languages, others);
- return this;
- }
-
@Override
public SortedSet<String> languages() {
doPreloadFiles();
- return languages;
+ return cache.languages();
}
@Override
@@ -245,6 +229,8 @@ public class DefaultFileSystem implements FileSystem {
public void add(InputDir inputDir) {
doAdd(inputDir);
}
+
+ protected abstract SortedSet<String> languages();
}
/**
@@ -255,6 +241,7 @@ public class DefaultFileSystem implements FileSystem {
private final Map<String, InputDir> dirMap = new HashMap<>();
private final SetMultimap<String, InputFile> filesByNameCache = LinkedHashMultimap.create();
private final SetMultimap<String, InputFile> filesByExtensionCache = LinkedHashMultimap.create();
+ private SortedSet<String> languages = new TreeSet<>();
@Override
public Iterable<InputFile> inputFiles() {
@@ -283,6 +270,9 @@ public class DefaultFileSystem implements FileSystem {
@Override
protected void doAdd(InputFile inputFile) {
+ if (inputFile.language() != null) {
+ languages.add(inputFile.language());
+ }
fileMap.put(inputFile.relativePath(), inputFile);
filesByNameCache.put(FilenamePredicate.getFilename(inputFile), inputFile);
filesByExtensionCache.put(FileExtensionPredicate.getExtension(inputFile), inputFile);
@@ -292,6 +282,11 @@ public class DefaultFileSystem implements FileSystem {
protected void doAdd(InputDir inputDir) {
dirMap.put(inputDir.relativePath(), inputDir);
}
+
+ @Override
+ protected SortedSet<String> languages() {
+ return languages;
+ }
}
@Override
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/TestInputFileBuilder.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/TestInputFileBuilder.java
index 48f0f871853..6f803e1a8f0 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/TestInputFileBuilder.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/TestInputFileBuilder.java
@@ -19,12 +19,14 @@
*/
package org.sonar.api.batch.fs.internal;
+import java.io.File;
import java.io.StringReader;
import java.nio.charset.Charset;
import java.nio.file.Path;
import java.nio.file.Paths;
import javax.annotation.Nullable;
+import org.sonar.api.batch.bootstrap.ProjectDefinition;
import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.utils.PathUtils;
@@ -143,4 +145,10 @@ public class TestInputFileBuilder {
inputFile.setPublish(publish);
return inputFile;
}
+
+ public static DefaultInputModule newDefaultInputModule(String moduleKey, File baseDir) {
+ ProjectDefinition definition = ProjectDefinition.create().setKey(moduleKey);
+ definition.setBaseDir(baseDir);
+ return new DefaultInputModule(definition, TestInputFileBuilder.nextBatchId());
+ }
}
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/SensorDescriptor.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/SensorDescriptor.java
index 373d40aaf1c..5239c783363 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/SensorDescriptor.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/SensorDescriptor.java
@@ -79,4 +79,9 @@ public interface SensorDescriptor {
*/
SensorDescriptor requireProperties(String... propertyKeys);
+ /**
+ * This sensor should be executed at the project level, instead of per-module.
+ * @since 6.4
+ */
+ SensorDescriptor global();
}
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/internal/DefaultSensorDescriptor.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/internal/DefaultSensorDescriptor.java
index 56dc26847fa..59cb196c2c0 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/internal/DefaultSensorDescriptor.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/internal/DefaultSensorDescriptor.java
@@ -32,6 +32,7 @@ public class DefaultSensorDescriptor implements SensorDescriptor {
private InputFile.Type type = null;
private String[] ruleRepositories = new String[0];
private String[] properties = new String[0];
+ private boolean global = false;
public String name() {
return name;
@@ -54,6 +55,10 @@ public class DefaultSensorDescriptor implements SensorDescriptor {
return Arrays.asList(properties);
}
+ public boolean isGlobal() {
+ return global;
+ }
+
@Override
public DefaultSensorDescriptor name(String name) {
this.name = name;
@@ -99,4 +104,10 @@ public class DefaultSensorDescriptor implements SensorDescriptor {
return this;
}
+ @Override
+ public SensorDescriptor global() {
+ this.global = true;
+ return this;
+ }
+
}
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/DefaultFileSystemTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/DefaultFileSystemTest.java
index 8e7a286e529..1fcc28e0322 100644
--- a/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/DefaultFileSystemTest.java
+++ b/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/DefaultFileSystemTest.java
@@ -74,8 +74,10 @@ public class DefaultFileSystemTest {
public void add_languages() {
assertThat(fs.languages()).isEmpty();
- fs.addLanguages("java", "php", "cobol");
- assertThat(fs.languages()).containsOnly("cobol", "java", "php");
+ fs.add(new TestInputFileBuilder("foo", "src/Foo.php").setLanguage("php").build());
+ fs.add(new TestInputFileBuilder("foo", "src/Bar.java").setLanguage("java").build());
+
+ assertThat(fs.languages()).containsOnly("java", "php");
}
@Test