DefaultJavaFileScannerContext implements JavaFileScannerContext, RegexScannerContext { @Override public void reportIssue(JavaCheck javaCheck, Tree tree, String message) { reportIssue(javaCheck, tree, message, Collections.emptyList(), null); } DefaultJavaFileScannerContext(CompilationUnitTree tree, InputFile inputFile, Sema semanticModel,
                                       @Nullable SonarComponents sonarComponents, JavaVersion javaVersion, boolean fileParsed); @Override CompilationUnitTree getTree(); @Override void addIssueOnFile(JavaCheck javaCheck, String message); @Override void addIssueOnProject(JavaCheck check, String message); @Override void addIssue(int line, JavaCheck javaCheck, String message); @Override void addIssue(int line, JavaCheck javaCheck, String message, @Nullable Integer cost); @Override @Nullable Object getSemanticModel(); @Override JavaVersion getJavaVersion(); @Override boolean fileParsed(); @Deprecated @Override String getFileKey(); @Override void reportIssue(JavaCheck javaCheck, Tree tree, String message); @Override void reportIssue(RegexCheck regexCheck, RegexSyntaxElement regexTree, String message, @Nullable Integer cost, List<RegexCheck.RegexIssueLocation> secondaries); @Override void reportIssue(RegexCheck regexCheck, Tree javaSyntaxElement, String message, @Nullable Integer cost, List<RegexIssueLocation> secondaries); @Override RegexParseResult regexForLiterals(FlagSet initialFlags, LiteralTree... stringLiterals); @Override void reportIssue(JavaCheck javaCheck, Tree syntaxNode, String message, List<Location> secondary, @Nullable Integer cost); @Override void reportIssueWithFlow(JavaCheck javaCheck, Tree syntaxNode, String message, Iterable<List<Location>> flows, @Nullable Integer cost); @Override void reportIssue(JavaCheck javaCheck, Tree startTree, Tree endTree, String message); @Override void reportIssue(JavaCheck javaCheck, Tree startTree, Tree endTree, String message, List<Location> secondary, @Nullable Integer cost); @Override List<String> getFileLines(); @Override String getFileContent(); void reportIssue(AnalyzerMessage message); AnalyzerMessage createAnalyzerMessage(JavaCheck javaCheck, Tree startTree, String message); @Override InputFile getInputFile(); @Override InputComponent getProject(); @Override File getWorkingDirectory(); @Override List<Tree> getComplexityNodes(Tree tree); @Override Optional<SourceMap> sourceMap();  }
@Test void report_issue_between_two_trees() { ClassTree tree = (ClassTree) compilationUnitTree.types().get(0); VariableTree firstMember = (VariableTree) tree.members().get(0); VariableTree secondMember = (VariableTree) tree.members().get(1); context.reportIssue(CHECK, firstMember.simpleName(), secondMember.equalToken(), "msg"); assertThat(reportedMessage.getMessage()).isEqualTo("msg"); assertThat(reportedMessage.getCost()).isNull(); assertThat(reportedMessage.flows).isEmpty(); assertMessagePosition(reportedMessage, 2, 6, 3, 10); }
@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); }
@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); }
@Test void report_issue_on_tree() { context.reportIssue(CHECK, compilationUnitTree, "msg"); assertThat(reportedMessage.getMessage()).isEqualTo("msg"); assertThat(reportedMessage.getCost()).isNull(); assertThat(reportedMessage.flows).isEmpty(); assertMessagePosition(reportedMessage, 1, 0, 4, 1); }
@Test void report_issue_on_tree_with_cross_file_scanner_throws() { assertThatExceptionOfType(UnsupportedOperationException.class) .isThrownBy(() -> context.reportIssue(END_OF_ANALYSIS_CHECK, compilationUnitTree, "msg")) .withMessage("EndOfAnalysisCheck must only call reportIssue with AnalyzerMessage and must never pass a Tree reference."); }
@Test void report_issue_on_tree_with_no_secondary() { ClassTree tree = (ClassTree) compilationUnitTree.types().get(0); context.reportIssue(CHECK, tree.simpleName(), "msg", new ArrayList<>(), null); assertThat(reportedMessage.getMessage()).isEqualTo("msg"); assertThat(reportedMessage.getCost()).isNull(); assertThat(reportedMessage.flows).isEmpty(); assertMessagePosition(reportedMessage, 1, 6, 1, 7); }
@Test void report_issue_on_tree_with_cost() { ClassTree tree = (ClassTree) compilationUnitTree.types().get(0); context.reportIssue(CHECK, tree.simpleName(), "msg", new ArrayList<>(), COST); assertThat(reportedMessage.getMessage()).isEqualTo("msg"); assertThat(reportedMessage.getCost()).isEqualTo(COST); assertThat(reportedMessage.flows).isEmpty(); assertMessagePosition(reportedMessage, 1, 6, 1, 7); }
@Test void report_issue_on_tree_with_secondary() { ClassTree tree = (ClassTree) compilationUnitTree.types().get(0); Tree firstMember = tree.members().get(0); Tree secondMember = tree.members().get(1); ArrayList<Location> secondary = new ArrayList<>(); secondary.add(new JavaFileScannerContext.Location("secondary", firstMember)); secondary.add(new JavaFileScannerContext.Location("secondary", secondMember)); context.reportIssue(CHECK, tree.simpleName(), "msg", secondary, null); assertThat(reportedMessage.getMessage()).isEqualTo("msg"); assertThat(reportedMessage.getCost()).isNull(); assertThat(reportedMessage.flows).hasSize(2); assertMessagePosition(reportedMessage, 1, 6, 1, 7); List<AnalyzerMessage> secondaries = reportedMessage.flows.stream().map(flow -> flow.get(0)).collect(Collectors.toList()); assertThat(secondaries).hasSize(2); assertMessagePosition(secondaries.get(0), 2, 2, 2, 13); assertMessagePosition(secondaries.get(1), 3, 2, 3, 15); }