Search in sources :

Example 1 with AnalyzerMessage

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;
}
Also used : SonarComponents(org.sonar.java.SonarComponents) InputComponent(org.sonar.api.batch.fs.InputComponent) JavaCheck(org.sonar.plugins.java.api.JavaCheck) AnalyzerMessage(org.sonar.java.reporting.AnalyzerMessage) InputFile(org.sonar.api.batch.fs.InputFile)

Example 2 with AnalyzerMessage

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);
}
Also used : DefaultJavaFileScannerContext(org.sonar.java.model.DefaultJavaFileScannerContext) ArrayList(java.util.ArrayList) SyntaxTrivia(org.sonar.plugins.java.api.tree.SyntaxTrivia) AnalyzerMessage(org.sonar.java.reporting.AnalyzerMessage)

Example 3 with AnalyzerMessage

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);
        }
    }
}
Also used : InputFile(org.sonar.api.batch.fs.InputFile) AnalysisException(org.sonar.java.AnalysisException) JavaWriteCache(org.sonar.plugins.java.api.caching.JavaWriteCache) Arrays(java.util.Arrays) ANY(org.sonar.plugins.java.api.semantic.MethodMatchers.ANY) EndOfAnalysis(org.sonar.plugins.java.api.internal.EndOfAnalysis) RuleProperty(org.sonar.check.RuleProperty) DefaultJavaFileScannerContext(org.sonar.java.model.DefaultJavaFileScannerContext) ExpressionUtils(org.sonar.java.model.ExpressionUtils) ModuleScannerContext(org.sonar.plugins.java.api.ModuleScannerContext) ArrayList(java.util.ArrayList) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) HashSet(java.util.HashSet) Loggers(org.sonar.api.utils.log.Loggers) Matcher(java.util.regex.Matcher) Locale(java.util.Locale) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) IssuableSubscriptionVisitor(org.sonar.plugins.java.api.IssuableSubscriptionVisitor) MethodMatchers(org.sonar.plugins.java.api.semantic.MethodMatchers) Logger(org.sonar.api.utils.log.Logger) InputFileScannerContext(org.sonar.plugins.java.api.InputFileScannerContext) Set(java.util.Set) AnalyzerMessage(org.sonar.java.reporting.AnalyzerMessage) Tree(org.sonar.plugins.java.api.tree.Tree) JavaFileScannerContext(org.sonar.plugins.java.api.JavaFileScannerContext) CachedResult.toBytes(org.sonar.java.checks.security.ExcessiveContentRequestCheck.CachedResult.toBytes) CacheContext(org.sonar.plugins.java.api.caching.CacheContext) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) DefaultModuleScannerContext(org.sonar.java.model.DefaultModuleScannerContext) JavaReadCache(org.sonar.plugins.java.api.caching.JavaReadCache) List(java.util.List) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) Rule(org.sonar.check.Rule) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) DefaultJavaFileScannerContext(org.sonar.java.model.DefaultJavaFileScannerContext) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) AnalyzerMessage(org.sonar.java.reporting.AnalyzerMessage)

Example 4 with AnalyzerMessage

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");
}
Also used : SonarComponents(org.sonar.java.SonarComponents) AnalyzerMessage(org.sonar.java.reporting.AnalyzerMessage) VisitorsBridgeForTests(org.sonar.java.testing.VisitorsBridgeForTests) Test(org.junit.jupiter.api.Test)

Example 5 with AnalyzerMessage

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");
}
Also used : SonarComponents(org.sonar.java.SonarComponents) AnalyzerMessage(org.sonar.java.reporting.AnalyzerMessage) VisitorsBridgeForTests(org.sonar.java.testing.VisitorsBridgeForTests) Test(org.junit.jupiter.api.Test)

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