From 2183aa17c17e8cb7385bf1662a57368c0b2979fd Mon Sep 17 00:00:00 2001 From: Julien HENRY Date: Mon, 31 Jul 2017 10:37:53 +0200 Subject: [PATCH] SONAR-9641 Introduce InputFile:uri() --- .../org/sonar/api/batch/fs/IndexedFile.java | 22 +++++++++++++++++++ .../sonar/api/batch/fs/InputComponent.java | 2 +- .../org/sonar/api/batch/fs/InputFile.java | 8 +++++++ .../org/sonar/api/batch/fs/InputPath.java | 16 ++++++++++++++ .../batch/fs/internal/DefaultIndexedFile.java | 13 +++++++++-- .../batch/fs/internal/DefaultInputDir.java | 6 +++++ .../batch/fs/internal/DefaultInputFile.java | 16 ++++++++++++-- .../fs/internal/DefaultInputFileTest.java | 2 ++ 8 files changed, 80 insertions(+), 5 deletions(-) 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 057c94efa72..fe94fce9c3a 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 @@ -22,6 +22,7 @@ package org.sonar.api.batch.fs; import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.net.URI; import java.nio.file.Path; import javax.annotation.CheckForNull; @@ -42,7 +43,9 @@ public interface IndexedFile extends InputPath { * /path/to/module/src/main/java/com/Foo.java. *
* Relative path is not null and is normalized ('foo/../foo' is replaced by 'foo'). + * @deprecated since 6.6 use {@link #inputStream()}, {@link #filename()} or {@link #uri()} */ + @Deprecated @Override String relativePath(); @@ -52,7 +55,9 @@ public interface IndexedFile extends InputPath { * This is not canonical path. Symbolic links are not resolved. For example if /project/src links * to /tmp/src and basedir is /project, then this method returns /project/src/index.php. Use * {@code file().getCanonicalPath()} to resolve symbolic link. + * @deprecated since 6.6 use {@link #inputStream()}, {@link #filename()} or {@link #uri()} */ + @Deprecated @Override String absolutePath(); @@ -60,7 +65,9 @@ public interface IndexedFile extends InputPath { * The underlying absolute {@link java.io.File}. It should not be used to read the file in the filesystem. * @see #contents() * @see #inputStream() + * @deprecated since 6.6 use {@link #inputStream()}, {@link #filename()} or {@link #uri()} */ + @Deprecated @Override File file(); @@ -70,10 +77,25 @@ public interface IndexedFile extends InputPath { * @see #contents() * @see #inputStream() * @since 5.1 + * @deprecated since 6.6 use {@link #inputStream()}, {@link #filename()} or {@link #uri()} */ + @Deprecated @Override Path path(); + /** + * Identifier of the file. The only guarantee is that it is unique in the project. + * You should not assume it is a file:// URI. + * @since 6.6 + */ + URI uri(); + + /** + * Filename for this file (inclusing extension). For example: MyClass.java. + * @since 6.6 + */ + String filename(); + /** * Language, for example "java" or "php". Can be null if indexation of all files is enabled and no language claims to support the file. */ 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 24ac24ad4fa..34b0d9edd12 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,7 +30,7 @@ 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 (scanner, server, WS...). * It doesn't include the branch. */ String key(); diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/InputFile.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/InputFile.java index 00463b1ed09..92253347869 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/InputFile.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/InputFile.java @@ -63,7 +63,9 @@ public interface InputFile extends IndexedFile { * /path/to/module/src/main/java/com/Foo.java. *
* Relative path is not null and is normalized ('foo/../foo' is replaced by 'foo'). + * @deprecated since 6.6 use {@link #inputStream()}, {@link #filename()} or {@link #uri()} */ + @Deprecated @Override String relativePath(); @@ -73,7 +75,9 @@ public interface InputFile extends IndexedFile { * This is not canonical path. Symbolic links are not resolved. For example if /project/src links * to /tmp/src and basedir is /project, then this method returns /project/src/index.php. Use * {@code file().getCanonicalPath()} to resolve symbolic link. + * @deprecated since 6.6 use {@link #inputStream()}, {@link #filename()} or {@link #uri()} */ + @Deprecated @Override String absolutePath(); @@ -81,7 +85,9 @@ public interface InputFile extends IndexedFile { * The underlying absolute {@link java.io.File}. It should not be used to read the file in the filesystem. * @see #contents() * @see #inputStream() + * @deprecated since 6.6 use {@link #inputStream()}, {@link #filename()} or {@link #uri()} */ + @Deprecated @Override File file(); @@ -91,7 +97,9 @@ public interface InputFile extends IndexedFile { * @see #contents() * @see #inputStream() * @since 5.1 + * @deprecated since 6.6 use {@link #inputStream()}, {@link #filename()} or {@link #uri()} */ + @Deprecated @Override Path path(); diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/InputPath.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/InputPath.java index b85eb289517..274e05a3e44 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/InputPath.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/InputPath.java @@ -20,6 +20,7 @@ package org.sonar.api.batch.fs; import java.io.File; +import java.net.URI; import java.nio.file.Path; /** @@ -34,26 +35,41 @@ public interface InputPath extends InputComponent { /** * @see InputFile#relativePath() * @see InputDir#relativePath() + * @deprecated since 6.5 use {@link #uri()} */ + @Deprecated String relativePath(); /** * @see InputFile#absolutePath() * @see InputDir#absolutePath() + * @deprecated since 6.5 use {@link #uri()} */ + @Deprecated String absolutePath(); /** * @see InputFile#file() * @see InputDir#file() + * @deprecated since 6.5 use {@link #uri()} */ + @Deprecated File file(); /** * @see InputFile#path() * @see InputDir#path() * @since 5.1 + * @deprecated since 6.5 use {@link #uri()} */ + @Deprecated Path path(); + /** + * Identifier of the component. The only guarantee is that it is unique in the project. + * You should not assume it is a file:// URI. + * @since 6.5 + */ + URI uri(); + } 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 b3a0dbbe9ee..a91ab8ad2aa 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 @@ -22,13 +22,12 @@ package org.sonar.api.batch.fs.internal; import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.net.URI; import java.nio.file.Files; import java.nio.file.Path; - import javax.annotation.CheckForNull; import javax.annotation.Nullable; import javax.annotation.concurrent.Immutable; - import org.sonar.api.batch.fs.IndexedFile; import org.sonar.api.batch.fs.InputFile.Type; import org.sonar.api.utils.PathUtils; @@ -142,4 +141,14 @@ public class DefaultIndexedFile extends DefaultInputComponent implements Indexed public boolean isFile() { return true; } + + @Override + public String filename() { + return path().getFileName().toString(); + } + + @Override + public URI uri() { + return path().toUri(); + } } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultInputDir.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultInputDir.java index e271a327dac..79031632b08 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultInputDir.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultInputDir.java @@ -20,6 +20,7 @@ package org.sonar.api.batch.fs.internal; import java.io.File; +import java.net.URI; import java.nio.file.Path; import org.apache.commons.lang.StringUtils; import org.sonar.api.batch.fs.InputDir; @@ -117,4 +118,9 @@ public class DefaultInputDir extends DefaultInputComponent implements InputDir { public String toString() { return "[moduleKey=" + moduleKey + ", relative=" + relativePath + ", basedir=" + moduleBaseDir + "]"; } + + @Override + public URI uri() { + return path().toUri(); + } } 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 b3ea8263c0d..4312c634c04 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 @@ -25,6 +25,7 @@ import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.net.URI; import java.nio.charset.Charset; import java.nio.file.Files; import java.nio.file.Path; @@ -77,8 +78,9 @@ public class DefaultInputFile extends DefaultInputComponent implements InputFile @Override public InputStream inputStream() throws IOException { - return contents != null ? new ByteArrayInputStream(contents.getBytes(charset())) : new BOMInputStream(Files.newInputStream(path()), - ByteOrderMark.UTF_8, ByteOrderMark.UTF_16LE, ByteOrderMark.UTF_16BE, ByteOrderMark.UTF_32LE, ByteOrderMark.UTF_32BE); + return contents != null ? new ByteArrayInputStream(contents.getBytes(charset())) + : new BOMInputStream(Files.newInputStream(path()), + ByteOrderMark.UTF_8, ByteOrderMark.UTF_16LE, ByteOrderMark.UTF_16BE, ByteOrderMark.UTF_32LE, ByteOrderMark.UTF_32BE); } @Override @@ -337,4 +339,14 @@ public class DefaultInputFile extends DefaultInputComponent implements InputFile return true; } + @Override + public String filename() { + return indexedFile.filename(); + } + + @Override + public URI uri() { + return indexedFile.uri(); + } + } diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/DefaultInputFileTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/DefaultInputFileTest.java index 82ccf11b3e8..cf66115c695 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/DefaultInputFileTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/batch/fs/internal/DefaultInputFileTest.java @@ -62,6 +62,8 @@ public class DefaultInputFileTest { assertThat(inputFile.relativePath()).isEqualTo("src/Foo.php"); assertThat(new File(inputFile.relativePath())).isRelative(); assertThat(inputFile.absolutePath()).endsWith("Foo.php"); + assertThat(inputFile.filename()).isEqualTo("Foo.php"); + assertThat(inputFile.uri()).hasPath(baseDir.resolve("src/Foo.php").toUri().getPath()); assertThat(new File(inputFile.absolutePath())).isAbsolute(); assertThat(inputFile.language()).isEqualTo("php"); assertThat(inputFile.status()).isEqualTo(InputFile.Status.ADDED); -- 2.39.5