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"));
}
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;
}
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);
}
}
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"));
}
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;
}
Aggregations