Skip to content

Commit 057b165

Browse files
authored
Add nullability checks (#5839)
1 parent bafc6b6 commit 057b165

File tree

2 files changed

+40
-11
lines changed

2 files changed

+40
-11
lines changed

src/io/flutter/jxbrowser/JxBrowserManager.java

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import io.flutter.utils.FileUtils;
2828
import io.flutter.utils.JxBrowserUtils;
2929
import org.jetbrains.annotations.NotNull;
30+
import org.jetbrains.annotations.Nullable;
3031

3132
import java.io.File;
3233
import java.io.FileNotFoundException;
@@ -50,20 +51,26 @@
5051
public class JxBrowserManager {
5152
private static JxBrowserManager manager;
5253

54+
@NotNull
5355
protected static final String DOWNLOAD_PATH =
5456
PathManager.getPluginsPath() + File.separatorChar + "flutter-intellij" + File.separatorChar + "jxbrowser";
57+
@NotNull
5558
private static final AtomicReference<JxBrowserStatus> status = new AtomicReference<>(JxBrowserStatus.NOT_INSTALLED);
59+
@NotNull
5660
private static final AtomicBoolean listeningForSetting = new AtomicBoolean(false);
61+
@NotNull
5762
private static final Logger LOG = Logger.getInstance(JxBrowserManager.class);
63+
@NotNull
5864
private static CompletableFuture<JxBrowserStatus> installation = new CompletableFuture<>();
65+
@NotNull
5966
public static final String ANALYTICS_CATEGORY = "jxbrowser";
6067
private static InstallationFailedReason latestFailureReason;
6168
private final JxBrowserUtils jxBrowserUtils;
6269
private final Analytics analytics;
6370
private final FileUtils fileUtils;
6471

6572
@VisibleForTesting
66-
protected JxBrowserManager(JxBrowserUtils jxBrowserUtils, Analytics analytics, FileUtils fileUtils) {
73+
protected JxBrowserManager(@NotNull JxBrowserUtils jxBrowserUtils, @NotNull Analytics analytics, @NotNull FileUtils fileUtils) {
6774
this.jxBrowserUtils = jxBrowserUtils;
6875
this.analytics = analytics;
6976
this.fileUtils = fileUtils;
@@ -72,6 +79,7 @@ protected JxBrowserManager(JxBrowserUtils jxBrowserUtils, Analytics analytics, F
7279
@NotNull
7380
public static JxBrowserManager getInstance() {
7481
if (manager == null) {
82+
//noinspection ConstantConditions
7583
manager = new JxBrowserManager(new JxBrowserUtils(), FlutterInitializer.getAnalytics(), FileUtils.getInstance());
7684
}
7785
return manager;
@@ -82,17 +90,21 @@ protected static void resetForTest() {
8290
status.set(JxBrowserStatus.NOT_INSTALLED);
8391
}
8492

93+
@NotNull
8594
public JxBrowserStatus getStatus() {
95+
//noinspection ConstantConditions
8696
return status.get();
8797
}
8898

99+
@Nullable
89100
public InstallationFailedReason getLatestFailureReason() {
90101
return latestFailureReason;
91102
}
92103

93104
/**
94105
* Call {@link #setUp} before this function to ensure that an installation has started.
95106
*/
107+
@Nullable
96108
public JxBrowserStatus waitForInstallation(int seconds) throws TimeoutException {
97109
try {
98110
return installation.get(seconds, TimeUnit.SECONDS);
@@ -114,7 +126,7 @@ public void retryFromFailed(@NotNull Project project) {
114126
private class SettingsListener implements FlutterSettings.Listener {
115127
final Project project;
116128

117-
public SettingsListener(Project project) {
129+
public SettingsListener(@NotNull Project project) {
118130
this.project = project;
119131
}
120132

@@ -123,18 +135,19 @@ public void settingsChanged() {
123135
final FlutterSettings settings = FlutterSettings.getInstance();
124136

125137
// Set up JxBrowser files if the embedded inspector option has been turned on and the files aren't already loaded.
138+
//noinspection ConstantConditions
126139
if (settings.isEnableEmbeddedBrowsers() && getStatus().equals(JxBrowserStatus.NOT_INSTALLED)) {
127140
setUp(project);
128141
}
129142
}
130143
}
131144

132-
private void setStatusFailed(InstallationFailedReason reason) {
145+
private void setStatusFailed(@NotNull InstallationFailedReason reason) {
133146
setStatusFailed(reason, null);
134147
}
135148

136-
private void setStatusFailed(InstallationFailedReason reason, Long time) {
137-
StringBuilder eventName = new StringBuilder();
149+
private void setStatusFailed(@NotNull InstallationFailedReason reason, @Nullable Long time) {
150+
final StringBuilder eventName = new StringBuilder();
138151
eventName.append("installationFailed-");
139152
eventName.append(reason.failureType);
140153
if (reason.detail != null) {
@@ -159,6 +172,7 @@ public void listenForSettingChanges(@NotNull Project project) {
159172
return;
160173
}
161174

175+
//noinspection ConstantConditions
162176
FlutterSettings.getInstance().addListener(new SettingsListener(project));
163177
}
164178

@@ -221,6 +235,7 @@ public void setUp(@NotNull Project project) {
221235
final String[] fileNames = {platformFileName, jxBrowserUtils.getApiFileName(), jxBrowserUtils.getSwingFileName()};
222236
boolean allDownloaded = true;
223237
for (String fileName : fileNames) {
238+
assert fileName != null;
224239
if (!fileUtils.fileExists(getFilePath(fileName))) {
225240
allDownloaded = false;
226241
break;
@@ -236,6 +251,7 @@ public void setUp(@NotNull Project project) {
236251
// Delete any already existing files.
237252
// TODO(helin24): Handle if files cannot be deleted.
238253
for (String fileName : fileNames) {
254+
assert fileName != null;
239255
final String filePath = getFilePath(fileName);
240256
if (!fileUtils.deleteFile(filePath)) {
241257
LOG.info(project.getName() + ": Existing file could not be deleted - " + filePath);
@@ -245,12 +261,14 @@ public void setUp(@NotNull Project project) {
245261
downloadJxBrowser(project, fileNames);
246262
}
247263

248-
protected void downloadJxBrowser(Project project, String[] fileNames) {
264+
protected void downloadJxBrowser(@NotNull Project project, @NotNull String[] fileNames) {
249265
// The FileDownloader API is used by other plugins - e.g.
250266
// https://github.com/JetBrains/intellij-community/blob/b09f8151e0d189d70363266c3bb6edb5f6bfeca4/plugins/markdown/src/org/intellij/plugins/markdown/ui/preview/javafx/JavaFXInstallator.java#L48
251267
final List<FileDownloader> fileDownloaders = new ArrayList<>();
252268
final DownloadableFileService service = DownloadableFileService.getInstance();
269+
assert service != null;
253270
for (String fileName : fileNames) {
271+
assert fileName != null;
254272
final DownloadableFileDescription
255273
description = service.createFileDescription(jxBrowserUtils.getDistributionLink(fileName), fileName);
256274
fileDownloaders.add(service.createDownloader(Collections.singletonList(description), fileName));
@@ -264,6 +282,7 @@ public void run(@NotNull ProgressIndicator indicator) {
264282
try {
265283
for (int i = 0; i < fileDownloaders.size(); i++) {
266284
final FileDownloader downloader = fileDownloaders.get(i);
285+
assert downloader != null;
267286
currentFileName = fileNames[i];
268287
final Pair<File, DownloadableFileDescription> download =
269288
ContainerUtil.getFirstItem(downloader.download(new File(DOWNLOAD_PATH)));
@@ -288,11 +307,13 @@ public void run(@NotNull ProgressIndicator indicator) {
288307
ProgressManager.getInstance().runProcessWithProgressAsynchronously(task, processIndicator);
289308
}
290309

291-
private void loadClasses(String[] fileNames) {
310+
private void loadClasses(@NotNull String[] fileNames) {
292311
for (String fileName : fileNames) {
312+
assert fileName != null;
293313
final String fullPath = getFilePath(fileName);
294314

295315
try {
316+
//noinspection ConstantConditions
296317
fileUtils.loadClass(this.getClass().getClassLoader(), fullPath);
297318
} catch (Exception ex) {
298319
LOG.info("Failed to load JxBrowser file", ex);
@@ -303,6 +324,7 @@ private void loadClasses(String[] fileNames) {
303324
LOG.info("Loaded JxBrowser file successfully: " + fullPath);
304325
}
305326
try {
327+
//noinspection ThrowableNotThrown
306328
final UnsupportedRenderingModeException test = new UnsupportedRenderingModeException(RenderingMode.HARDWARE_ACCELERATED);
307329
} catch (NoClassDefFoundError e) {
308330
LOG.info("Failed to find JxBrowser class");
@@ -314,13 +336,15 @@ private void loadClasses(String[] fileNames) {
314336
installation.complete(JxBrowserStatus.INSTALLED);
315337
}
316338

317-
private void loadClasses2021(String[] fileNames) {
318-
List<Path> paths = new ArrayList<>();
339+
private void loadClasses2021(@NotNull String[] fileNames) {
340+
final List<Path> paths = new ArrayList<>();
319341

320342
try {
321343
for (String fileName: fileNames) {
344+
assert fileName != null;
322345
paths.add(Paths.get(getFilePath(fileName)));
323346
}
347+
//noinspection ConstantConditions
324348
fileUtils.loadPaths(this.getClass().getClassLoader(), paths);
325349
} catch (Exception ex) {
326350
LOG.info("Failed to load JxBrowser file", ex);
@@ -329,6 +353,7 @@ private void loadClasses2021(String[] fileNames) {
329353
}
330354

331355
try {
356+
//noinspection ThrowableNotThrown
332357
final UnsupportedRenderingModeException test = new UnsupportedRenderingModeException(RenderingMode.HARDWARE_ACCELERATED);
333358
} catch (NoClassDefFoundError e) {
334359
LOG.info("Failed to find JxBrowser class");
@@ -340,7 +365,8 @@ private void loadClasses2021(String[] fileNames) {
340365
installation.complete(JxBrowserStatus.INSTALLED);
341366
}
342367

343-
private String getFilePath(String fileName) {
368+
@NotNull
369+
private String getFilePath(@NotNull String fileName) {
344370
return DOWNLOAD_PATH + File.separatorChar + fileName;
345371
}
346372
}

src/io/flutter/utils/JxBrowserUtils.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package io.flutter.utils;
77

88
import com.intellij.openapi.util.SystemInfo;
9+
import org.jetbrains.annotations.NotNull;
910
// import com.intellij.util.system.CpuArch;
1011

1112
import java.io.FileNotFoundException;
@@ -47,10 +48,12 @@ public String getSwingFileName() {
4748
return String.format("%s-swing-%s.%s", JXBROWSER_FILE_PREFIX, JXBROWSER_FILE_VERSION, JXBROWSER_FILE_SUFFIX);
4849
}
4950

50-
public String getDistributionLink(String fileName) {
51+
@NotNull
52+
public String getDistributionLink(@NotNull String fileName) {
5153
return "https://storage.googleapis.com/flutter_infra_release/flutter/intellij/jxbrowser/" + fileName;
5254
}
5355

56+
@NotNull
5457
public String getJxBrowserKey() throws FileNotFoundException {
5558
if (JxBrowserUtils.class.getResource("/jxbrowser/jxbrowser.properties") == null) {
5659
throw new FileNotFoundException("jxbrowser.properties file does not exist");

0 commit comments

Comments
 (0)