Skip to content

Commit

Permalink
SLLS-184 Fix issues after migration to Java 17
Browse files Browse the repository at this point in the history
  • Loading branch information
jblievremont authored and sophio-japharidze-sonarsource committed Oct 31, 2023
1 parent 0e26cb6 commit 0b2ad28
Show file tree
Hide file tree
Showing 17 changed files with 84 additions and 125 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,31 +97,19 @@ private static Language toSqLanguage(@Nullable String clientLanguageId) {
return null;
}
// See https://microsoft.github.io/language-server-protocol/specification#textDocumentItem
switch (clientLanguageId) {
case "javascript":
case "javascriptreact":
case "vue":
case "vue component":
case "babel es6 javascript":
return Language.JS;
case "python":
return Language.PYTHON;
case "typescript":
case "typescriptreact":
return Language.TS;
case "html":
return Language.HTML;
case "oraclesql":
return Language.PLSQL;
case "apex":
case "apex-anon":
return switch (clientLanguageId) {
case "javascript", "javascriptreact", "vue", "vue component", "babel es6 javascript" -> Language.JS;
case "python" -> Language.PYTHON;
case "typescript", "typescriptreact" -> Language.TS;
case "html" -> Language.HTML;
case "oraclesql" -> Language.PLSQL;
case "apex", "apex-anon" ->
// See https://github.com/forcedotcom/salesforcedx-vscode/blob/5e4b7715d1cb3d1ee2780780ed63f70f58e93b20/packages/salesforcedx-vscode-apex/package.json#L273
return Language.APEX;
case "yaml":
return Language.YAML;
default:
Language.APEX;
case "yaml" -> Language.YAML;
default ->
// Other supported languages map to the same key as the one used in SonarQube/SonarCloud
return Language.forKey(clientLanguageId).orElse(null);
}
Language.forKey(clientLanguageId).orElse(null);
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicLong;
import java.util.stream.Collectors;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import org.sonarsource.sonarlint.ls.connected.ProjectBindingManager;
Expand All @@ -49,7 +48,6 @@
import org.sonarsource.sonarlint.ls.util.Utils;

import static java.lang.String.format;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toSet;

/**
Expand Down Expand Up @@ -256,18 +254,18 @@ public void shutdown() {
public void analyzeAllOpenFilesInFolder(@Nullable WorkspaceFolderWrapper folder) {
var openedFileUrisInFolder = openFilesCache.getAll().stream()
.filter(f -> belongToFolder(folder, f.getUri()))
.collect(Collectors.toList());
.toList();
analyseNotIgnoredFiles(openedFileUrisInFolder);
}

private void analyseNotIgnoredFiles(List<VersionedOpenFile> files) {
var uriStrings = files.stream().map(it -> it.getUri().toString()).collect(toList());
var uriStrings = files.stream().map(it -> it.getUri().toString()).toList();
var fileUrisParams = new SonarLintExtendedLanguageClient.FileUrisParams(uriStrings);
client.filterOutExcludedFiles(fileUrisParams)
.thenAccept(notIgnoredFileUris -> {
var notIgnoredFiles = files
.stream().filter(it -> notIgnoredFileUris.getFileUris().contains(it.getUri().toString()))
.collect(toList());
.toList();
analyzeAsync(AnalysisParams.newAnalysisParams(notIgnoredFiles));
});
}
Expand Down Expand Up @@ -306,7 +304,7 @@ public void analyzeAllOpenCOrCppFilesInFolder(@Nullable WorkspaceFolderWrapper f
var openedCorCppFileUrisInFolder = openFilesCache.getAll().stream()
.filter(VersionedOpenFile::isCOrCpp)
.filter(f -> belongToFolder(folder, f.getUri()))
.collect(Collectors.toList());
.toList();
analyseNotIgnoredFiles(openedCorCppFileUrisInFolder);
}

Expand All @@ -317,21 +315,21 @@ public void scanForHotspotsInFiles(List<VersionedOpenFile> files) {
private void analyzeAllUnboundOpenFiles() {
var openedUnboundFileUris = openFilesCache.getAll().stream()
.filter(f -> bindingManager.getBinding(f.getUri()).isEmpty())
.collect(Collectors.toList());
.toList();
analyseNotIgnoredFiles(openedUnboundFileUris);
}

private void analyzeAllOpenNotebooks() {
var openNotebookUris = openNotebooksCache.getAll().stream()
.map(VersionedOpenNotebook::asVersionedOpenFile)
.collect(Collectors.toList());
.toList();
analyseNotIgnoredFiles(openNotebookUris);
}

private void analyzeAllOpenJavaFiles() {
var openedJavaFileUris = openFilesCache.getAll().stream()
.filter(VersionedOpenFile::isJava)
.collect(toList());
.toList();
analyseNotIgnoredFiles(openedJavaFileUris);
}

Expand Down
18 changes: 9 additions & 9 deletions src/main/java/org/sonarsource/sonarlint/ls/CommandManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,16 +169,16 @@ private void computeCodeActionsForSonarLintIssues(Diagnostic diagnostic, List<Ei
var hasBinding = binding.isPresent();
if (issueForDiagnostic.isPresent()) {
var versionedIssue = issueForDiagnostic.get();
ruleContextKey = versionedIssue.getIssue().getRuleDescriptionContextKey().orElse("");
ruleContextKey = versionedIssue.issue().getRuleDescriptionContextKey().orElse("");
var quickFixes = isNotebookCellUri && versionedOpenNotebook.isPresent() ?
versionedOpenNotebook.get().toCellIssue(versionedIssue.getIssue()).quickFixes() :
versionedIssue.getIssue().quickFixes();
versionedOpenNotebook.get().toCellIssue(versionedIssue.issue()).quickFixes() :
versionedIssue.issue().quickFixes();
cancelToken.checkCanceled();
quickFixes.forEach(fix -> {
var newCodeAction = new CodeAction(SONARLINT_ACTION_PREFIX + fix.message());
newCodeAction.setKind(CodeActionKind.QuickFix);
newCodeAction.setDiagnostics(List.of(diagnostic));
newCodeAction.setEdit(newWorkspaceEdit(fix, versionedIssue.getDocumentVersion()));
newCodeAction.setEdit(newWorkspaceEdit(fix, versionedIssue.documentVersion()));
newCodeAction.setCommand(new Command(fix.message(), SONARLINT_QUICK_FIX_APPLIED, List.of(ruleKey)));
codeActions.add(Either.forRight(newCodeAction));
});
Expand All @@ -199,8 +199,8 @@ private void computeCodeActionsForSonarLintIssues(Diagnostic diagnostic, List<Ei

private Optional<CodeAction> createResolveIssueCodeAction(Diagnostic diagnostic, URI uri, ProjectBindingWrapper binding, String ruleKey,
IssuesCache.VersionedIssue versionedIssue) {
var isDelegatingIssue = versionedIssue.getIssue() instanceof DelegatingIssue;
var delegatingIssue = isDelegatingIssue ? ((DelegatingIssue) versionedIssue.getIssue()) : null;
var isDelegatingIssue = versionedIssue.issue() instanceof DelegatingIssue;
var delegatingIssue = isDelegatingIssue ? ((DelegatingIssue) versionedIssue.issue()) : null;
if (delegatingIssue != null && delegatingIssue.getIssueId() != null) {
var issueId = delegatingIssue.getIssueId();
var serverIssueKey = delegatingIssue.getServerIssueKey();
Expand Down Expand Up @@ -229,9 +229,9 @@ private CodeAction createResolveIssueCodeAction(Diagnostic diagnostic, String ru

private static void addShowAllLocationsCodeAction(IssuesCache.VersionedIssue versionedIssue,
List<Either<Command, CodeAction>> codeActions, Diagnostic diagnostic, String ruleKey, boolean isNotebook) {
if (!versionedIssue.getIssue().flows().isEmpty() && !isNotebook) {
if (!versionedIssue.issue().flows().isEmpty() && !isNotebook) {
var titleShowAllLocations = String.format("Show all locations for issue '%s'", ruleKey);
codeActions.add(newQuickFix(diagnostic, titleShowAllLocations, ShowAllLocationsCommand.ID, List.of(ShowAllLocationsCommand.params(versionedIssue.getIssue()))));
codeActions.add(newQuickFix(diagnostic, titleShowAllLocations, ShowAllLocationsCommand.ID, List.of(ShowAllLocationsCommand.params(versionedIssue.issue()))));
}
}

Expand Down Expand Up @@ -460,7 +460,7 @@ private void handleShowHotspotFlows(ExecuteCommandParams params) {
SonarLintLogger.get().error("Hotspot is not found during showing flows");
return;
}
var hotspot = issue.getIssue();
var hotspot = issue.issue();
client.showIssueOrHotspot(ShowAllLocationsCommand.params(hotspot));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import org.sonarsource.sonarlint.ls.notebooks.OpenNotebooksCache;
import org.sonarsource.sonarlint.ls.util.Utils;

import static java.util.stream.Collectors.toList;
import static org.sonarsource.sonarlint.ls.util.Utils.buildMessageWithPluralizedSuffix;

public class DiagnosticPublisher {
Expand Down Expand Up @@ -83,7 +82,7 @@ public void publishDiagnostics(URI f, boolean onlyHotspots) {
}

Diagnostic convert(Map.Entry<String, VersionedIssue> entry) {
var issue = entry.getValue().getIssue();
var issue = entry.getValue().issue();
return prepareDiagnostic(issue, entry.getKey(), false, focusOnNewCode);
}

Expand Down Expand Up @@ -111,8 +110,8 @@ public static Diagnostic prepareDiagnostic(Issue issue, String entryKey, boolean
}

static void setSeverity(Diagnostic diagnostic, Issue issue, boolean focusOnNewCode) {
if (focusOnNewCode && issue instanceof DelegatingIssue) {
var newCodeSeverity = ((DelegatingIssue) issue).isOnNewCode() ? DiagnosticSeverity.Warning : DiagnosticSeverity.Hint;
if (focusOnNewCode && issue instanceof DelegatingIssue delegatingIssue) {
var newCodeSeverity = delegatingIssue.isOnNewCode() ? DiagnosticSeverity.Warning : DiagnosticSeverity.Hint;
diagnostic.setSeverity(newCodeSeverity);
} else {
diagnostic.setSeverity(DiagnosticSeverity.Warning);
Expand Down Expand Up @@ -145,8 +144,7 @@ public String getEntryKey() {

}
public static void setSource(Diagnostic diagnostic, Issue issue) {
if (issue instanceof DelegatingIssue) {
var delegatedIssue = (DelegatingIssue) issue;
if (issue instanceof DelegatingIssue delegatedIssue) {
var isKnown = delegatedIssue.getServerIssueKey() != null;
var isHotspot = delegatedIssue.getType() == RuleType.SECURITY_HOTSPOT;
diagnostic.setSource(isKnown && isHotspot ? REMOTE_SOURCE : SONARLINT_SOURCE);
Expand All @@ -157,8 +155,7 @@ public static void setSource(Diagnostic diagnostic, Issue issue) {

private static void setData(Diagnostic diagnostic, Issue issue, String entryKey) {
var data = new DiagnosticData();
if (issue instanceof DelegatingIssue) {
var delegatedIssue = (DelegatingIssue) issue;
if (issue instanceof DelegatingIssue delegatedIssue) {
data.setStatus(delegatedIssue.getReviewStatus());
data.setServerIssueKey(delegatedIssue.getServerIssueKey());
}
Expand All @@ -185,7 +182,7 @@ private PublishDiagnosticsParams createPublishDiagnosticsParams(URI newUri) {

Map<String, VersionedIssue> localIssues = issuesCache.get(newUri);

if (!firstSecretIssueDetected && localIssues.values().stream().anyMatch(v -> v.getIssue().getRuleKey().startsWith(Language.SECRETS.getLanguageKey()))) {
if (!firstSecretIssueDetected && localIssues.values().stream().anyMatch(v -> v.issue().getRuleKey().startsWith(Language.SECRETS.getLanguageKey()))) {
client.showFirstSecretDetectionNotification();
firstSecretIssueDetected = true;
}
Expand All @@ -197,7 +194,7 @@ private PublishDiagnosticsParams createPublishDiagnosticsParams(URI newUri) {

var diagnosticList = Stream.concat(localDiagnostics, taintDiagnostics)
.sorted(DiagnosticPublisher.byLineNumber())
.collect(toList());
.toList();
p.setDiagnostics(diagnosticList);
p.setUri(newUri.toString());

Expand All @@ -211,7 +208,7 @@ private PublishDiagnosticsParams createPublishSecurityHotspotsParams(URI newUri)
.stream()
.map(this::convert)
.sorted(DiagnosticPublisher.byLineNumber())
.collect(toList()));
.toList());
p.setUri(newUri.toString());

return p;
Expand Down
28 changes: 6 additions & 22 deletions src/main/java/org/sonarsource/sonarlint/ls/IssuesCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ public void reportIssue(VersionedOpenFile versionedOpenFile, Issue issue) {

private static String getIssueId(Issue issue) {
String issueId = null;
if (issue instanceof DelegatingIssue) {
var issueUuid = ((DelegatingIssue) issue).getIssueId();
if (issue instanceof DelegatingIssue delegatingIssue) {
var issueUuid = delegatingIssue.getIssueId();
issueId = issueUuid != null ? issueUuid.toString() : null;
}
if (issueId == null) {
Expand Down Expand Up @@ -120,8 +120,8 @@ public void removeIssueWithServerKey(String fileUriStr, String key) {
}

private static boolean isLocalIssueWithKey(String key, VersionedIssue versionedIssue) {
return versionedIssue.getIssue() instanceof DelegatingIssue
&& (key.equals(((DelegatingIssue) versionedIssue.getIssue()).getIssueId().toString()));
return versionedIssue.issue() instanceof DelegatingIssue delegatingIssue
&& (key.equals(delegatingIssue.getIssueId().toString()));
}

public Optional<Map.Entry<String, VersionedIssue>> findIssuePerId(String fileUriStr, String serverIssueKey) {
Expand All @@ -140,7 +140,7 @@ public void updateIssueStatus(String fileUriStr, String serverIssueKey, HotspotS
var issuePerId = findIssuePerId(fileUriStr, serverIssueKey);
if (issuePerId.isPresent()) {
var versionedIssue = issuePerId.get().getValue();
var delegatingIssue = (DelegatingIssue) versionedIssue.getIssue();
var delegatingIssue = (DelegatingIssue) versionedIssue.issue();
var clonedDelegatingIssue = delegatingIssue.cloneWithNewStatus(hotspotReviewStatusValueOfHotspotStatus(newStatus));
var clonedVersionedIssue = new VersionedIssue(clonedDelegatingIssue, versionedIssue.documentVersion);
var issuesByKey = issuesPerIdPerFileURI.get(URI.create(fileUriStr));
Expand All @@ -159,23 +159,7 @@ public Optional<VersionedIssue> getIssueForDiagnostic(URI fileUri, Diagnostic d)
.filter(Objects::nonNull);
}

public static class VersionedIssue {
private final Issue issue;
private final int documentVersion;

public VersionedIssue(Issue issue, int documentVersion) {
this.issue = issue;
this.documentVersion = documentVersion;
}

public Issue getIssue() {
return issue;
}

public int getDocumentVersion() {
return documentVersion;
}
}
public record VersionedIssue(Issue issue, int documentVersion) {}

public Map<String, VersionedIssue> get(URI fileUri) {
return inProgressAnalysisIssuesPerIdPerFileURI.getOrDefault(fileUri, issuesPerIdPerFileURI.getOrDefault(fileUri, Map.of()));
Expand Down
Loading

0 comments on commit 0b2ad28

Please sign in to comment.