From 0b2ad2801538a660acda5a07660cdf296aa364d8 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Lievremont Date: Tue, 31 Oct 2023 09:54:06 +0100 Subject: [PATCH] SLLS-184 Fix issues after migration to Java 17 --- .../sonarlint/ls/AnalysisClientInputFile.java | 36 +++++++------------ .../sonarlint/ls/AnalysisScheduler.java | 16 ++++----- .../sonarlint/ls/CommandManager.java | 18 +++++----- .../sonarlint/ls/DiagnosticPublisher.java | 19 +++++----- .../sonarsource/sonarlint/ls/IssuesCache.java | 28 ++++----------- .../sonarlint/ls/SonarLintLanguageServer.java | 34 ++++++++---------- .../sonarlint/ls/backend/BackendService.java | 5 ++- .../ls/commands/ShowAllLocationsCommand.java | 13 ++++--- .../connected/ServerIssueTrackerWrapper.java | 12 +++---- .../ls/connected/domain/TaintIssue.java | 3 +- .../events/ServerSentEventsHandler.java | 4 +-- .../ls/folders/WorkspaceFoldersManager.java | 3 +- .../NotebookDiagnosticPublisher.java | 6 ++-- .../ls/notebooks/VersionedOpenNotebook.java | 4 +-- .../ls/settings/RulesConfiguration.java | 2 +- .../ls/settings/SettingsManager.java | 2 +- .../sonarsource/sonarlint/ls/util/Utils.java | 4 +-- 17 files changed, 84 insertions(+), 125 deletions(-) diff --git a/src/main/java/org/sonarsource/sonarlint/ls/AnalysisClientInputFile.java b/src/main/java/org/sonarsource/sonarlint/ls/AnalysisClientInputFile.java index a00850337..877c5e162 100644 --- a/src/main/java/org/sonarsource/sonarlint/ls/AnalysisClientInputFile.java +++ b/src/main/java/org/sonarsource/sonarlint/ls/AnalysisClientInputFile.java @@ -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); + }; } } diff --git a/src/main/java/org/sonarsource/sonarlint/ls/AnalysisScheduler.java b/src/main/java/org/sonarsource/sonarlint/ls/AnalysisScheduler.java index 8d7644b12..daef2f10c 100644 --- a/src/main/java/org/sonarsource/sonarlint/ls/AnalysisScheduler.java +++ b/src/main/java/org/sonarsource/sonarlint/ls/AnalysisScheduler.java @@ -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; @@ -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; /** @@ -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 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)); }); } @@ -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); } @@ -317,21 +315,21 @@ public void scanForHotspotsInFiles(List 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); } diff --git a/src/main/java/org/sonarsource/sonarlint/ls/CommandManager.java b/src/main/java/org/sonarsource/sonarlint/ls/CommandManager.java index 88439887d..69faf94e2 100644 --- a/src/main/java/org/sonarsource/sonarlint/ls/CommandManager.java +++ b/src/main/java/org/sonarsource/sonarlint/ls/CommandManager.java @@ -169,16 +169,16 @@ private void computeCodeActionsForSonarLintIssues(Diagnostic diagnostic, List { 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)); }); @@ -199,8 +199,8 @@ private void computeCodeActionsForSonarLintIssues(Diagnostic diagnostic, List 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(); @@ -229,9 +229,9 @@ private CodeAction createResolveIssueCodeAction(Diagnostic diagnostic, String ru private static void addShowAllLocationsCodeAction(IssuesCache.VersionedIssue versionedIssue, List> 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())))); } } @@ -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)); } diff --git a/src/main/java/org/sonarsource/sonarlint/ls/DiagnosticPublisher.java b/src/main/java/org/sonarsource/sonarlint/ls/DiagnosticPublisher.java index b878032ff..3e0eb3660 100644 --- a/src/main/java/org/sonarsource/sonarlint/ls/DiagnosticPublisher.java +++ b/src/main/java/org/sonarsource/sonarlint/ls/DiagnosticPublisher.java @@ -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 { @@ -83,7 +82,7 @@ public void publishDiagnostics(URI f, boolean onlyHotspots) { } Diagnostic convert(Map.Entry entry) { - var issue = entry.getValue().getIssue(); + var issue = entry.getValue().issue(); return prepareDiagnostic(issue, entry.getKey(), false, focusOnNewCode); } @@ -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); @@ -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); @@ -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()); } @@ -185,7 +182,7 @@ private PublishDiagnosticsParams createPublishDiagnosticsParams(URI newUri) { Map 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; } @@ -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()); @@ -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; diff --git a/src/main/java/org/sonarsource/sonarlint/ls/IssuesCache.java b/src/main/java/org/sonarsource/sonarlint/ls/IssuesCache.java index 026a9ccab..b64100118 100644 --- a/src/main/java/org/sonarsource/sonarlint/ls/IssuesCache.java +++ b/src/main/java/org/sonarsource/sonarlint/ls/IssuesCache.java @@ -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) { @@ -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> findIssuePerId(String fileUriStr, String serverIssueKey) { @@ -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)); @@ -159,23 +159,7 @@ public Optional 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 get(URI fileUri) { return inProgressAnalysisIssuesPerIdPerFileURI.getOrDefault(fileUri, issuesPerIdPerFileURI.getOrDefault(fileUri, Map.of())); diff --git a/src/main/java/org/sonarsource/sonarlint/ls/SonarLintLanguageServer.java b/src/main/java/org/sonarsource/sonarlint/ls/SonarLintLanguageServer.java index 104e4d6e0..b31898e6e 100644 --- a/src/main/java/org/sonarsource/sonarlint/ls/SonarLintLanguageServer.java +++ b/src/main/java/org/sonarsource/sonarlint/ls/SonarLintLanguageServer.java @@ -74,6 +74,7 @@ import org.eclipse.lsp4j.SetTraceParams; import org.eclipse.lsp4j.TextDocumentSyncKind; import org.eclipse.lsp4j.TextDocumentSyncOptions; +import org.eclipse.lsp4j.WindowClientCapabilities; import org.eclipse.lsp4j.WorkDoneProgressCancelParams; import org.eclipse.lsp4j.WorkspaceFoldersOptions; import org.eclipse.lsp4j.WorkspaceServerCapabilities; @@ -160,12 +161,10 @@ public class SonarLintLanguageServer implements SonarLintExtendedLanguageServer, private final WorkspaceFoldersManager workspaceFoldersManager; private final SettingsManager settingsManager; private final ProjectBindingManager bindingManager; - private final SmartNotifications smartNotifications; private final AnalysisScheduler analysisScheduler; private final TaintVulnerabilitiesCache taintVulnerabilitiesCache; private final OpenFilesCache openFilesCache; private final OpenNotebooksCache openNotebooksCache; - private final NodeJsRuntime nodeJsRuntime; private final EnginesFactory enginesFactory; private final StandaloneEngineManager standaloneEngineManager; private final CommandManager commandManager; @@ -178,7 +177,7 @@ public class SonarLintLanguageServer implements SonarLintExtendedLanguageServer, private final IssuesCache securityHotspotsCache; private final DiagnosticPublisher diagnosticPublisher; private final ScmIgnoredCache scmIgnoredCache; - private ServerSynchronizer serverSynchronizer; + private final ServerSynchronizer serverSynchronizer; private final LanguageClientLogger lsLogOutput; private final TaintIssuesUpdater taintIssuesUpdater; @@ -192,10 +191,7 @@ public class SonarLintLanguageServer implements SonarLintExtendedLanguageServer, private TraceValue traceLevel; private final ModuleEventsProcessor moduleEventsProcessor; - private final ServerSentEventsHandlerService serverSentEventsHandler; - private final TaintVulnerabilityRaisedNotification taintVulnerabilityRaisedNotification; private final BackendServiceFacade backendServiceFacade; - private final CleanAsYouCodeManager cleanAsYouCodeManager; private final Collection analyzers; private final CountDownLatch shutdownLatch; @@ -233,7 +229,7 @@ public class SonarLintLanguageServer implements SonarLintExtendedLanguageServer, this.settingsManager = new SettingsManager(this.client, this.workspaceFoldersManager, backendServiceFacade); vsCodeClient.setSettingsManager(settingsManager); backendServiceFacade.setSettingsManager(settingsManager); - this.nodeJsRuntime = new NodeJsRuntime(settingsManager); + var nodeJsRuntime = new NodeJsRuntime(settingsManager); var fileTypeClassifier = new FileTypeClassifier(); javaConfigCache = new JavaConfigCache(client, openFilesCache, lsLogOutput); this.enginesFactory = new EnginesFactory(analyzers, getEmbeddedPluginsToPath(), globalLogOutput, nodeJsRuntime, @@ -249,7 +245,7 @@ public class SonarLintLanguageServer implements SonarLintExtendedLanguageServer, this.settingsManager.addListener((WorkspaceSettingsChangeListener) bindingManager); this.settingsManager.addListener((WorkspaceFolderSettingsChangeListener) bindingManager); this.workspaceFoldersManager.addListener(settingsManager); - this.smartNotifications = new SmartNotifications(client, telemetry); + var smartNotifications = new SmartNotifications(client, telemetry); vsCodeClient.setSmartNotifications(smartNotifications); var skippedPluginsNotifier = new SkippedPluginsNotifier(client); this.scmIgnoredCache = new ScmIgnoredCache(client); @@ -265,8 +261,8 @@ public class SonarLintLanguageServer implements SonarLintExtendedLanguageServer, this.serverSynchronizer = new ServerSynchronizer(client, progressManager, bindingManager, analysisScheduler, backendServiceFacade); this.commandManager = new CommandManager(client, settingsManager, bindingManager, serverSynchronizer, telemetry, taintVulnerabilitiesCache, issuesCache, securityHotspotsCache, backendServiceFacade, workspaceFoldersManager, openNotebooksCache); - this.taintVulnerabilityRaisedNotification = new TaintVulnerabilityRaisedNotification(client, commandManager); - this.serverSentEventsHandler = new ServerSentEventsHandler(bindingManager, taintVulnerabilitiesCache, + var taintVulnerabilityRaisedNotification = new TaintVulnerabilityRaisedNotification(client, commandManager); + ServerSentEventsHandlerService serverSentEventsHandler = new ServerSentEventsHandler(bindingManager, taintVulnerabilitiesCache, taintVulnerabilityRaisedNotification, settingsManager, workspaceFoldersManager, analysisScheduler); vsCodeClient.setServerSentEventsHandlerService(serverSentEventsHandler); this.branchManager = new WorkspaceFolderBranchManager(client, bindingManager, backendServiceFacade); @@ -275,8 +271,8 @@ public class SonarLintLanguageServer implements SonarLintExtendedLanguageServer, this.workspaceFoldersManager.setBindingManager(bindingManager); this.taintIssuesUpdater = new TaintIssuesUpdater(bindingManager, taintVulnerabilitiesCache, workspaceFoldersManager, settingsManager, diagnosticPublisher, backendServiceFacade); - this.cleanAsYouCodeManager = new CleanAsYouCodeManager(diagnosticPublisher, openFilesCache, backendServiceFacade); - this.settingsManager.addListener(this.cleanAsYouCodeManager); + var cleanAsYouCodeManager = new CleanAsYouCodeManager(diagnosticPublisher, openFilesCache, backendServiceFacade); + this.settingsManager.addListener(cleanAsYouCodeManager); this.shutdownLatch = new CountDownLatch(1); launcher.startListening(); } @@ -298,7 +294,7 @@ public CompletableFuture initialize(InitializeParams params) { boolean workDoneSupportedByClient = ofNullable(params.getCapabilities()) .flatMap(capabilities -> ofNullable(capabilities.getWindow())) - .map(window -> window.getWorkDoneProgress()) + .map(WindowClientCapabilities::getWorkDoneProgress) .orElse(false); progressManager.setWorkDoneProgressSupportedByClient(workDoneSupportedByClient); @@ -691,7 +687,7 @@ public void openHotspotInBrowser(OpenHotspotInBrowserLsParams params) { return; } var versionedIssue = securityHotspotsCache.get(fileUri).get(hotspotId); - var delegatingIssue = (DelegatingIssue) versionedIssue.getIssue(); + var delegatingIssue = (DelegatingIssue) versionedIssue.issue(); var openHotspotInBrowserParams = new OpenHotspotInBrowserParams(workspaceFolderUri.toString(), branchNameOptional.get(), delegatingIssue.getServerIssueKey()); backendServiceFacade.getBackendService().openHotspotInBrowser(openHotspotInBrowserParams); @@ -702,7 +698,7 @@ public CompletableFuture showHotspotRuleDescription(ShowHotspotRuleDescrip var fileUri = params.fileUri; var ruleKey = params.ruleKey; var issue = securityHotspotsCache.get(create(fileUri)).get(params.getHotspotId()); - var ruleContextKey = Objects.isNull(issue) ? "" : issue.getIssue().getRuleDescriptionContextKey().orElse(""); + var ruleContextKey = Objects.isNull(issue) ? "" : issue.issue().getRuleDescriptionContextKey().orElse(""); var showHotspotCommandParams = new ExecuteCommandParams(SONARLINT_OPEN_RULE_DESCRIPTION_FROM_CODE_ACTION_COMMAND, List.of(new JsonPrimitive(ruleKey), new JsonPrimitive(fileUri), new JsonPrimitive(ruleContextKey))); return CompletableFutures.computeAsync(cancelToken -> { @@ -778,7 +774,7 @@ public CompletableFuture scanFolderForHotspots(ScanFolderForHotspotsParams private void runScan(ScanFolderForHotspotsParams params) { var filesToAnalyze = params.getDocuments().stream() .map(d -> new VersionedOpenFile(create(d.getUri()), d.getLanguageId(), d.getVersion(), d.getText())) - .collect(Collectors.toList()); + .toList(); analysisScheduler.scanForHotspotsInFiles(filesToAnalyze); } @@ -847,7 +843,7 @@ public CompletableFuture getAllowedHotspotSta var checkStatusChangePermittedParams = new CheckStatusChangePermittedParams(connectionId, params.getHotspotKey()); return backendServiceFacade.getBackendService().getAllowedHotspotStatuses(checkStatusChangePermittedParams).thenApply(r -> { var delegatingIssue = (DelegatingIssue) securityHotspotsCache - .findIssuePerId(params.getFileUri(), params.getHotspotKey()).get().getValue().getIssue(); + .findIssuePerId(params.getFileUri(), params.getHotspotKey()).get().getValue().issue(); var reviewStatus = delegatingIssue.getReviewStatus(); var statuses = r.getAllowedStatuses().stream().filter(s -> s != hotspotStatusValueOfHotspotReviewStatus(reviewStatus)) - .map(HotspotStatus::getTitle).collect(Collectors.toList()); + .map(HotspotStatus::getTitle).toList(); return new GetAllowedHotspotStatusesResponse( r.isPermitted(), r.getNotPermittedReason(), diff --git a/src/main/java/org/sonarsource/sonarlint/ls/backend/BackendService.java b/src/main/java/org/sonarsource/sonarlint/ls/backend/BackendService.java index 260cfac10..1ac4fbd3f 100644 --- a/src/main/java/org/sonarsource/sonarlint/ls/backend/BackendService.java +++ b/src/main/java/org/sonarsource/sonarlint/ls/backend/BackendService.java @@ -25,7 +25,6 @@ import java.util.concurrent.CompletableFuture; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutionException; -import java.util.stream.Collectors; import org.eclipse.lsp4j.WorkspaceFolder; import org.sonarsource.sonarlint.core.clientapi.SonarLintBackend; import org.sonarsource.sonarlint.core.clientapi.backend.analysis.GetSupportedFilePatternsParams; @@ -121,14 +120,14 @@ public static List extractSonarQubeConnecti return connections.entrySet().stream() .filter(it -> !it.getValue().isSonarCloudAlias()) .map(it -> new SonarQubeConnectionConfigurationDto(it.getKey(), it.getValue().getServerUrl(), it.getValue().isSmartNotificationsDisabled())) - .collect(Collectors.toList()); + .toList(); } public static List extractSonarCloudConnections(Map connections) { return connections.entrySet().stream() .filter(it -> it.getValue().isSonarCloudAlias()) .map(it -> new SonarCloudConnectionConfigurationDto(it.getKey(), it.getValue().getOrganizationKey(), it.getValue().isSmartNotificationsDisabled())) - .collect(Collectors.toList()); + .toList(); } public ConfigurationScopeDto getConfigScopeDto(WorkspaceFolder added, Optional bindingOptional) { diff --git a/src/main/java/org/sonarsource/sonarlint/ls/commands/ShowAllLocationsCommand.java b/src/main/java/org/sonarsource/sonarlint/ls/commands/ShowAllLocationsCommand.java index ac9151836..aeb5175fe 100644 --- a/src/main/java/org/sonarsource/sonarlint/ls/commands/ShowAllLocationsCommand.java +++ b/src/main/java/org/sonarsource/sonarlint/ls/commands/ShowAllLocationsCommand.java @@ -27,7 +27,6 @@ import java.util.Map; import java.util.Optional; import java.util.function.Function; -import java.util.stream.Collectors; import javax.annotation.CheckForNull; import javax.annotation.Nullable; import org.sonarsource.sonarlint.core.analysis.api.ClientInputFile; @@ -66,7 +65,7 @@ private Param(Issue issue) { this.message = issue.getMessage(); this.severity = issue.getSeverity().toString(); this.ruleKey = issue.getRuleKey(); - this.flows = issue.flows().stream().map(Flow::new).collect(Collectors.toList()); + this.flows = issue.flows().stream().map(Flow::new).toList(); this.textRange = issue.getTextRange(); this.connectionId = null; this.creationDate = null; @@ -77,7 +76,7 @@ public Param(ShowIssueParams showIssueParams, ProjectBindingManager projectBindi this.message = showIssueParams.getMessage(); this.severity = ""; this.ruleKey = showIssueParams.getRuleKey(); - this.flows = showIssueParams.getFlows().stream().map(flowDto -> new Flow(flowDto, projectBindingManager)).collect(Collectors.toList()); + this.flows = showIssueParams.getFlows().stream().map(flowDto -> new Flow(flowDto, projectBindingManager)).toList(); this.textRange = new TextRange(showIssueParams.getTextRange().getStartLine(), showIssueParams.getTextRange().getStartLineOffset(), showIssueParams.getTextRange().getEndLine(), @@ -110,7 +109,7 @@ public Param(ShowIssueParams showIssueParams, ProjectBindingManager projectBindi this.message = issue.getMessage(); this.severity = issue.getSeverity().toString(); this.ruleKey = issue.getRuleKey(); - this.flows = issue.getFlows().stream().map(f -> new Flow(f, pathResolver, localFileCache)).collect(Collectors.toList()); + this.flows = issue.getFlows().stream().map(f -> new Flow(f, pathResolver, localFileCache)).toList(); this.textRange = issue.getTextRange(); this.connectionId = connectionId; this.creationDate = DateTimeFormatter.ISO_DATE_TIME.format(issue.getCreationDate().atOffset(ZoneOffset.UTC)); @@ -159,15 +158,15 @@ static class Flow { private final List locations; private Flow(org.sonarsource.sonarlint.core.analysis.api.Flow flow) { - this.locations = flow.locations().stream().map(Location::new).collect(Collectors.toList()); + this.locations = flow.locations().stream().map(Location::new).toList(); } private Flow(FlowDto flow, ProjectBindingManager projectBindingManager) { - this.locations = flow.getLocations().stream().map(locationDto -> new Location(locationDto, new HashMap<>(), projectBindingManager)).collect(Collectors.toList()); + this.locations = flow.getLocations().stream().map(locationDto -> new Location(locationDto, new HashMap<>(), projectBindingManager)).toList(); } private Flow(ServerTaintIssue.Flow flow, Function> pathResolver, Map localFileCache) { - this.locations = flow.locations().stream().map(l -> new Location(l, pathResolver, localFileCache)).collect(Collectors.toList()); + this.locations = flow.locations().stream().map(l -> new Location(l, pathResolver, localFileCache)).toList(); } public List getLocations() { diff --git a/src/main/java/org/sonarsource/sonarlint/ls/connected/ServerIssueTrackerWrapper.java b/src/main/java/org/sonarsource/sonarlint/ls/connected/ServerIssueTrackerWrapper.java index fc9671923..19750e5a6 100644 --- a/src/main/java/org/sonarsource/sonarlint/ls/connected/ServerIssueTrackerWrapper.java +++ b/src/main/java/org/sonarsource/sonarlint/ls/connected/ServerIssueTrackerWrapper.java @@ -28,7 +28,6 @@ import java.util.Objects; import java.util.Optional; import java.util.function.Supplier; -import java.util.stream.Collectors; import javax.annotation.CheckForNull; import org.eclipse.lsp4j.jsonrpc.messages.Either; import org.jetbrains.annotations.NotNull; @@ -147,7 +146,7 @@ private static void matchAndTrackIssues(String filePath, IssueListener issueList @NotNull private static Map> getClientTrackedIssuesByServerRelativePath(String filePath, Collection issueTrackables) { - var clientTrackedIssueDtos = issueTrackables.stream().map(ServerIssueTrackerWrapper::createClientTrackedIssueDto).collect(Collectors.toList()); + var clientTrackedIssueDtos = issueTrackables.stream().map(ServerIssueTrackerWrapper::createClientTrackedIssueDto).toList(); return Map.of(filePath, clientTrackedIssueDtos); } @@ -189,9 +188,9 @@ static Collection toIssueTrackables(Collection issues) { return issues.stream() .filter(it -> it.getType() != RuleType.SECURITY_HOTSPOT) .map(issue -> { - var inputFile = issue.getInputFile() instanceof AnalysisClientInputFile ? ((AnalysisClientInputFile) issue.getInputFile()) : null; + var inputFile = issue.getInputFile() instanceof AnalysisClientInputFile actualInputFile ? actualInputFile : null; if (inputFile != null) { - var fileLines = inputFile.contents().lines().collect(Collectors.toList()); + var fileLines = inputFile.contents().lines().toList(); var textRange = issue.getTextRange(); var textRangeContent = getTextRangeContentOfFile(fileLines, textRange); var startLine = issue.getStartLine(); @@ -201,12 +200,13 @@ static Collection toIssueTrackables(Collection issues) { return null; }) .filter(Objects::nonNull) - .collect(Collectors.toList()); + .map(Trackable.class::cast) + .toList(); } private static Collection toHotspotTrackables(Collection issues) { return issues.stream().filter(it -> it.getType() == RuleType.SECURITY_HOTSPOT) - .map(IssueTrackable::new).collect(Collectors.toList()); + .map(IssueTrackable::new).map(Trackable.class::cast).toList(); } } diff --git a/src/main/java/org/sonarsource/sonarlint/ls/connected/domain/TaintIssue.java b/src/main/java/org/sonarsource/sonarlint/ls/connected/domain/TaintIssue.java index 9d92a45dd..cbd7e07d4 100644 --- a/src/main/java/org/sonarsource/sonarlint/ls/connected/domain/TaintIssue.java +++ b/src/main/java/org/sonarsource/sonarlint/ls/connected/domain/TaintIssue.java @@ -22,7 +22,6 @@ import java.time.Instant; import java.util.List; import java.util.Map; -import java.util.stream.Collectors; import org.jetbrains.annotations.Nullable; import org.sonarsource.sonarlint.core.commons.CleanCodeAttribute; import org.sonarsource.sonarlint.core.commons.ImpactSeverity; @@ -66,6 +65,6 @@ public static List from(List serverTaintIssues, bo return serverTaintIssues .stream() .map(serverTaintIssue -> TaintIssue.from(serverTaintIssue, source)) - .collect(Collectors.toList()); + .toList(); } } diff --git a/src/main/java/org/sonarsource/sonarlint/ls/connected/events/ServerSentEventsHandler.java b/src/main/java/org/sonarsource/sonarlint/ls/connected/events/ServerSentEventsHandler.java index aa0d5daba..7fd0e24af 100644 --- a/src/main/java/org/sonarsource/sonarlint/ls/connected/events/ServerSentEventsHandler.java +++ b/src/main/java/org/sonarsource/sonarlint/ls/connected/events/ServerSentEventsHandler.java @@ -62,8 +62,8 @@ public void handleEvents(ServerEvent event) { event instanceof IssueChangedEvent) { taintVulnerabilitiesCache.getAllFilesWithTaintIssues() .forEach(projectBindingManager::updateTaintIssueCacheFromStorageForFile); - } else if (event instanceof ServerHotspotEvent) { - var fileUri = getFileUriFromEvent((ServerHotspotEvent) event); + } else if (event instanceof ServerHotspotEvent serverHotspotEvent) { + var fileUri = getFileUriFromEvent(serverHotspotEvent); fileUri.ifPresent(analysisScheduler::didReceiveHotspotEvent); } } diff --git a/src/main/java/org/sonarsource/sonarlint/ls/folders/WorkspaceFoldersManager.java b/src/main/java/org/sonarsource/sonarlint/ls/folders/WorkspaceFoldersManager.java index ff520cf31..4cc688056 100644 --- a/src/main/java/org/sonarsource/sonarlint/ls/folders/WorkspaceFoldersManager.java +++ b/src/main/java/org/sonarsource/sonarlint/ls/folders/WorkspaceFoldersManager.java @@ -33,7 +33,6 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.function.Function; -import java.util.stream.Collectors; import javax.annotation.CheckForNull; import javax.annotation.Nullable; import org.eclipse.lsp4j.WorkspaceFolder; @@ -141,7 +140,7 @@ public Optional findFolderForFile(URI uri) { .filter(wfRoot -> isAncestor(wfRoot, uri)) // Sort by path descending length to prefer the deepest one in case of multiple nested workspace folders .sorted(Comparator.comparingInt(wfRoot -> wfRoot.getPath().length()).reversed()) - .collect(Collectors.toList()); + .toList(); if (folderUriCandidates.isEmpty()) { return Optional.empty(); } diff --git a/src/main/java/org/sonarsource/sonarlint/ls/notebooks/NotebookDiagnosticPublisher.java b/src/main/java/org/sonarsource/sonarlint/ls/notebooks/NotebookDiagnosticPublisher.java index f222de4a6..664c35d49 100644 --- a/src/main/java/org/sonarsource/sonarlint/ls/notebooks/NotebookDiagnosticPublisher.java +++ b/src/main/java/org/sonarsource/sonarlint/ls/notebooks/NotebookDiagnosticPublisher.java @@ -63,14 +63,14 @@ public void publishNotebookDiagnostics(URI uri, VersionedOpenNotebook versionedO var localDiagnostics = localIssues.entrySet() .stream() - .map(entry -> Map.entry(entry.getKey(), versionedOpenNotebook.toCellIssue(entry.getValue().getIssue()))) + .map(entry -> Map.entry(entry.getKey(), versionedOpenNotebook.toCellIssue(entry.getValue().issue()))) .map(NotebookDiagnosticPublisher::convertCellIssue) .collect(groupingBy(diagnostic -> { var localIssue = localIssues.get(((DiagnosticPublisher.DiagnosticData) diagnostic.getData()).getEntryKey()); var cellUri = URI.create(""); - if (localIssue != null && localIssue.getIssue() != null && localIssue.getIssue().getStartLine() != null) { + if (localIssue != null && localIssue.issue() != null && localIssue.issue().getStartLine() != null) { // Better to not publish any diagnostics than to publish for wrong location - cellUri = versionedOpenNotebook.getCellUri(localIssue.getIssue().getStartLine()).orElse(URI.create("")); + cellUri = versionedOpenNotebook.getCellUri(localIssue.issue().getStartLine()).orElse(URI.create("")); } var cellsWithIssues = notebookCellsWithIssues.get(uri); diff --git a/src/main/java/org/sonarsource/sonarlint/ls/notebooks/VersionedOpenNotebook.java b/src/main/java/org/sonarsource/sonarlint/ls/notebooks/VersionedOpenNotebook.java index 2a5f42464..2e972c9ab 100644 --- a/src/main/java/org/sonarsource/sonarlint/ls/notebooks/VersionedOpenNotebook.java +++ b/src/main/java/org/sonarsource/sonarlint/ls/notebooks/VersionedOpenNotebook.java @@ -143,10 +143,10 @@ public DelegatingCellIssue toCellIssue(Issue issue) { var newTextRange = fileTextRangeToCellTextRange(textEdit.range().getStartLine(), textEdit.range().getStartLineOffset(), textEdit.range().getEndLine(), textEdit.range().getEndLineOffset(), virtualFileLineToCellLine); return new TextEdit(newTextRange, textEdit.newText()); - }).collect(Collectors.toList()); + }).toList(); var clientInputFile = new InFolderClientInputFile(textEditCellUri.get(), "", false); return new ClientInputFileEdit(clientInputFile, newTextEdits); - }).collect(Collectors.toList()); + }).toList(); var convertedQuickFix = new QuickFix(newFileEdits, quickFix.message()); convertedQuickFixes.add(convertedQuickFix); } diff --git a/src/main/java/org/sonarsource/sonarlint/ls/settings/RulesConfiguration.java b/src/main/java/org/sonarsource/sonarlint/ls/settings/RulesConfiguration.java index 5cc04aca0..bbdea0938 100644 --- a/src/main/java/org/sonarsource/sonarlint/ls/settings/RulesConfiguration.java +++ b/src/main/java/org/sonarsource/sonarlint/ls/settings/RulesConfiguration.java @@ -89,7 +89,7 @@ private static RuleKey safeParseRuleKey(String key) { @CheckForNull private static String safeParseLevel(Map config) { Object levelValue = config.get("level"); - return levelValue instanceof String ? (String) levelValue : null; + return levelValue instanceof String actualValue ? actualValue : null; } @SuppressWarnings("unchecked") diff --git a/src/main/java/org/sonarsource/sonarlint/ls/settings/SettingsManager.java b/src/main/java/org/sonarsource/sonarlint/ls/settings/SettingsManager.java index 1b3c6ff4d..2990f28ff 100644 --- a/src/main/java/org/sonarsource/sonarlint/ls/settings/SettingsManager.java +++ b/src/main/java/org/sonarsource/sonarlint/ls/settings/SettingsManager.java @@ -387,7 +387,7 @@ private String getTokenFromClient(String serverUrlOrOrganization) { } private static boolean checkRequiredAttribute(Map map, String label, String... requiredAttributes) { - var missing = stream(requiredAttributes).filter(att -> isBlank((String) map.get(att))).collect(Collectors.toList()); + var missing = stream(requiredAttributes).filter(att -> isBlank((String) map.get(att))).toList(); if (!missing.isEmpty()) { LOG.error("Incomplete {} connection configuration. Required parameters must not be blank: {}.", label, String.join(",", missing)); return false; diff --git a/src/main/java/org/sonarsource/sonarlint/ls/util/Utils.java b/src/main/java/org/sonarsource/sonarlint/ls/util/Utils.java index 17bd1f723..fadaf7fc4 100644 --- a/src/main/java/org/sonarsource/sonarlint/ls/util/Utils.java +++ b/src/main/java/org/sonarsource/sonarlint/ls/util/Utils.java @@ -241,7 +241,7 @@ public static Optional safelyGetCompletableFuture(CompletableFuture fu } public static boolean isDelegatingIssueWithServerIssueKey(String serverIssueKey, Map.Entry issueEntry) { - return issueEntry.getValue().getIssue() instanceof DelegatingIssue - && (serverIssueKey.equals(((DelegatingIssue) issueEntry.getValue().getIssue()).getServerIssueKey())); + return issueEntry.getValue().issue() instanceof DelegatingIssue delegatingIssue + && (serverIssueKey.equals(delegatingIssue.getServerIssueKey())); } }