aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api/src/main
diff options
context:
space:
mode:
Diffstat (limited to 'sonar-plugin-api/src/main')
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/InputFile.java10
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/DefaultInputFile.java5
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/batch/fs/internal/FileMetadata.java14
3 files changed, 15 insertions, 14 deletions
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 4e0af79ccc7..d98180bf417 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
@@ -105,12 +105,18 @@ public interface InputFile extends InputPath {
Status status();
/**
- * Number of physical lines. This method supports all end-of-line characters. Returns
- * zero if the file is empty.
+ * Number of physical lines. This method supports all end-of-line characters. Formula is (number of line break + 1). Returns
+ * 1 if the file is empty.</br> Returns 2 for <tt>foo\nbar</tt>. Returns 3 for <tt>foo\nbar\n</tt>.
*/
int lines();
/**
+ * Check if the file content is empty (ignore potential BOM).
+ * @since 5.2
+ */
+ boolean isEmpty();
+
+ /**
* Return a {@link TextPointer} in the given file.
* @param line Line of the pointer. Start at 1.
* @param lineOffset Offset in the line. Start at 0.
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 4f89a1c0dd3..5190bb1a0f1 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
@@ -104,6 +104,11 @@ public class DefaultInputFile implements InputFile {
return lines;
}
+ @Override
+ public boolean isEmpty() {
+ return lastValidOffset == 0;
+ }
+
/**
* Component key.
*/
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 cb5ea5660af..fa894077be4 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
@@ -66,7 +66,7 @@ public class FileMetadata implements BatchComponent {
}
private static class LineCounter extends CharHandler {
- private int lines = 0;
+ private int lines = 1;
private int nonBlankLines = 0;
private boolean blankLine = true;
boolean alreadyLoggedInvalidCharacter = false;
@@ -80,9 +80,6 @@ public class FileMetadata implements BatchComponent {
@Override
protected void handleAll(char c) {
- if (this.lines == 0) {
- this.lines = 1;
- }
if (!alreadyLoggedInvalidCharacter && c == '\ufffd') {
LOG.warn("Invalid character encountered in file {} at line {} for encoding {}. Please fix file content or configure the encoding to be used using property '{}'.", file,
lines, encoding, CoreProperties.ENCODING_PROPERTY);
@@ -156,20 +153,13 @@ public class FileMetadata implements BatchComponent {
private final MessageDigest lineMd5Digest = DigestUtils.getMd5Digest();
private final StringBuilder sb = new StringBuilder();
private final LineHashConsumer consumer;
- private int line = 0;
+ private int line = 1;
public LineHashComputer(LineHashConsumer consumer) {
this.consumer = consumer;
}
@Override
- protected void handleAll(char c) {
- if (this.line == 0) {
- this.line = 1;
- }
- }
-
- @Override
protected void handleIgnoreEoL(char c) {
if (!Character.isWhitespace(c)) {
sb.append(c);