aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api-impl
diff options
context:
space:
mode:
authorMartin Strecker <103252490+martin-strecker-sonarsource@users.noreply.github.com>2024-09-26 09:52:57 +0200
committersonartech <sonartech@sonarsource.com>2024-09-26 20:03:14 +0000
commit1ecf4a4f63829bb8c861b760945b094f06704b6e (patch)
tree41555c008c9437a07a2ba4e87fc49d33725767b6 /sonar-plugin-api-impl
parent845f2eee0588f98fd3fc1d111e70aea9979a45eb (diff)
downloadsonarqube-1ecf4a4f63829bb8c861b760945b094f06704b6e.tar.gz
sonarqube-1ecf4a4f63829bb8c861b760945b094f06704b6e.zip
SONAR-23160 Improve exception message in DefaultSymbol.newReference (#10823)
Diffstat (limited to 'sonar-plugin-api-impl')
-rw-r--r--sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/sensor/symbol/internal/DefaultSymbolTable.java6
-rw-r--r--sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/symbol/internal/DefaultSymbolTableTest.java16
2 files changed, 20 insertions, 2 deletions
diff --git a/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/sensor/symbol/internal/DefaultSymbolTable.java b/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/sensor/symbol/internal/DefaultSymbolTable.java
index 91ef8fddb6a..d81ef8914be 100644
--- a/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/sensor/symbol/internal/DefaultSymbolTable.java
+++ b/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/sensor/symbol/internal/DefaultSymbolTable.java
@@ -107,7 +107,11 @@ public class DefaultSymbolTable extends DefaultStorable implements NewSymbolTabl
@Override
public NewSymbol newReference(TextRange range) {
requireNonNull(range, "Provided range is null");
- checkArgument(!declaration.overlap(range), "Overlapping symbol declaration and reference for symbol at %s", declaration);
+ checkArgument(!declaration.overlap(range),
+ "Overlapping symbol declaration and reference for symbol declared at %s and referenced at %s in file %s",
+ declaration,
+ range,
+ inputFile);
references.add(range);
return this;
}
diff --git a/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/symbol/internal/DefaultSymbolTableTest.java b/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/symbol/internal/DefaultSymbolTableTest.java
index 5e9d8dbc65d..1ead8dbef1c 100644
--- a/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/symbol/internal/DefaultSymbolTableTest.java
+++ b/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/sensor/symbol/internal/DefaultSymbolTableTest.java
@@ -27,8 +27,10 @@ import org.sonar.api.batch.fs.InputFile;
import org.sonar.api.batch.fs.TextRange;
import org.sonar.api.batch.fs.internal.TestInputFileBuilder;
import org.sonar.api.batch.sensor.internal.SensorStorage;
+import org.sonar.api.batch.sensor.symbol.NewSymbol;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.mock;
public class DefaultSymbolTableTest {
@@ -51,7 +53,7 @@ public class DefaultSymbolTableTest {
symbolTableBuilder
.newSymbol(1, 0, 1, 10)
.newReference(2, 10, 2, 15)
- .newReference(1, 16, 1, 20);
+ .newReference(1, 16, 1, 20);
symbolTableBuilder
.newSymbol(1, 12, 1, 15)
@@ -63,6 +65,18 @@ public class DefaultSymbolTableTest {
}
@Test
+ public void fail_on_reference_overlaps_declaration() {
+ NewSymbol symbol = new DefaultSymbolTable(mock(SensorStorage.class))
+ .onFile(INPUT_FILE)
+ .newSymbol(1, 0, 1, 10);
+
+ assertThatThrownBy(() -> symbol.newReference(1, 3, 1, 12))
+ .isInstanceOf(IllegalArgumentException.class)
+ .hasMessage(
+ "Overlapping symbol declaration and reference for symbol declared at Range[from [line=1, lineOffset=0] to [line=1, lineOffset=10]] and referenced at Range[from [line=1, lineOffset=3] to [line=1, lineOffset=12]] in file src/Foo.java");
+ }
+
+ @Test
public void should_register_symbols() {
assertThat(referencesPerSymbol).hasSize(2);
}