Search in sources :

Example 1 with RegexCheck

use of org.sonar.java.regex.RegexCheck in project sonar-java by SonarSource.

the class DefaultJavaFileScannerContextTest method report_issue_on_regex_tree_with_secondary.

@Test
void report_issue_on_regex_tree_with_secondary() {
    RegexCheck regexCheck = new RegexCheck() {
    };
    String regex = "x{42}|y{23}";
    RegexTree regexTree = RegexParserTestUtils.assertSuccessfulParse(regex);
    DisjunctionTree disjunctionTree = (DisjunctionTree) regexTree;
    RepetitionTree x42 = (RepetitionTree) disjunctionTree.getAlternatives().get(0);
    CurlyBraceQuantifier rep42 = (CurlyBraceQuantifier) x42.getQuantifier();
    RepetitionTree y23 = (RepetitionTree) disjunctionTree.getAlternatives().get(1);
    CurlyBraceQuantifier rep23 = (CurlyBraceQuantifier) y23.getQuantifier();
    RegexCheck.RegexIssueLocation secondary = new RegexCheck.RegexIssueLocation(rep42, "regexSecondary");
    context.reportIssue(regexCheck, rep23, "regexMsg", null, Collections.singletonList(secondary));
    assertThat(reportedMessage.getMessage()).isEqualTo("regexMsg");
    assertThat(reportedMessage.getCost()).isNull();
    assertMessagePosition(reportedMessage, 3, 8, 3, 12);
    assertThat(reportedMessage.flows).hasSize(1);
    List<AnalyzerMessage> reportedSecondaries = reportedMessage.flows.get(0);
    assertThat(reportedSecondaries).hasSize(1);
    AnalyzerMessage reportedSecondary = reportedSecondaries.get(0);
    assertThat(reportedSecondary.getMessage()).isEqualTo("regexSecondary");
    assertThat(reportedSecondary.getCost()).isNull();
    assertMessagePosition(reportedSecondary, 3, 2, 3, 6);
}
Also used : RepetitionTree(org.sonarsource.analyzer.commons.regex.ast.RepetitionTree) RegexTree(org.sonarsource.analyzer.commons.regex.ast.RegexTree) AnalyzerMessage(org.sonar.java.reporting.AnalyzerMessage) RegexCheck(org.sonar.java.regex.RegexCheck) DisjunctionTree(org.sonarsource.analyzer.commons.regex.ast.DisjunctionTree) CurlyBraceQuantifier(org.sonarsource.analyzer.commons.regex.ast.CurlyBraceQuantifier) Test(org.junit.jupiter.api.Test)

Example 2 with RegexCheck

use of org.sonar.java.regex.RegexCheck in project sonar-java by SonarSource.

the class DefaultJavaFileScannerContextTest method report_issue_on_regex_tree.

@Test
void report_issue_on_regex_tree() {
    RegexCheck regexCheck = new RegexCheck() {
    };
    String regex = "x{42}|y{23}";
    RegexTree regexTree = RegexParserTestUtils.assertSuccessfulParse(regex);
    DisjunctionTree disjunctionTree = (DisjunctionTree) regexTree;
    RepetitionTree y23 = (RepetitionTree) disjunctionTree.getAlternatives().get(1);
    CurlyBraceQuantifier rep23 = (CurlyBraceQuantifier) y23.getQuantifier();
    int cost = 42;
    context.reportIssue(regexCheck, rep23, "regexMsg", cost, Collections.emptyList());
    assertThat(reportedMessage.getMessage()).isEqualTo("regexMsg");
    assertThat(reportedMessage.getCost()).isEqualTo(Double.valueOf(cost));
    assertThat(reportedMessage.flows).isEmpty();
    assertMessagePosition(reportedMessage, 3, 8, 3, 12);
}
Also used : RepetitionTree(org.sonarsource.analyzer.commons.regex.ast.RepetitionTree) RegexTree(org.sonarsource.analyzer.commons.regex.ast.RegexTree) RegexCheck(org.sonar.java.regex.RegexCheck) DisjunctionTree(org.sonarsource.analyzer.commons.regex.ast.DisjunctionTree) CurlyBraceQuantifier(org.sonarsource.analyzer.commons.regex.ast.CurlyBraceQuantifier) Test(org.junit.jupiter.api.Test)

Example 3 with RegexCheck

use of org.sonar.java.regex.RegexCheck 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)

Aggregations

RegexCheck (org.sonar.java.regex.RegexCheck)3 Test (org.junit.jupiter.api.Test)2 AnalyzerMessage (org.sonar.java.reporting.AnalyzerMessage)2 CurlyBraceQuantifier (org.sonarsource.analyzer.commons.regex.ast.CurlyBraceQuantifier)2 DisjunctionTree (org.sonarsource.analyzer.commons.regex.ast.DisjunctionTree)2 RegexTree (org.sonarsource.analyzer.commons.regex.ast.RegexTree)2 RepetitionTree (org.sonarsource.analyzer.commons.regex.ast.RepetitionTree)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1