Skip to content

Add nullability checks #5839

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 1 commit into from
Oct 26, 2021
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
46 changes: 36 additions & 10 deletions src/io/flutter/jxbrowser/JxBrowserManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import io.flutter.utils.FileUtils;
import io.flutter.utils.JxBrowserUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.io.FileNotFoundException;
Expand All @@ -50,20 +51,26 @@
public class JxBrowserManager {
private static JxBrowserManager manager;

@NotNull
protected static final String DOWNLOAD_PATH =
PathManager.getPluginsPath() + File.separatorChar + "flutter-intellij" + File.separatorChar + "jxbrowser";
@NotNull
private static final AtomicReference<JxBrowserStatus> status = new AtomicReference<>(JxBrowserStatus.NOT_INSTALLED);
@NotNull
private static final AtomicBoolean listeningForSetting = new AtomicBoolean(false);
@NotNull
private static final Logger LOG = Logger.getInstance(JxBrowserManager.class);
@NotNull
private static CompletableFuture<JxBrowserStatus> installation = new CompletableFuture<>();
@NotNull
public static final String ANALYTICS_CATEGORY = "jxbrowser";
private static InstallationFailedReason latestFailureReason;
private final JxBrowserUtils jxBrowserUtils;
private final Analytics analytics;
private final FileUtils fileUtils;

@VisibleForTesting
protected JxBrowserManager(JxBrowserUtils jxBrowserUtils, Analytics analytics, FileUtils fileUtils) {
protected JxBrowserManager(@NotNull JxBrowserUtils jxBrowserUtils, @NotNull Analytics analytics, @NotNull FileUtils fileUtils) {
this.jxBrowserUtils = jxBrowserUtils;
this.analytics = analytics;
this.fileUtils = fileUtils;
Expand All @@ -72,6 +79,7 @@ protected JxBrowserManager(JxBrowserUtils jxBrowserUtils, Analytics analytics, F
@NotNull
public static JxBrowserManager getInstance() {
if (manager == null) {
//noinspection ConstantConditions
manager = new JxBrowserManager(new JxBrowserUtils(), FlutterInitializer.getAnalytics(), FileUtils.getInstance());
}
return manager;
Expand All @@ -82,17 +90,21 @@ protected static void resetForTest() {
status.set(JxBrowserStatus.NOT_INSTALLED);
}

@NotNull
public JxBrowserStatus getStatus() {
//noinspection ConstantConditions
return status.get();
}

@Nullable
public InstallationFailedReason getLatestFailureReason() {
return latestFailureReason;
}

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

public SettingsListener(Project project) {
public SettingsListener(@NotNull Project project) {
this.project = project;
}

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

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

private void setStatusFailed(InstallationFailedReason reason) {
private void setStatusFailed(@NotNull InstallationFailedReason reason) {
setStatusFailed(reason, null);
}

private void setStatusFailed(InstallationFailedReason reason, Long time) {
StringBuilder eventName = new StringBuilder();
private void setStatusFailed(@NotNull InstallationFailedReason reason, @Nullable Long time) {
final StringBuilder eventName = new StringBuilder();
eventName.append("installationFailed-");
eventName.append(reason.failureType);
if (reason.detail != null) {
Expand All @@ -159,6 +172,7 @@ public void listenForSettingChanges(@NotNull Project project) {
return;
}

//noinspection ConstantConditions
FlutterSettings.getInstance().addListener(new SettingsListener(project));
}

Expand Down Expand Up @@ -221,6 +235,7 @@ public void setUp(@NotNull Project project) {
final String[] fileNames = {platformFileName, jxBrowserUtils.getApiFileName(), jxBrowserUtils.getSwingFileName()};
boolean allDownloaded = true;
for (String fileName : fileNames) {
assert fileName != null;
if (!fileUtils.fileExists(getFilePath(fileName))) {
allDownloaded = false;
break;
Expand All @@ -236,6 +251,7 @@ public void setUp(@NotNull Project project) {
// Delete any already existing files.
// TODO(helin24): Handle if files cannot be deleted.
for (String fileName : fileNames) {
assert fileName != null;
final String filePath = getFilePath(fileName);
if (!fileUtils.deleteFile(filePath)) {
LOG.info(project.getName() + ": Existing file could not be deleted - " + filePath);
Expand All @@ -245,12 +261,14 @@ public void setUp(@NotNull Project project) {
downloadJxBrowser(project, fileNames);
}

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

private void loadClasses(String[] fileNames) {
private void loadClasses(@NotNull String[] fileNames) {
for (String fileName : fileNames) {
assert fileName != null;
final String fullPath = getFilePath(fileName);

try {
//noinspection ConstantConditions
fileUtils.loadClass(this.getClass().getClassLoader(), fullPath);
} catch (Exception ex) {
LOG.info("Failed to load JxBrowser file", ex);
Expand All @@ -303,6 +324,7 @@ private void loadClasses(String[] fileNames) {
LOG.info("Loaded JxBrowser file successfully: " + fullPath);
}
try {
//noinspection ThrowableNotThrown
final UnsupportedRenderingModeException test = new UnsupportedRenderingModeException(RenderingMode.HARDWARE_ACCELERATED);
} catch (NoClassDefFoundError e) {
LOG.info("Failed to find JxBrowser class");
Expand All @@ -314,13 +336,15 @@ private void loadClasses(String[] fileNames) {
installation.complete(JxBrowserStatus.INSTALLED);
}

private void loadClasses2021(String[] fileNames) {
List<Path> paths = new ArrayList<>();
private void loadClasses2021(@NotNull String[] fileNames) {
final List<Path> paths = new ArrayList<>();

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

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

private String getFilePath(String fileName) {
@NotNull
private String getFilePath(@NotNull String fileName) {
return DOWNLOAD_PATH + File.separatorChar + fileName;
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/io/flutter/utils/JxBrowserUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package io.flutter.utils;

import com.intellij.openapi.util.SystemInfo;
import org.jetbrains.annotations.NotNull;
// import com.intellij.util.system.CpuArch;

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

public String getDistributionLink(String fileName) {
@NotNull
public String getDistributionLink(@NotNull String fileName) {
return "https://storage.googleapis.com/flutter_infra_release/flutter/intellij/jxbrowser/" + fileName;
}

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