diff options
author | Duarte Meneses <duarte.meneses@sonarsource.com> | 2020-04-13 10:18:20 -0500 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2020-04-17 20:03:43 +0000 |
commit | 2ba726630087bb0baae61e05e1c20943ec9e6931 (patch) | |
tree | 73d132bc2aecc9f611b1e35d943d5b106aff0944 /sonar-plugin-api-impl/src/main | |
parent | e014a2b976e733f33f369b868dbdf475b06650fe (diff) | |
download | sonarqube-2ba726630087bb0baae61e05e1c20943ec9e6931.tar.gz sonarqube-2ba726630087bb0baae61e05e1c20943ec9e6931.zip |
SONAR-13206 Show warning if scanner fails to detect files' encoding
Diffstat (limited to 'sonar-plugin-api-impl/src/main')
3 files changed, 20 insertions, 7 deletions
diff --git a/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/fs/internal/FileMetadata.java b/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/fs/internal/FileMetadata.java index 8e9cdb11bf6..751af8b7ef4 100644 --- a/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/fs/internal/FileMetadata.java +++ b/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/fs/internal/FileMetadata.java @@ -34,6 +34,7 @@ import org.sonar.api.batch.fs.internal.charhandler.FileHashComputer; import org.sonar.api.batch.fs.internal.charhandler.LineCounter; import org.sonar.api.batch.fs.internal.charhandler.LineHashComputer; import org.sonar.api.batch.fs.internal.charhandler.LineOffsetCounter; +import org.sonar.api.notifications.AnalysisWarnings; /** * Computes hash of files. Ends of Lines are ignored, so files with @@ -43,13 +44,18 @@ import org.sonar.api.batch.fs.internal.charhandler.LineOffsetCounter; public class FileMetadata { private static final char LINE_FEED = '\n'; private static final char CARRIAGE_RETURN = '\r'; + private final AnalysisWarnings analysisWarnings; + + public FileMetadata(AnalysisWarnings analysisWarnings) { + this.analysisWarnings = analysisWarnings; + } /** * Compute hash of a file ignoring line ends differences. * Maximum performance is needed. */ public Metadata readMetadata(InputStream stream, Charset encoding, String filePath, @Nullable CharHandler otherHandler) { - LineCounter lineCounter = new LineCounter(filePath, encoding); + LineCounter lineCounter = new LineCounter(analysisWarnings, filePath, encoding); FileHashComputer fileHashComputer = new FileHashComputer(filePath); LineOffsetCounter lineOffsetCounter = new LineOffsetCounter(); @@ -73,7 +79,7 @@ public class FileMetadata { * For testing purpose */ public Metadata readMetadata(Reader reader) { - LineCounter lineCounter = new LineCounter("fromString", StandardCharsets.UTF_16); + LineCounter lineCounter = new LineCounter(analysisWarnings, "fromString", StandardCharsets.UTF_16); FileHashComputer fileHashComputer = new FileHashComputer("fromString"); LineOffsetCounter lineOffsetCounter = new LineOffsetCounter(); CharHandler[] handlers = {lineCounter, fileHashComputer, lineOffsetCounter}; diff --git a/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/fs/internal/TestInputFileBuilder.java b/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/fs/internal/TestInputFileBuilder.java index b0e9d79c3e8..cd5ff8ed3de 100644 --- a/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/fs/internal/TestInputFileBuilder.java +++ b/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/fs/internal/TestInputFileBuilder.java @@ -31,6 +31,7 @@ import javax.annotation.CheckForNull; import javax.annotation.Nullable; import org.sonar.api.batch.bootstrap.ProjectDefinition; import org.sonar.api.batch.fs.InputFile; +import org.sonar.api.notifications.AnalysisWarnings; import org.sonar.api.utils.PathUtils; /** @@ -207,7 +208,8 @@ public class TestInputFileBuilder { } public TestInputFileBuilder initMetadata(String content) { - return setMetadata(new FileMetadata().readMetadata(new StringReader(content))); + AnalysisWarnings analysisWarnings = (warning) -> {}; + return setMetadata(new FileMetadata(analysisWarnings).readMetadata(new StringReader(content))); } public DefaultInputFile build() { diff --git a/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/fs/internal/charhandler/LineCounter.java b/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/fs/internal/charhandler/LineCounter.java index 95a4426cae3..cb3e7c563eb 100644 --- a/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/fs/internal/charhandler/LineCounter.java +++ b/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/fs/internal/charhandler/LineCounter.java @@ -21,20 +21,24 @@ package org.sonar.api.batch.fs.internal.charhandler; import java.nio.charset.Charset; import org.sonar.api.CoreProperties; +import org.sonar.api.notifications.AnalysisWarnings; import org.sonar.api.utils.log.Logger; import org.sonar.api.utils.log.Loggers; public class LineCounter extends CharHandler { private static final Logger LOG = Loggers.get(LineCounter.class); - + + private final AnalysisWarnings analysisWarnings; + private final String filePath; + private final Charset encoding; + private int lines = 1; private int nonBlankLines = 0; private boolean blankLine = true; boolean alreadyLoggedInvalidCharacter = false; - private final String filePath; - private final Charset encoding; - public LineCounter(String filePath, Charset encoding) { + public LineCounter(AnalysisWarnings analysisWarnings, String filePath, Charset encoding) { + this.analysisWarnings = analysisWarnings; this.filePath = filePath; this.encoding = encoding; } @@ -42,6 +46,7 @@ public class LineCounter extends CharHandler { @Override public void handleAll(char c) { if (!alreadyLoggedInvalidCharacter && c == '\ufffd') { + analysisWarnings.addUnique("There are problems with file encoding in the source code. Please check the scanner logs for more details."); LOG.warn("Invalid character encountered in file {} at line {} for encoding {}. Please fix file content or configure the encoding to be used using property '{}'.", filePath, lines, encoding, CoreProperties.ENCODING_PROPERTY); alreadyLoggedInvalidCharacter = true; |