From 9e05e540da5dd6a4b9364ee19cb94464a6d986f7 Mon Sep 17 00:00:00 2001 From: pq Date: Wed, 23 Apr 2025 16:30:34 -0700 Subject: [PATCH] [perf] use `try`-with-resources for auto-closeables --- .../src/io/flutter/FlutterInitializer.java | 94 ++++++++++--------- .../src/io/flutter/bazel/WorkspaceCache.java | 8 +- .../logging/FlutterConsoleLogManager.java | 9 +- .../survey/FlutterSurveyNotifications.java | 15 +-- .../io/flutter/project/ProjectWatchTest.java | 16 ++-- 5 files changed, 72 insertions(+), 70 deletions(-) diff --git a/flutter-idea/src/io/flutter/FlutterInitializer.java b/flutter-idea/src/io/flutter/FlutterInitializer.java index 3c9d8b87ec..3497a10467 100644 --- a/flutter-idea/src/io/flutter/FlutterInitializer.java +++ b/flutter-idea/src/io/flutter/FlutterInitializer.java @@ -191,7 +191,7 @@ private void setUpDtdAnalytics(Project project) { //Thread t1 = new Thread(() -> { // UnifiedAnalytics unifiedAnalytics = new UnifiedAnalytics(project); // TODO(helin24): Turn on after adding some unified analytics reporting. - //unifiedAnalytics.manageConsent(); + //unifiedAnalytics.manageConsent(); //}); //t1.start(); } @@ -222,55 +222,57 @@ private void sendThemeChangedEvent(@NotNull Project project) { lastScheduledThemeChangeTime.set(requestTime); // Schedule event to be sent in a second if nothing more recent has come in. - Executors.newSingleThreadScheduledExecutor().schedule(() -> { - if (lastScheduledThemeChangeTime.get() != requestTime) { - // A more recent request has been set, so drop this request. - return; - } - - final JsonObject params = new JsonObject(); - params.addProperty("eventKind", "themeChanged"); - params.addProperty("streamId", "Editor"); - - final JsonObject themeData = new JsonObject(); - final DevToolsUtils utils = new DevToolsUtils(); - themeData.addProperty("isDarkMode", Boolean.FALSE.equals(utils.getIsBackgroundBright())); - themeData.addProperty("backgroundColor", utils.getColorHexCode()); - themeData.addProperty("fontSize", utils.getFontSize().intValue()); - - final JsonObject eventData = new JsonObject(); - eventData.add("theme", themeData); - params.add("eventData", eventData); - - try { - final DtdUtils dtdUtils = new DtdUtils(); - final DartToolingDaemonService dtdService = dtdUtils.readyDtdService(project).get(); - if (dtdService == null) { - LOG.error("Unable to send theme changed event because DTD service is null"); + try (var executor = Executors.newSingleThreadScheduledExecutor()) { + executor.schedule(() -> { + if (lastScheduledThemeChangeTime.get() != requestTime) { + // A more recent request has been set, so drop this request. return; } - dtdService.sendRequest("postEvent", params, false, object -> { - JsonObject result = object.getAsJsonObject("result"); - if (result == null) { - LOG.error("Theme changed event returned null result"); - return; - } - JsonPrimitive type = result.getAsJsonPrimitive("type"); - if (type == null) { - LOG.error("Theme changed event result type is null"); - return; - } - if (!"Success".equals(type.getAsString())) { - LOG.error("Theme changed event result: " + type.getAsString()); + final JsonObject params = new JsonObject(); + params.addProperty("eventKind", "themeChanged"); + params.addProperty("streamId", "Editor"); + + final JsonObject themeData = new JsonObject(); + final DevToolsUtils utils = new DevToolsUtils(); + themeData.addProperty("isDarkMode", Boolean.FALSE.equals(utils.getIsBackgroundBright())); + themeData.addProperty("backgroundColor", utils.getColorHexCode()); + themeData.addProperty("fontSize", utils.getFontSize().intValue()); + + final JsonObject eventData = new JsonObject(); + eventData.add("theme", themeData); + params.add("eventData", eventData); + + try { + final DtdUtils dtdUtils = new DtdUtils(); + final DartToolingDaemonService dtdService = dtdUtils.readyDtdService(project).get(); + if (dtdService == null) { + LOG.error("Unable to send theme changed event because DTD service is null"); + return; + } + + dtdService.sendRequest("postEvent", params, false, object -> { + JsonObject result = object.getAsJsonObject("result"); + if (result == null) { + LOG.error("Theme changed event returned null result"); + return; + } + JsonPrimitive type = result.getAsJsonPrimitive("type"); + if (type == null) { + LOG.error("Theme changed event result type is null"); + return; + } + if (!"Success".equals(type.getAsString())) { + LOG.error("Theme changed event result: " + type.getAsString()); + } } - } - ); - } - catch (WebSocketException | InterruptedException | ExecutionException e) { - LOG.error("Unable to send theme changed event", e); - } - }, 1, TimeUnit.SECONDS); + ); + } + catch (WebSocketException | InterruptedException | ExecutionException e) { + LOG.error("Unable to send theme changed event", e); + } + }, 1, TimeUnit.SECONDS); + } } private void checkSdkVersionNotification(@NotNull Project project) { diff --git a/flutter-idea/src/io/flutter/bazel/WorkspaceCache.java b/flutter-idea/src/io/flutter/bazel/WorkspaceCache.java index 7d52966fc2..dca79fd5e3 100644 --- a/flutter-idea/src/io/flutter/bazel/WorkspaceCache.java +++ b/flutter-idea/src/io/flutter/bazel/WorkspaceCache.java @@ -69,10 +69,10 @@ private WorkspaceCache(@NotNull final Project project) { }); // Detect module root changes. - ProjectWatch.subscribe(project, this::scheduleRefresh); - - // Load initial value. - refresh(); + try (var projectWatch = ProjectWatch.subscribe(project, this::scheduleRefresh)) { + // Load initial value. + refresh(); + } } private void scheduleRefresh() { diff --git a/flutter-idea/src/io/flutter/logging/FlutterConsoleLogManager.java b/flutter-idea/src/io/flutter/logging/FlutterConsoleLogManager.java index 15a0b949a3..3f7514153b 100644 --- a/flutter-idea/src/io/flutter/logging/FlutterConsoleLogManager.java +++ b/flutter-idea/src/io/flutter/logging/FlutterConsoleLogManager.java @@ -45,10 +45,7 @@ import java.util.ArrayList; import java.util.Collections; -import java.util.concurrent.CompletableFuture; -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.Executors; -import java.util.concurrent.TimeUnit; +import java.util.concurrent.*; import java.util.concurrent.atomic.AtomicInteger; /** @@ -391,7 +388,9 @@ public void actionPerformed(@NotNull AnActionEvent event) { } }); Notifications.Bus.notify(notification, app.getProject()); - Executors.newSingleThreadScheduledExecutor().schedule(notification::expire, 25, TimeUnit.SECONDS); + try (var executor = Executors.newSingleThreadScheduledExecutor()) { + executor.schedule(notification::expire, 25, TimeUnit.SECONDS); + } } private String getChildIndent(String indent, DiagnosticsNode property) { diff --git a/flutter-idea/src/io/flutter/survey/FlutterSurveyNotifications.java b/flutter-idea/src/io/flutter/survey/FlutterSurveyNotifications.java index 381dc5851f..3f66dbe060 100644 --- a/flutter-idea/src/io/flutter/survey/FlutterSurveyNotifications.java +++ b/flutter-idea/src/io/flutter/survey/FlutterSurveyNotifications.java @@ -111,12 +111,13 @@ public void actionPerformed(@NotNull AnActionEvent event) { }); // Display the prompt after a short delay. - final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(); - scheduler.schedule(() -> { - if (!myProject.isDisposed()) { - Notifications.Bus.notify(notification, myProject); - } - }, NOTIFICATION_DELAY_IN_SECS, TimeUnit.SECONDS); - scheduler.shutdown(); + try (var scheduler = Executors.newSingleThreadScheduledExecutor()) { + scheduler.schedule(() -> { + if (!myProject.isDisposed()) { + Notifications.Bus.notify(notification, myProject); + } + }, NOTIFICATION_DELAY_IN_SECS, TimeUnit.SECONDS); + scheduler.shutdown(); + } } } diff --git a/flutter-idea/testSrc/unit/io/flutter/project/ProjectWatchTest.java b/flutter-idea/testSrc/unit/io/flutter/project/ProjectWatchTest.java index e67e7bb25d..cde2f3329c 100644 --- a/flutter-idea/testSrc/unit/io/flutter/project/ProjectWatchTest.java +++ b/flutter-idea/testSrc/unit/io/flutter/project/ProjectWatchTest.java @@ -43,14 +43,14 @@ public void shouldSendEventWhenProjectCloses() throws Exception { public void shouldSendEventWhenModuleRootsChange() throws Exception { Testing.runOnDispatchThread(() -> { final AtomicInteger callCount = new AtomicInteger(); - final ProjectWatch listen = ProjectWatch.subscribe(fixture.getProject(), callCount::incrementAndGet); - - VirtualFile[] contentRoots = ModuleRootManager.getInstance(fixture.getModule()).getContentRoots(); - VirtualFile dir = contentRoots[0].createChildDirectory(this, "testDir"); - ModuleRootModificationUtil.addContentRoot(fixture.getModule(), dir); - PlatformTestUtil.dispatchAllEventsInIdeEventQueue(); - // The number of events fired is an implementation detail of the project manager. We just need at least one. - assertNotEquals(0, callCount.get()); + try (var listen = ProjectWatch.subscribe(fixture.getProject(), callCount::incrementAndGet)) { + VirtualFile[] contentRoots = ModuleRootManager.getInstance(fixture.getModule()).getContentRoots(); + VirtualFile dir = contentRoots[0].createChildDirectory(this, "testDir"); + ModuleRootModificationUtil.addContentRoot(fixture.getModule(), dir); + PlatformTestUtil.dispatchAllEventsInIdeEventQueue(); + // The number of events fired is an implementation detail of the project manager. We just need at least one. + assertNotEquals(0, callCount.get()); + } }); }