use of org.sonar.java.reporting.AnalyzerMessage in project sonar-java by SonarSource.
the class DefaultJavaFileScannerContextTest method createSonarComponentsMock.
private SonarComponents createSonarComponentsMock() {
SonarComponents sonarComponents = mock(SonarComponents.class);
doAnswer(invocation -> {
reportedMessage = (AnalyzerMessage) invocation.getArguments()[0];
return null;
}).when(sonarComponents).reportIssue(any(AnalyzerMessage.class));
doAnswer(invocation -> {
Integer cost = invocation.getArgument(4);
reportedMessage = new AnalyzerMessage(invocation.getArgument(1), invocation.getArgument(0), null, invocation.getArgument(3), cost != null ? cost : 0);
return null;
}).when(sonarComponents).addIssue(any(InputComponent.class), any(JavaCheck.class), anyInt(), anyString(), any());
when(sonarComponents.fileLines(any(InputFile.class))).thenReturn(Arrays.asList("1st line", "2nd line"));
when(sonarComponents.inputFileContents(any(InputFile.class))).thenReturn("content");
when(sonarComponents.workDir()).thenReturn(WORK_DIR);
when(sonarComponents.project()).thenReturn(PROJECT_BASE_DIR);
return sonarComponents;
}
use of org.sonar.java.reporting.AnalyzerMessage in project sonar-java by SonarSource.
the class CommentedOutCodeLineCheck method visitToken.
@Override
public void visitToken(SyntaxToken syntaxToken) {
List<AnalyzerMessage> issues = new ArrayList<>();
AnalyzerMessage previousRelatedIssue = null;
int previousCommentLine = -1;
for (SyntaxTrivia syntaxTrivia : syntaxToken.trivias()) {
int currentCommentLine = syntaxTrivia.range().start().line();
if (currentCommentLine != previousCommentLine + 1 && currentCommentLine != previousCommentLine) {
previousRelatedIssue = null;
}
if (!isHeader(syntaxTrivia) && !isJavadoc(syntaxTrivia.comment()) && !isJSNI(syntaxTrivia.comment())) {
previousRelatedIssue = collectIssues(issues, syntaxTrivia, previousRelatedIssue);
previousCommentLine = currentCommentLine;
}
}
DefaultJavaFileScannerContext scannerContext = (DefaultJavaFileScannerContext) this.context;
issues.forEach(scannerContext::reportIssue);
}
use of org.sonar.java.reporting.AnalyzerMessage in project sonar-java by SonarSource.
the class ExcessiveContentRequestCheck method visitNode.
@Override
public void visitNode(Tree tree) {
DefaultJavaFileScannerContext defaultContext = (DefaultJavaFileScannerContext) context;
if (tree.is(Tree.Kind.NEW_CLASS)) {
NewClassTree newClassTree = (NewClassTree) tree;
if (MULTIPART_CONSTRUCTOR.matches(newClassTree)) {
// Create an issue that we will report only at the end of the analysis if the maximum size was never set.
AnalyzerMessage analyzerMessage = defaultContext.createAnalyzerMessage(this, newClassTree, MESSAGE_SIZE_NOT_SET);
multipartConstructorIssues.add(analyzerMessage);
currentFileInstantiates = true;
}
} else {
MethodInvocationTree mit = (MethodInvocationTree) tree;
if (METHODS_SETTING_MAX_SIZE.matches(mit)) {
currentFileSetsMaximumSize = true;
sizeSetSomewhere = true;
getIfExceedSize(mit.arguments().get(0)).map(bytesExceeding -> defaultContext.createAnalyzerMessage(this, mit, String.format(MESSAGE_EXCEED_SIZE, bytesExceeding, fileUploadSizeLimit))).ifPresent(defaultContext::reportIssue);
}
}
}
use of org.sonar.java.reporting.AnalyzerMessage in project sonar-java by SonarSource.
the class ParsingErrorCheckTest method javacEmptyStatementsInImportsBug.
/**
* Related to SONARJAVA-3486: discrepancies between JLS, javac (more tolerant than JLS) and ECJ (strict to JLS).
* According to JLS (ยง7.3 Compilation Units), empty statements are not allowed in import section.
*/
@Test
void javacEmptyStatementsInImportsBug() {
SonarComponents sonarComponents = mock(SonarComponents.class);
when(sonarComponents.inputFileContents(any())).thenCallRealMethod();
VisitorsBridgeForTests visitorsBridge = new VisitorsBridgeForTests(new ParsingErrorCheck(), sonarComponents);
JavaAstScanner.scanSingleFileForTests(TestUtils.inputFile("src/test/files/checks/parsing/EmptyStatementsInImportsBug.java"), visitorsBridge);
Set<AnalyzerMessage> issues = visitorsBridge.lastCreatedTestContext().getIssues();
assertThat(issues).hasSize(1);
AnalyzerMessage issue = issues.iterator().next();
assertThat(issue.getLine()).isEqualTo(3);
assertThat(issue.getMessage()).isEqualTo("Parse error");
}
use of org.sonar.java.reporting.AnalyzerMessage in project sonar-java by SonarSource.
the class ParsingErrorCheckTest method test.
@Test
void test() {
SonarComponents sonarComponents = mock(SonarComponents.class);
when(sonarComponents.inputFileContents(any())).thenCallRealMethod();
VisitorsBridgeForTests visitorsBridge = new VisitorsBridgeForTests(new ParsingErrorCheck(), sonarComponents);
JavaAstScanner.scanSingleFileForTests(TestUtils.inputFile("src/test/files/checks/parsing/ParsingError.java"), visitorsBridge);
Set<AnalyzerMessage> issues = visitorsBridge.lastCreatedTestContext().getIssues();
assertThat(issues).hasSize(1);
AnalyzerMessage issue = issues.iterator().next();
assertThat(issue.getLine()).isEqualTo(1);
assertThat(issue.getMessage()).isEqualTo("Parse error");
}
Aggregations