Skip to content

[perf] use try-with-resources for auto-closeables #8111

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 48 additions & 46 deletions flutter-idea/src/io/flutter/FlutterInitializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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) {
Expand Down
8 changes: 4 additions & 4 deletions flutter-idea/src/io/flutter/bazel/WorkspaceCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
});
}

Expand Down