use of org.sonar.java.reporting.AnalyzerMessage in project sonar-java by SonarSource.
the class DefaultJavaFileScannerContext method completeAnalyzerMessageWithFlows.
private static <L> void completeAnalyzerMessageWithFlows(AnalyzerMessage analyzerMessage, Iterable<List<L>> flows, Function<L, AnalyzerMessage.TextSpan> flowItemLocationProdivder, Function<L, String> flowItemMessageProvider) {
JavaCheck check = analyzerMessage.getCheck();
InputComponent component = analyzerMessage.getInputComponent();
for (List<L> flow : flows) {
List<AnalyzerMessage> sonarqubeFlow = flow.stream().map(l -> new AnalyzerMessage(check, component, flowItemLocationProdivder.apply(l), flowItemMessageProvider.apply(l), 0)).collect(Collectors.toList());
analyzerMessage.flows.add(sonarqubeFlow);
}
}
use of org.sonar.java.reporting.AnalyzerMessage in project sonar-java by SonarSource.
the class FilterVerifier method verify.
public static void verify(String filename, JavaIssueFilter filter, JavaCheck... extraJavaChecks) {
IssueCollector issueCollector = new IssueCollector();
List<JavaCheck> visitors = new ArrayList<>();
visitors.add(filter);
visitors.add(issueCollector);
// instantiate the rules filtered by the filter
visitors.addAll(instantiateRules(filter.filteredRules()));
visitors.addAll(Arrays.asList(extraJavaChecks));
Collection<File> classpath = FileUtils.listFiles(new File(FilesUtils.DEFAULT_TEST_JARS_DIRECTORY), new String[] { "jar", "zip" }, true);
List<File> projectClasspath = new ArrayList<>(classpath);
projectClasspath.add(new File("target/test-classes"));
InputFile inputFile = TestUtils.inputFile(filename);
VisitorsBridgeForTests visitorsBridge = new VisitorsBridgeForTests(visitors, projectClasspath, sonarComponents(inputFile), new JavaVersionImpl());
JavaAstScanner.scanSingleFileForTests(inputFile, visitorsBridge);
JavaFileScannerContextForTests testJavaFileScannerContext = visitorsBridge.lastCreatedTestContext();
Map<Integer, Set<String>> issuesByLines = new HashMap<>();
Set<AnalyzerMessage> issues = testJavaFileScannerContext.getIssues();
for (AnalyzerMessage analyzerMessage : issues) {
Integer issueLine = analyzerMessage.getLine();
String ruleKey = AnnotationUtils.getAnnotation(analyzerMessage.getCheck().getClass(), Rule.class).key();
FilterableIssue issue = mock(FilterableIssue.class);
when(issue.ruleKey()).thenReturn(RuleKey.of("java", ruleKey));
when(issue.componentKey()).thenReturn(inputFile.key());
when(issue.line()).thenReturn(issueLine);
if (issueCollector.rejectedIssuesLines.contains(issueLine)) {
assertThat(filter.accept(issue)).overridingErrorMessage("Line #" + issueLine + " has been marked with 'NoIssue' but issue of rule '" + ruleKey + "' has been accepted!").isFalse();
} else if (issueCollector.acceptedIssuesLines.contains(issueLine)) {
// force check on accepted issues
assertThat(filter.accept(issue)).overridingErrorMessage("Line #" + issueLine + " has been marked with 'WithIssue' but no issue have been raised!").isTrue();
} else {
issuesByLines.computeIfAbsent(issueLine, k -> new HashSet<>()).add(ruleKey);
}
}
if (!issuesByLines.isEmpty()) {
List<Integer> lines = new ArrayList<>(issuesByLines.keySet());
Collections.sort(lines);
StringBuilder builder = new StringBuilder();
for (Integer line : lines) {
builder.append("\n#" + line + ": " + issuesByLines.get(line).toString());
}
Fail.fail("The following lines have not been marked with 'WithIssue' or 'NoIssue' and raised issues:" + builder.toString());
}
}
use of org.sonar.java.reporting.AnalyzerMessage in project sonar-java by SonarSource.
the class SonarComponents method reportIssue.
@VisibleForTesting
void reportIssue(AnalyzerMessage analyzerMessage, RuleKey key, InputComponent fileOrProject, @Nullable Double cost) {
Objects.requireNonNull(context);
JavaIssue issue = JavaIssue.create(context, key, cost);
AnalyzerMessage.TextSpan textSpan = analyzerMessage.primaryLocation();
if (textSpan == null) {
// either an issue at file or project level
issue.setPrimaryLocationOnComponent(fileOrProject, analyzerMessage.getMessage());
} else {
if (!textSpan.onLine()) {
Preconditions.checkState(!textSpan.isEmpty(), "Issue location should not be empty");
}
issue.setPrimaryLocation((InputFile) fileOrProject, analyzerMessage.getMessage(), textSpan.startLine, textSpan.startCharacter, textSpan.endLine, textSpan.endCharacter);
}
if (!analyzerMessage.flows.isEmpty()) {
issue.addFlow((InputFile) analyzerMessage.getInputComponent(), analyzerMessage.flows);
}
issue.save();
}
use of org.sonar.java.reporting.AnalyzerMessage in project sonar-java by SonarSource.
the class InternalCheckVerifier method validateLocation.
private static void validateLocation(AnalyzerMessage analyzerMessage, Map<Expectations.IssueAttribute, Object> attrs) {
AnalyzerMessage.TextSpan textSpan = analyzerMessage.primaryLocation();
Objects.requireNonNull(textSpan);
assertAttributeMatch(analyzerMessage, normalizeColumn(textSpan.startCharacter), attrs, START_COLUMN);
assertAttributeMatch(analyzerMessage, textSpan.endLine, attrs, END_LINE);
assertAttributeMatch(analyzerMessage, normalizeColumn(textSpan.endCharacter), attrs, END_COLUMN);
}
use of org.sonar.java.reporting.AnalyzerMessage in project sonar-java by SonarSource.
the class InternalCheckVerifier method validateFlowAttributes.
private void validateFlowAttributes(List<AnalyzerMessage> actual, String flowId) {
SortedSet<Expectations.FlowComment> expected = expectations.flows.get(flowId);
validateFlowMessages(actual, flowId, expected);
Iterator<AnalyzerMessage> actualIterator = actual.iterator();
Iterator<Expectations.FlowComment> expectedIterator = expected.iterator();
while (actualIterator.hasNext() && expectedIterator.hasNext()) {
AnalyzerMessage actualFlow = actualIterator.next();
if (actualFlow.primaryLocation() == null) {
throw new AssertionError(String.format("Flow without location: %s", actualFlow));
}
validateLocation(actualFlow, expectedIterator.next().attributes);
}
}
Aggregations