Search in sources :

Example 21 with AnalyzerMessage

use of org.sonar.java.reporting.AnalyzerMessage in project sonar-java by SonarSource.

the class IssuableSubscriptionVisitorTest method test_custom_rules_report_issues.

@Test
void test_custom_rules_report_issues() throws Exception {
    VisitorsBridgeForTests visitorsBridge = new VisitorsBridgeForTests(Collections.singletonList(new CustomRule()), new ArrayList<>(), null, new JavaVersionImpl());
    JavaAstScanner.scanSingleFileForTests(TestUtils.inputFile("src/test/resources/IssuableSubscriptionClass.java"), visitorsBridge);
    Set<AnalyzerMessage> issues = visitorsBridge.lastCreatedTestContext().getIssues();
    assertThat(issues).hasSize(7);
}
Also used : JavaVersionImpl(org.sonar.java.model.JavaVersionImpl) AnalyzerMessage(org.sonar.java.reporting.AnalyzerMessage) VisitorsBridgeForTests(org.sonar.java.testing.VisitorsBridgeForTests) Test(org.junit.jupiter.api.Test)

Example 22 with AnalyzerMessage

use of org.sonar.java.reporting.AnalyzerMessage in project sonar-java by SonarSource.

the class SpringBeansShouldBeAccessibleCheck method addMessageToMap.

private void addMessageToMap(String classPackageName, IdentifierTree classNameTree) {
    DefaultJavaFileScannerContext defaultContext = (DefaultJavaFileScannerContext) context;
    AnalyzerMessage analyzerMessage = defaultContext.createAnalyzerMessage(this, classNameTree, String.format(MESSAGE_FORMAT, classNameTree.name()));
    messagesPerPackage.computeIfAbsent(classPackageName, k -> new ArrayList<>()).add(analyzerMessage);
}
Also used : InputFile(org.sonar.api.batch.fs.InputFile) Arrays(java.util.Arrays) EndOfAnalysis(org.sonar.plugins.java.api.internal.EndOfAnalysis) DefaultJavaFileScannerContext(org.sonar.java.model.DefaultJavaFileScannerContext) HashMap(java.util.HashMap) ModuleScannerContext(org.sonar.plugins.java.api.ModuleScannerContext) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Loggers(org.sonar.api.utils.log.Loggers) SymbolMetadata(org.sonar.plugins.java.api.semantic.SymbolMetadata) Map(java.util.Map) IssuableSubscriptionVisitor(org.sonar.plugins.java.api.IssuableSubscriptionVisitor) SetUtils(org.sonarsource.analyzer.commons.collections.SetUtils) Logger(org.sonar.api.utils.log.Logger) InputFileScannerContext(org.sonar.plugins.java.api.InputFileScannerContext) Collection(java.util.Collection) Set(java.util.Set) AnalyzerMessage(org.sonar.java.reporting.AnalyzerMessage) Tree(org.sonar.plugins.java.api.tree.Tree) Collectors(java.util.stream.Collectors) JavaFileScannerContext(org.sonar.plugins.java.api.JavaFileScannerContext) StandardCharsets(java.nio.charset.StandardCharsets) Objects(java.util.Objects) DefaultModuleScannerContext(org.sonar.java.model.DefaultModuleScannerContext) List(java.util.List) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) Optional(java.util.Optional) Rule(org.sonar.check.Rule) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) Collections(java.util.Collections) Symbol(org.sonar.plugins.java.api.semantic.Symbol) DefaultJavaFileScannerContext(org.sonar.java.model.DefaultJavaFileScannerContext) ArrayList(java.util.ArrayList) AnalyzerMessage(org.sonar.java.reporting.AnalyzerMessage)

Example 23 with AnalyzerMessage

use of org.sonar.java.reporting.AnalyzerMessage in project sonar-java by SonarSource.

the class CommentedOutCodeLineCheck method collectIssues.

public AnalyzerMessage collectIssues(List<AnalyzerMessage> issues, SyntaxTrivia syntaxTrivia, @Nullable AnalyzerMessage previousRelatedIssue) {
    String[] lines = syntaxTrivia.comment().split("\r\n?|\n");
    AnalyzerMessage issue = previousRelatedIssue;
    for (int lineOffset = 0; lineOffset < lines.length; lineOffset++) {
        String line = lines[lineOffset];
        if (!isJavadocLink(line) && codeRecognizer.isLineOfCode(line)) {
            int startLine = syntaxTrivia.range().start().line() + lineOffset;
            int startColumnOffset = (lineOffset == 0 ? syntaxTrivia.range().start().columnOffset() : 0);
            if (issue != null) {
                issue.flows.add(Collections.singletonList(createAnalyzerMessage(startLine, startColumnOffset, line, "Code")));
            } else {
                issue = createAnalyzerMessage(startLine, startColumnOffset, line, MESSAGE);
                issues.add(issue);
            }
        }
    }
    return issue;
}
Also used : AnalyzerMessage(org.sonar.java.reporting.AnalyzerMessage)

Example 24 with AnalyzerMessage

use of org.sonar.java.reporting.AnalyzerMessage in project sonar-java by SonarSource.

the class DefaultJavaFileScannerContext method reportIssue.

private void reportIssue(RegexCheck regexCheck, AnalyzerMessage.TextSpan mainLocation, String message, @Nullable Integer cost, List<RegexCheck.RegexIssueLocation> secondaries) {
    List<List<RegexCheck.RegexIssueLocation>> secondariesAsFlows = new ArrayList<>();
    secondaries.stream().flatMap(regexIssueLocation -> regexIssueLocation.toSingleLocationItems().stream()).map(Collections::singletonList).forEach(secondariesAsFlows::add);
    AnalyzerMessage analyzerMessage = new AnalyzerMessage(regexCheck, inputFile, mainLocation, message, cost != null ? cost : 0);
    completeAnalyzerMessageWithFlows(analyzerMessage, secondariesAsFlows, ril -> ril.locations().get(0), RegexCheck.RegexIssueLocation::message);
    reportIssue(analyzerMessage);
}
Also used : ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) AnalyzerMessage(org.sonar.java.reporting.AnalyzerMessage) RegexCheck(org.sonar.java.regex.RegexCheck)

Example 25 with AnalyzerMessage

use of org.sonar.java.reporting.AnalyzerMessage in project sonar-java by SonarSource.

the class DefaultJavaFileScannerContext method createAnalyzerMessage.

protected static AnalyzerMessage createAnalyzerMessage(InputFile inputFile, JavaCheck javaCheck, Tree startTree, @Nullable Tree endTree, String message, Iterable<List<Location>> flows, @Nullable Integer cost) {
    AnalyzerMessage.TextSpan location = endTree != null ? AnalyzerMessage.textSpanBetween(startTree, endTree) : AnalyzerMessage.textSpanFor(startTree);
    AnalyzerMessage analyzerMessage = new AnalyzerMessage(javaCheck, inputFile, location, message, cost != null ? cost : 0);
    completeAnalyzerMessageWithFlows(analyzerMessage, flows, loc -> AnalyzerMessage.textSpanFor(loc.syntaxNode), loc -> loc.msg);
    return analyzerMessage;
}
Also used : AnalyzerMessage(org.sonar.java.reporting.AnalyzerMessage)

Aggregations

AnalyzerMessage (org.sonar.java.reporting.AnalyzerMessage)30 Test (org.junit.jupiter.api.Test)11 InputFile (org.sonar.api.batch.fs.InputFile)10 ArrayList (java.util.ArrayList)9 List (java.util.List)8 JavaFileScannerContext (org.sonar.plugins.java.api.JavaFileScannerContext)8 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)7 JavaCheck (org.sonar.plugins.java.api.JavaCheck)7 SonarComponents (org.sonar.java.SonarComponents)6 Tree (org.sonar.plugins.java.api.tree.Tree)5 File (java.io.File)4 Collections (java.util.Collections)4 Set (java.util.Set)4 Collectors (java.util.stream.Collectors)4 VisitorsBridgeForTests (org.sonar.java.testing.VisitorsBridgeForTests)4 HashSet (java.util.HashSet)3 Optional (java.util.Optional)3 ValueSource (org.junit.jupiter.params.provider.ValueSource)3 InputComponent (org.sonar.api.batch.fs.InputComponent)3 SensorContextTester (org.sonar.api.batch.sensor.internal.SensorContextTester)3