Search in sources :

Example 1 with TextSpan

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);
}
Also used : TextSpan(org.sonar.java.AnalyzerMessage.TextSpan) XmlLocation(org.sonar.maven.model.XmlLocation) AnalyzerMessage(org.sonar.java.AnalyzerMessage) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 2 with TextSpan

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);
}
Also used : TextSpan(org.sonar.java.AnalyzerMessage.TextSpan)

Example 3 with TextSpan

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);
}
Also used : TextSpan(org.sonar.java.AnalyzerMessage.TextSpan) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) Test(org.junit.Test)

Example 4 with TextSpan

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);
}
Also used : TextSpan(org.sonar.java.AnalyzerMessage.TextSpan) AnalyzerMessage(org.sonar.java.AnalyzerMessage) CheckForNull(javax.annotation.CheckForNull)

Example 5 with TextSpan

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;
}
Also used : TextSpan(org.sonar.java.AnalyzerMessage.TextSpan) SonarComponents(org.sonar.java.SonarComponents) InvocationOnMock(org.mockito.invocation.InvocationOnMock) AnalyzerMessage(org.sonar.java.AnalyzerMessage) File(java.io.File)

Aggregations

TextSpan (org.sonar.java.AnalyzerMessage.TextSpan)5 AnalyzerMessage (org.sonar.java.AnalyzerMessage)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 File (java.io.File)1 CheckForNull (javax.annotation.CheckForNull)1 Test (org.junit.Test)1 InvocationOnMock (org.mockito.invocation.InvocationOnMock)1 SonarComponents (org.sonar.java.SonarComponents)1 XmlLocation (org.sonar.maven.model.XmlLocation)1 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)1 CompilationUnitTree (org.sonar.plugins.java.api.tree.CompilationUnitTree)1