use of org.sonar.java.AnalyzerMessage.TextSpan in project sonar-java by SonarSource.
the class PomCheckContextImpl method getSecondaryAnalyzerMessage.
@VisibleForTesting
AnalyzerMessage getSecondaryAnalyzerMessage(JavaCheck check, File file, Location location) {
TextSpan ts;
if (location.onLine()) {
ts = textSpanForLine(file, location.line);
} else {
XmlLocation startLocation = location.tree.startLocation();
XmlLocation endLocation = location.tree.endLocation();
ts = new TextSpan(startLocation.line(), offsetFromColumn(startLocation.column()), endLocation.line(), offsetFromColumn(endLocation.column()));
if (ts.isEmpty()) {
ts = textSpanForLine(file, startLocation.line());
}
}
return new AnalyzerMessage(check, file, ts, location.msg, 0);
}
use of org.sonar.java.AnalyzerMessage.TextSpan in project sonar-java by SonarSource.
the class DefaultJavaFileScannerContextTest method assertMessagePosition.
private static void assertMessagePosition(AnalyzerMessage message, int startLine, int startColumn, int endLine, int endColumn) {
TextSpan location = message.primaryLocation();
assertThat(location.startLine).isEqualTo(startLine);
assertThat(location.startCharacter).isEqualTo(startColumn);
assertThat(location.endLine).isEqualTo(endLine);
assertThat(location.endCharacter).isEqualTo(endColumn);
}
use of org.sonar.java.AnalyzerMessage.TextSpan in project sonar-java by SonarSource.
the class AnalyzerMessageTest method textSpanForTrees.
@Test
public void textSpanForTrees() {
CompilationUnitTree cut = (CompilationUnitTree) JavaParser.createParser().parse("class A {\n}\n");
ClassTree classTree = (ClassTree) cut.types().get(0);
TextSpan textSpan;
textSpan = AnalyzerMessage.textSpanFor(classTree);
assertThat(textSpan.startLine).isEqualTo(1);
assertThat(textSpan.startCharacter).isEqualTo(0);
assertThat(textSpan.endLine).isEqualTo(2);
assertThat(textSpan.endCharacter).isEqualTo(1);
textSpan = AnalyzerMessage.textSpanBetween(classTree.declarationKeyword(), classTree.openBraceToken());
assertThat(textSpan.startLine).isEqualTo(1);
assertThat(textSpan.startCharacter).isEqualTo(0);
assertThat(textSpan.endLine).isEqualTo(1);
assertThat(textSpan.endCharacter).isEqualTo(9);
}
use of org.sonar.java.AnalyzerMessage.TextSpan in project sonar-java by SonarSource.
the class XmlCheckContextImpl method getSecondaryAnalyzerMessage.
@CheckForNull
private AnalyzerMessage getSecondaryAnalyzerMessage(JavaCheck check, File file, XmlDocumentLocation location) {
Integer startLine = nodeLine(location.node);
if (startLine == null) {
return null;
}
String line = sonarComponents.fileLines(file).get(startLine - 1);
TextSpan ts = new TextSpan(startLine, 0, startLine, line.length());
return new AnalyzerMessage(check, file, ts, location.msg, 0);
}
use of org.sonar.java.AnalyzerMessage.TextSpan in project sonar-java by SonarSource.
the class PomCheckContextImplTest method createSonarComponentsMock.
private static SonarComponents createSonarComponentsMock() {
SonarComponents sonarComponents = mock(SonarComponents.class);
Mockito.when(sonarComponents.fileLines(any(File.class))).thenReturn(Arrays.asList("line 1", "line 2", "line 3 is longer"));
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
reportedMessage = "onLine:" + invocation.getArguments()[3];
return null;
}
}).when(sonarComponents).addIssue(any(File.class), eq(CHECK), eq(LINE), anyString(), eq(null));
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
reportedMessage = "onFile:" + invocation.getArguments()[3];
return null;
}
}).when(sonarComponents).addIssue(any(File.class), eq(CHECK), eq(-1), anyString(), eq(null));
doAnswer(new Answer<Void>() {
@Override
public Void answer(InvocationOnMock invocation) throws Throwable {
AnalyzerMessage analyzerMessage = (AnalyzerMessage) invocation.getArguments()[0];
reportedMessage = "analyzerMessage:" + analyzerMessage.getMessage();
for (AnalyzerMessage secondary : analyzerMessage.flows.stream().map(l -> l.get(0)).collect(Collectors.toList())) {
TextSpan location = secondary.primaryLocation();
reportedMessage += ";" + secondary.getMessage() + "[" + location.startLine + ";" + location.startCharacter + "/" + location.endLine + ";" + location.endCharacter + "]";
}
return null;
}
}).when(sonarComponents).reportIssue(any(AnalyzerMessage.class));
return sonarComponents;
}
Aggregations