diff options
author | Duarte Meneses <duarte.meneses@sonarsource.com> | 2017-01-24 18:02:09 +0100 |
---|---|---|
committer | Duarte Meneses <duarte.meneses@sonarsource.com> | 2017-01-27 16:26:30 +0100 |
commit | 3c0dd8758d56ce8583a3a74c17a181e4cfe5918c (patch) | |
tree | 47766f525c8fa28e4baf78dd5de48b6a5ff82b58 /sonar-plugin-api | |
parent | 291382160309e0543067c59c193d126710370dee (diff) | |
download | sonarqube-3c0dd8758d56ce8583a3a74c17a181e4cfe5918c.tar.gz sonarqube-3c0dd8758d56ce8583a3a74c17a181e4cfe5918c.zip |
SONAR-8622 Lazily generate metadata for input files
Diffstat (limited to 'sonar-plugin-api')
10 files changed, 77 insertions, 36 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/bootstrap/ProjectKey.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/bootstrap/ProjectKey.java index bcd9941d387..a465da801c5 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/bootstrap/ProjectKey.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/bootstrap/ProjectKey.java @@ -19,6 +19,9 @@ */ package org.sonar.api.batch.bootstrap; +/** + * Provides root project key with branch + */ @FunctionalInterface public interface ProjectKey { String get(); diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/FilePredicates.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/FilePredicates.java index 1aa79288923..9c6d07fd33f 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/FilePredicates.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/FilePredicates.java @@ -61,6 +61,8 @@ public interface FilePredicates { * <code>some/path/Foo.java</code> and <code>other/path/Foo.java</code>. * The parameter must match exactly, no patterns are allowed, * and it must not be <code>null</code>. + * + * @since 6.3 */ FilePredicate hasFilename(String s); @@ -70,6 +72,8 @@ public interface FilePredicates { * <code>some/path/Foo.java</code> and <code>other/path/Foo.JAVA</code> * but not <code>some/path/Foo.js</code>. * The parameter must not be <code>null</code>. + * + * @since 6.3 */ FilePredicate hasExtension(String s); diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/FileSystem.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/FileSystem.java index 2c9adee3092..695e64b9344 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/FileSystem.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/FileSystem.java @@ -163,11 +163,15 @@ public interface FileSystem { @CheckForNull InputDir inputDir(String relativePath); - - InputModule module(); + /** + * @since 6.3 + */ Iterable<InputFile> getFilesByName(String filename); + /** + * @since 6.3 + */ Iterable<InputFile> getFilesByExtension(String extension); } } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/IndexedFile.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/IndexedFile.java index 89381593298..492ce417ed6 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/IndexedFile.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/IndexedFile.java @@ -32,6 +32,9 @@ import org.sonar.api.batch.fs.InputFile; import org.sonar.api.batch.fs.InputPath; /** + * Represents the indexed view of an {@link InputFile}. Accessing any of data exposed here won't trigger the expensive generation of + * metadata for the {@link InputFile}. + * * @since 6.3 */ public interface IndexedFile extends InputPath { diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/InputComponent.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/InputComponent.java index c6272fb8aee..645b7f86d20 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/InputComponent.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/InputComponent.java @@ -30,14 +30,14 @@ package org.sonar.api.batch.fs; public interface InputComponent { /** - * Component key shared by all part of SonarQube (batch, server, WS...) + * Component key shared by all part of SonarQube (batch, server, WS...). + * It doesn't include the branch. */ String key(); - + /** * Is the component an {@link InputFile} */ boolean isFile(); - } 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 de2c28766de..bdac6453b75 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 @@ -43,7 +43,6 @@ import org.sonar.api.batch.fs.FilePredicates; import org.sonar.api.batch.fs.FileSystem; import org.sonar.api.batch.fs.InputDir; import org.sonar.api.batch.fs.InputFile; -import org.sonar.api.batch.fs.InputModule; import org.sonar.api.scan.filesystem.PathResolver; import org.sonar.api.utils.PathUtils; @@ -204,11 +203,6 @@ public class DefaultFileSystem implements FileSystem { return this; } - public DefaultFileSystem add(InputModule inputModule) { - cache.add(inputModule); - 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)}. @@ -243,8 +237,6 @@ public class DefaultFileSystem implements FileSystem { protected abstract void doAdd(InputDir inputDir); - protected abstract void doAdd(InputModule inputModule); - final void add(InputFile inputFile) { doAdd(inputFile); } @@ -252,11 +244,6 @@ public class DefaultFileSystem implements FileSystem { public void add(InputDir inputDir) { doAdd(inputDir); } - - public void add(InputModule inputModule) { - doAdd(inputModule); - } - } /** @@ -265,7 +252,6 @@ public class DefaultFileSystem implements FileSystem { private static class MapCache extends Cache { private final Map<String, InputFile> fileMap = new HashMap<>(); private final Map<String, InputDir> dirMap = new HashMap<>(); - private InputModule module; private final SetMultimap<String, InputFile> filesByNameCache = LinkedHashMultimap.create(); private final SetMultimap<String, InputFile> filesByExtensionCache = LinkedHashMultimap.create(); @@ -285,11 +271,6 @@ public class DefaultFileSystem implements FileSystem { } @Override - public InputModule module() { - return module; - } - - @Override public Iterable<InputFile> getFilesByName(String filename) { return filesByNameCache.get(filename); } @@ -298,7 +279,6 @@ public class DefaultFileSystem implements FileSystem { return filesByExtensionCache.get(extension); } - @Override protected void doAdd(InputFile inputFile) { fileMap.put(inputFile.relativePath(), inputFile); filesByNameCache.put(FilenamePredicate.getFilename(inputFile), inputFile); @@ -309,11 +289,6 @@ public class DefaultFileSystem implements FileSystem { protected void doAdd(InputDir inputDir) { dirMap.put(inputDir.relativePath(), inputDir); } - - @Override - protected void doAdd(InputModule inputModule) { - module = inputModule; - } } @Override diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultIndexedFile.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultIndexedFile.java index d29b42c8b59..ff653561dfc 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultIndexedFile.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultIndexedFile.java @@ -29,6 +29,9 @@ import org.sonar.api.batch.fs.IndexedFile; import org.sonar.api.batch.fs.InputFile.Type; import org.sonar.api.utils.PathUtils; +/** + * @since 6.3 + */ public class DefaultIndexedFile extends DefaultInputComponent implements IndexedFile { private final String relativePath; private final String moduleKey; @@ -92,7 +95,7 @@ public class DefaultIndexedFile extends DefaultInputComponent implements Indexed } /** - * Component key. + * Component key (without branch). */ @Override public String key() { diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultInputFile.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultInputFile.java index cecc2e410d7..192ac05be54 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultInputFile.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultInputFile.java @@ -40,25 +40,33 @@ public class DefaultInputFile extends DefaultInputComponent implements InputFile private Status status; private Charset charset; private Metadata metadata; - private boolean publish = false; + private boolean publish; public DefaultInputFile(DefaultIndexedFile indexedFile, Consumer<DefaultInputFile> metadataGenerator) { super(indexedFile.batchId()); this.indexedFile = indexedFile; this.metadataGenerator = metadataGenerator; this.metadata = null; + this.publish = false; } - private void checkMetadata() { + public void checkMetadata() { if (metadata == null) { metadataGenerator.accept(this); } } - public void setPublish(boolean publish) { + /** + * @since 6.3 + */ + public DefaultInputFile setPublish(boolean publish) { this.publish = publish; + return this; } + /** + * @since 6.3 + */ public boolean publish() { return publish; } @@ -95,7 +103,7 @@ public class DefaultInputFile extends DefaultInputComponent implements InputFile } /** - * Component key. + * Component key (without branch). */ @Override public String key() { diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/SensorContext.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/SensorContext.java index 9e7ebd82422..1aa685e79c3 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/SensorContext.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/sensor/SensorContext.java @@ -157,7 +157,12 @@ public interface SensorContext { * @since 6.1 */ void addContextProperty(String key, String value); - + + /** + * Indicate that a file should be published in the report sent to SonarQube. + * Files are automatically marked if any data is created for it (issues, highlighting, coverage, etc.). + * @since 6.3 + */ void markForPublishing(InputFile inputFile); } diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/MetadataTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/MetadataTest.java new file mode 100644 index 00000000000..df0ce527645 --- /dev/null +++ b/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/MetadataTest.java @@ -0,0 +1,36 @@ +/* + * SonarQube + * Copyright (C) 2009-2016 SonarSource SA + * mailto:contact AT sonarsource DOT com + * + * 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 02110-1301, USA. + */ +package org.sonar.api.batch.fs.internal; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.Test; + +public class MetadataTest { + @Test + public void testRoundtrip() { + Metadata metadata = new Metadata(10, 20, "hash", new int[] {1, 2}, 30); + assertThat(metadata.lastValidOffset()).isEqualTo(30); + assertThat(metadata.lines()).isEqualTo(10); + assertThat(metadata.nonBlankLines()).isEqualTo(20); + assertThat(metadata.originalLineOffsets()).isEqualTo(new int[] {1, 2}); + assertThat(metadata.hash()).isEqualTo("hash"); + } +} |