aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api
diff options
context:
space:
mode:
authorDuarte Meneses <duarte.meneses@sonarsource.com>2017-04-05 14:21:53 +0200
committerdbmeneses <duarte.meneses@sonarsource.com>2017-04-11 13:39:57 +0200
commit964fff7ca9f072e6e084c0fac605980f19bc0278 (patch)
tree0176917753cd10f0c776492bc33c1cf05ea77613 /sonar-plugin-api
parent2489c157e7a4e563c7279b57f12c197aa53d28b5 (diff)
downloadsonarqube-964fff7ca9f072e6e084c0fac605980f19bc0278.tar.gz
sonarqube-964fff7ca9f072e6e084c0fac605980f19bc0278.zip
SONAR-9025 Optimize computation issue exclusions
Diffstat (limited to 'sonar-plugin-api')
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/FileMetadata.java31
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/scan/issue/filter/FilterableIssue.java5
2 files changed, 27 insertions, 9 deletions
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/FileMetadata.java b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/FileMetadata.java
index cf962b2c796..4ddcf1d8d4e 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/FileMetadata.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/FileMetadata.java
@@ -33,8 +33,10 @@ import java.nio.charset.CharsetEncoder;
import java.nio.charset.CodingErrorAction;
import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
+
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
+
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.ByteOrderMark;
@@ -270,15 +272,26 @@ public class FileMetadata {
* Compute hash of a file ignoring line ends differences.
* Maximum performance is needed.
*/
- public Metadata readMetadata(File file, Charset encoding) {
+ public Metadata readMetadata(File file, Charset encoding, @Nullable CharHandler otherHandler) {
LineCounter lineCounter = new LineCounter(file, encoding);
FileHashComputer fileHashComputer = new FileHashComputer(file);
LineOffsetCounter lineOffsetCounter = new LineOffsetCounter();
- readFile(file, encoding, lineCounter, fileHashComputer, lineOffsetCounter);
+
+ if (otherHandler != null) {
+ CharHandler[] handlers = {lineCounter, fileHashComputer, lineOffsetCounter, otherHandler};
+ readFile(file, encoding, handlers);
+ } else {
+ CharHandler[] handlers = {lineCounter, fileHashComputer, lineOffsetCounter};
+ readFile(file, encoding, handlers);
+ }
return new Metadata(lineCounter.lines(), lineCounter.nonBlankLines(), fileHashComputer.getHash(), lineOffsetCounter.getOriginalLineOffsets(),
lineOffsetCounter.getLastValidOffset());
}
+ public Metadata readMetadata(File file, Charset encoding) {
+ return readMetadata(file, encoding, null);
+ }
+
/**
* For testing purpose
*/
@@ -286,16 +299,18 @@ public class FileMetadata {
LineCounter lineCounter = new LineCounter(new File("fromString"), StandardCharsets.UTF_16);
FileHashComputer fileHashComputer = new FileHashComputer(new File("fromString"));
LineOffsetCounter lineOffsetCounter = new LineOffsetCounter();
+ CharHandler[] handlers = {lineCounter, fileHashComputer, lineOffsetCounter};
+
try {
- read(reader, lineCounter, fileHashComputer, lineOffsetCounter);
+ read(reader, handlers);
} catch (IOException e) {
- throw new IllegalStateException("Should never occurs", e);
+ throw new IllegalStateException("Should never occur", e);
}
return new Metadata(lineCounter.lines(), lineCounter.nonBlankLines(), fileHashComputer.getHash(), lineOffsetCounter.getOriginalLineOffsets(),
lineOffsetCounter.getLastValidOffset());
}
- public static void readFile(File file, Charset encoding, CharHandler... handlers) {
+ public static void readFile(File file, Charset encoding, CharHandler[] handlers) {
try (BOMInputStream bomIn = new BOMInputStream(new FileInputStream(file),
ByteOrderMark.UTF_8, ByteOrderMark.UTF_16LE, ByteOrderMark.UTF_16BE, ByteOrderMark.UTF_32LE, ByteOrderMark.UTF_32BE);
Reader reader = new BufferedReader(new InputStreamReader(bomIn, encoding))) {
@@ -305,7 +320,7 @@ public class FileMetadata {
}
}
- private static void read(Reader reader, CharHandler... handlers) throws IOException {
+ private static void read(Reader reader, CharHandler[] handlers) throws IOException {
char c;
int i = reader.read();
boolean afterCR = false;
@@ -354,15 +369,13 @@ public class FileMetadata {
@FunctionalInterface
public interface LineHashConsumer {
-
void consume(int lineIdx, @Nullable byte[] hash);
-
}
/**
* Compute a MD5 hash of each line of the file after removing of all blank chars
*/
public static void computeLineHashesForIssueTracking(InputFile f, LineHashConsumer consumer) {
- readFile(f.file(), f.charset(), new LineHashComputer(consumer, f.file()));
+ readFile(f.file(), f.charset(), new CharHandler[] {new LineHashComputer(consumer, f.file())});
}
}
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/scan/issue/filter/FilterableIssue.java b/sonar-plugin-api/src/main/java/org/sonar/api/scan/issue/filter/FilterableIssue.java
index 2068d49a24b..6928b75e036 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/scan/issue/filter/FilterableIssue.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/scan/issue/filter/FilterableIssue.java
@@ -20,6 +20,9 @@
package org.sonar.api.scan.issue.filter;
import java.util.Date;
+
+import javax.annotation.CheckForNull;
+
import org.sonar.api.rule.RuleKey;
/**
@@ -35,6 +38,7 @@ public interface FilterableIssue {
String message();
+ @CheckForNull
Integer line();
/**
@@ -46,6 +50,7 @@ public interface FilterableIssue {
/**
* @since 5.5
*/
+ @CheckForNull
Double gap();
Date creationDate();