Search in sources :

Example 31 with ClientInputFile

use of org.sonarsource.sonarlint.core.client.api.common.analysis.ClientInputFile in project sonar-java by SonarSource.

the class SonarLintTest method supportJavaSuppressWarning.

@Test
public void supportJavaSuppressWarning() throws Exception {
    ClientInputFile inputFile = prepareInputFile("Foo.java", "public class Foo {\n" + "  @SuppressWarnings(\"squid:S106\")\n" + "  public void foo() {\n" + "    int x;\n" + "    System.out.println(\"Foo\");\n" + "    System.out.println(\"Foo\"); //NOSONAR\n" + "  }\n" + "}", false);
    final List<Issue> issues = new ArrayList<>();
    sonarlintEngine.analyze(new StandaloneAnalysisConfiguration(baseDir.toPath(), temp.newFolder().toPath(), Collections.singletonList(inputFile), ImmutableMap.<String, String>of()), issues::add, null, null);
    assertThat(issues).extracting("ruleKey", "startLine", "inputFile.path", "severity").containsOnly(tuple("squid:S1220", null, inputFile.getPath(), "MINOR"), tuple("squid:S1481", 4, inputFile.getPath(), "MINOR"));
}
Also used : Issue(org.sonarsource.sonarlint.core.client.api.common.analysis.Issue) ArrayList(java.util.ArrayList) StandaloneAnalysisConfiguration(org.sonarsource.sonarlint.core.client.api.standalone.StandaloneAnalysisConfiguration) ClientInputFile(org.sonarsource.sonarlint.core.client.api.common.analysis.ClientInputFile) Test(org.junit.Test)

Example 32 with ClientInputFile

use of org.sonarsource.sonarlint.core.client.api.common.analysis.ClientInputFile in project sonarlint-intellij by SonarSource.

the class SonarLintAnalyzer method getInputFiles.

private List<ClientInputFile> getInputFiles(Module module, VirtualFileTestPredicate testPredicate, Collection<VirtualFile> filesToAnalyze) {
    List<ClientInputFile> inputFiles = new LinkedList<>();
    AccessToken token = app.acquireReadActionLock();
    try {
        for (VirtualFile f : filesToAnalyze) {
            boolean test = testPredicate.test(f);
            Charset charset = getEncoding(f);
            String relativePath = SonarLintUtils.getPortableRelativePath(module.getProject(), f);
            if (fileDocumentManager.isFileModified(f)) {
                inputFiles.add(new DefaultClientInputFile(f, relativePath, test, charset, fileDocumentManager.getDocument(f)));
            } else {
                inputFiles.add(new DefaultClientInputFile(f, relativePath, test, charset));
            }
        }
    } finally {
        token.finish();
    }
    return inputFiles;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) AccessToken(com.intellij.openapi.application.AccessToken) Charset(java.nio.charset.Charset) ClientInputFile(org.sonarsource.sonarlint.core.client.api.common.analysis.ClientInputFile) LinkedList(java.util.LinkedList)

Example 33 with ClientInputFile

use of org.sonarsource.sonarlint.core.client.api.common.analysis.ClientInputFile in project sonarlint-intellij by SonarSource.

the class SonarLintAnalyzer method analyzeModule.

public AnalysisResults analyzeModule(Module module, Collection<VirtualFile> filesToAnalyze, IssueListener listener, ProgressMonitor progressMonitor) {
    // Configure plugin properties. Nothing might be done if there is no configurator available for the extensions loaded in runtime.
    Map<String, String> pluginProps = new HashMap<>();
    AnalysisConfigurator[] analysisConfigurators = AnalysisConfigurator.EP_NAME.getExtensions();
    if (analysisConfigurators.length > 0) {
        for (AnalysisConfigurator config : analysisConfigurators) {
            console.debug("Configuring analysis with " + config.getClass().getName());
            pluginProps.putAll(config.configure(module));
        }
    } else {
        console.info("No analysis configurator found");
    }
    // configure files
    VirtualFileTestPredicate testPredicate = SonarLintUtils.get(module, VirtualFileTestPredicate.class);
    List<ClientInputFile> inputFiles = getInputFiles(module, testPredicate, filesToAnalyze);
    // Analyze
    long start = System.currentTimeMillis();
    try {
        SonarLintFacade facade = projectBindingManager.getFacade(true);
        String what;
        if (filesToAnalyze.size() == 1) {
            what = "'" + filesToAnalyze.iterator().next().getName() + "'";
        } else {
            what = Integer.toString(filesToAnalyze.size()) + " files";
        }
        console.info("Analysing " + what + "...");
        if (facade.requiresSavingFiles()) {
            console.debug("Saving files");
            LOG.assertTrue(!ApplicationManager.getApplication().isReadAccessAllowed(), "Should not be in a read action (risk of dead lock)");
            ApplicationManager.getApplication().invokeAndWait(() -> SonarLintUtils.saveFiles(filesToAnalyze), ModalityState.defaultModalityState());
        }
        AnalysisResults result = facade.startAnalysis(inputFiles, listener, pluginProps, progressMonitor);
        console.debug("Done in " + (System.currentTimeMillis() - start) + "ms\n");
        if (filesToAnalyze.size() == 1) {
            telemetry.analysisDoneOnSingleFile(filesToAnalyze.iterator().next().getExtension(), (int) (System.currentTimeMillis() - start));
        } else {
            telemetry.analysisDoneOnMultipleFiles();
        }
        return result;
    } catch (InvalidBindingException e) {
        // should not happen, as analysis should not have been submitted in this case.
        throw new IllegalStateException(e);
    }
}
Also used : InvalidBindingException(org.sonarlint.intellij.exception.InvalidBindingException) HashMap(java.util.HashMap) SonarLintFacade(org.sonarlint.intellij.core.SonarLintFacade) AnalysisResults(org.sonarsource.sonarlint.core.client.api.common.analysis.AnalysisResults) ClientInputFile(org.sonarsource.sonarlint.core.client.api.common.analysis.ClientInputFile)

Example 34 with ClientInputFile

use of org.sonarsource.sonarlint.core.client.api.common.analysis.ClientInputFile in project sonarlint-intellij by SonarSource.

the class StandaloneTest method simpleJava.

@Test
public void simpleJava() throws Exception {
    ClientInputFile inputFile = prepareInputFile("Foo.java", "public class Foo {\n" + "  public void foo() {\n" + "    int x;\n" + "    System.out.println(\"Foo\");\n" + "    System.out.println(\"Foo\"); //NOSONAR\n" + "  }\n" + "}");
    List<Issue> issues = analyze(inputFile);
    assertThat(issues).extracting("ruleKey", "startLine", "inputFile.path", "severity").containsOnly(tuple("squid:S106", 4, inputFile.getPath(), "MAJOR"), tuple("squid:S1220", null, inputFile.getPath(), "MINOR"), tuple("squid:S1481", 3, inputFile.getPath(), "MINOR"));
}
Also used : Issue(org.sonarsource.sonarlint.core.client.api.common.analysis.Issue) ClientInputFile(org.sonarsource.sonarlint.core.client.api.common.analysis.ClientInputFile) Test(org.junit.Test)

Example 35 with ClientInputFile

use of org.sonarsource.sonarlint.core.client.api.common.analysis.ClientInputFile in project sonarlint-intellij by SonarSource.

the class IssueProcessor method removeFailedFiles.

private Map<VirtualFile, Collection<LiveIssue>> removeFailedFiles(Collection<VirtualFile> analyzed, Collection<ClientInputFile> failedAnalysisFiles) {
    Set<VirtualFile> failedVirtualFiles = failedAnalysisFiles.stream().map(f -> (VirtualFile) f.getClientObject()).collect(Collectors.toSet());
    Map<VirtualFile, Collection<LiveIssue>> map = new HashMap<>();
    for (VirtualFile f : analyzed) {
        if (failedVirtualFiles.contains(f)) {
            console.info("File won't be refreshed because there were errors during analysis: " + f.getPath());
        } else {
            // it's important to add all files, even without issues, to correctly track the leak period (SLI-86)
            map.put(f, new ArrayList<>());
        }
    }
    return map;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) VirtualFile(com.intellij.openapi.vfs.VirtualFile) HashMap(java.util.HashMap) ClientInputFile(org.sonarsource.sonarlint.core.client.api.common.analysis.ClientInputFile) IssueLocation(org.sonarsource.sonarlint.core.client.api.common.analysis.IssueLocation) ReadAction(com.intellij.openapi.application.ReadAction) ArrayList(java.util.ArrayList) AccessToken(com.intellij.openapi.application.AccessToken) Map(java.util.Map) Project(com.intellij.openapi.project.Project) PsiFile(com.intellij.psi.PsiFile) Logger(com.intellij.openapi.diagnostic.Logger) LinkedList(java.util.LinkedList) RangeMarker(com.intellij.openapi.editor.RangeMarker) AnalysisCallback(org.sonarlint.intellij.analysis.AnalysisCallback) Collection(java.util.Collection) Set(java.util.Set) Collectors(java.util.stream.Collectors) AbstractProjectComponent(com.intellij.openapi.components.AbstractProjectComponent) SonarLintConsole(org.sonarlint.intellij.ui.SonarLintConsole) ProgressIndicator(com.intellij.openapi.progress.ProgressIndicator) List(java.util.List) TriggerType(org.sonarlint.intellij.trigger.TriggerType) Issue(org.sonarsource.sonarlint.core.client.api.common.analysis.Issue) SonarLintJob(org.sonarlint.intellij.analysis.SonarLintJob) Collections(java.util.Collections) ServerIssueUpdater(org.sonarlint.intellij.core.ServerIssueUpdater) HashMap(java.util.HashMap) Collection(java.util.Collection)

Aggregations

ClientInputFile (org.sonarsource.sonarlint.core.client.api.common.analysis.ClientInputFile)72 Test (org.junit.Test)45 Issue (org.sonarsource.sonarlint.core.client.api.common.analysis.Issue)36 ArrayList (java.util.ArrayList)30 StandaloneAnalysisConfiguration (org.sonarsource.sonarlint.core.client.api.standalone.StandaloneAnalysisConfiguration)25 TestClientInputFile (org.sonarsource.sonarlint.core.TestClientInputFile)19 File (java.io.File)14 ConnectedAnalysisConfiguration (org.sonarsource.sonarlint.core.client.api.connected.ConnectedAnalysisConfiguration)9 Path (java.nio.file.Path)8 HashMap (java.util.HashMap)7 AnalysisResults (org.sonarsource.sonarlint.core.client.api.common.analysis.AnalysisResults)7 LinkedList (java.util.LinkedList)6 VirtualFile (com.intellij.openapi.vfs.VirtualFile)5 Collection (java.util.Collection)4 AccessToken (com.intellij.openapi.application.AccessToken)3 Logger (com.intellij.openapi.diagnostic.Logger)3 RangeMarker (com.intellij.openapi.editor.RangeMarker)3 ProgressIndicator (com.intellij.openapi.progress.ProgressIndicator)3 Project (com.intellij.openapi.project.Project)3 PsiFile (com.intellij.psi.PsiFile)3