diff --git a/flutter-idea/src/io/flutter/jxbrowser/JxBrowserManager.java b/flutter-idea/src/io/flutter/jxbrowser/JxBrowserManager.java index 031588b1a0..8d56770800 100644 --- a/flutter-idea/src/io/flutter/jxbrowser/JxBrowserManager.java +++ b/flutter-idea/src/io/flutter/jxbrowser/JxBrowserManager.java @@ -87,19 +87,17 @@ else if (Objects.equals(info.getMajorVersion(), "2020")) { public static final String ANALYTICS_CATEGORY = "jxbrowser"; private static InstallationFailedReason latestFailureReason; private final JxBrowserUtils jxBrowserUtils; - private final FileUtils fileUtils; @VisibleForTesting - protected JxBrowserManager(@NotNull JxBrowserUtils jxBrowserUtils, @NotNull FileUtils fileUtils) { + protected JxBrowserManager(@NotNull JxBrowserUtils jxBrowserUtils) { this.jxBrowserUtils = jxBrowserUtils; - this.fileUtils = fileUtils; } @NotNull public static JxBrowserManager getInstance() { if (manager == null) { //noinspection ConstantConditions - manager = new JxBrowserManager(new JxBrowserUtils(), FileUtils.getInstance()); + manager = new JxBrowserManager(new JxBrowserUtils()); } return manager; } @@ -229,7 +227,7 @@ public void setUp(@NotNull String projectName) { LOG.info(projectName + ": Installing JxBrowser"); - final boolean directoryExists = fileUtils.makeDirectory(DOWNLOAD_PATH); + final boolean directoryExists = FileUtils.makeDirectory(DOWNLOAD_PATH); if (!directoryExists) { LOG.info(projectName + ": Unable to create directory for JxBrowser files"); setStatusFailed(new InstallationFailedReason(FailureType.DIRECTORY_CREATION_FAILED)); @@ -251,7 +249,7 @@ public void setUp(@NotNull String projectName) { boolean allDownloaded = true; for (String fileName : fileNames) { assert fileName != null; - if (!fileUtils.fileExists(getFilePath(fileName))) { + if (!FileUtils.fileExists(getFilePath(fileName))) { allDownloaded = false; break; } @@ -268,7 +266,7 @@ public void setUp(@NotNull String projectName) { for (String fileName : fileNames) { assert fileName != null; final String filePath = getFilePath(fileName); - if (!fileUtils.deleteFile(filePath)) { + if (!FileUtils.deleteFile(filePath)) { LOG.info(projectName + ": Existing file could not be deleted - " + filePath); } } @@ -350,7 +348,7 @@ private void loadClasses(@NotNull String[] fileNames) { paths.add(Paths.get(getFilePath(fileName))); } //noinspection ConstantConditions - fileUtils.loadPaths(this.getClass().getClassLoader(), paths); + FileUtils.loadPaths(this.getClass().getClassLoader(), paths); } catch (Exception ex) { LOG.info("Failed to load JxBrowser file", ex); diff --git a/flutter-idea/src/io/flutter/utils/AsyncUtils.java b/flutter-idea/src/io/flutter/utils/AsyncUtils.java index 50fc0769fb..fd0a31612c 100644 --- a/flutter-idea/src/io/flutter/utils/AsyncUtils.java +++ b/flutter-idea/src/io/flutter/utils/AsyncUtils.java @@ -15,13 +15,21 @@ import java.util.concurrent.CompletableFuture; import java.util.function.BiConsumer; +import org.jetbrains.annotations.NotNull; + public class AsyncUtils { + + private AsyncUtils() { + throw new AssertionError("No instances."); + } + /** * Helper to get the value of a future on the UI thread. *

* The action will never be called if the future is cancelled. */ - public static void whenCompleteUiThread(CompletableFuture future, BiConsumer action) { + public static void whenCompleteUiThread(@NotNull CompletableFuture future, + @NotNull BiConsumer action) { future.whenCompleteAsync( (T value, Throwable throwable) -> { // Exceptions due to the Future being cancelled need to be treated @@ -39,7 +47,7 @@ public static void whenCompleteUiThread(CompletableFuture future, BiConsu ); } - public static void invokeLater(Runnable runnable) { + public static void invokeLater(@NotNull Runnable runnable) { final Application app = ApplicationManager.getApplication(); if (app == null || app.isUnitTestMode()) { // This case existing to support unit testing. @@ -50,7 +58,7 @@ public static void invokeLater(Runnable runnable) { } } - public static void invokeAndWait(Runnable runnable) throws ProcessCanceledException { + public static void invokeAndWait(@NotNull Runnable runnable) throws ProcessCanceledException { final Application app = ApplicationManager.getApplication(); if (app == null || app.isUnitTestMode()) { try { diff --git a/flutter-idea/src/io/flutter/utils/CollectionUtils.java b/flutter-idea/src/io/flutter/utils/CollectionUtils.java index 66ede8d684..f476a974cd 100644 --- a/flutter-idea/src/io/flutter/utils/CollectionUtils.java +++ b/flutter-idea/src/io/flutter/utils/CollectionUtils.java @@ -14,7 +14,9 @@ import java.util.stream.Stream; public class CollectionUtils { + private CollectionUtils() { + throw new AssertionError("No instances."); } public static boolean anyMatch(@NotNull T[] in, @NotNull final Predicate predicate) { diff --git a/flutter-idea/src/io/flutter/utils/ElementIO.java b/flutter-idea/src/io/flutter/utils/ElementIO.java index 52ad866969..c529f2761c 100644 --- a/flutter-idea/src/io/flutter/utils/ElementIO.java +++ b/flutter-idea/src/io/flutter/utils/ElementIO.java @@ -16,6 +16,11 @@ * Utilities for reading and writing IntelliJ run configurations to and from the disk. */ public class ElementIO { + + private ElementIO() { + throw new AssertionError("No instances."); + } + public static void addOption(@NotNull Element element, @NotNull String name, @Nullable String value) { if (value == null) return; @@ -25,7 +30,7 @@ public static void addOption(@NotNull Element element, @NotNull String name, @Nu element.addContent(child); } - public static Map readOptions(Element element) { + public static Map readOptions(@NotNull Element element) { final Map result = new HashMap<>(); for (Element child : element.getChildren()) { if ("option".equals(child.getName())) { diff --git a/flutter-idea/src/io/flutter/utils/FileUtils.java b/flutter-idea/src/io/flutter/utils/FileUtils.java index 1dacb711ce..dc9bc3ba41 100644 --- a/flutter-idea/src/io/flutter/utils/FileUtils.java +++ b/flutter-idea/src/io/flutter/utils/FileUtils.java @@ -5,31 +5,27 @@ */ package io.flutter.utils; -import com.intellij.openapi.vfs.VirtualFile; import com.intellij.util.lang.UrlClassLoader; -import com.jetbrains.lang.dart.DartFileType; -import org.jetbrains.annotations.NotNull; import java.io.File; import java.nio.file.Path; import java.util.List; -public class FileUtils { - private static FileUtils fileUtils; +import org.jetbrains.annotations.NotNull; - public static FileUtils getInstance() { - if (fileUtils == null) { - fileUtils = new FileUtils(); - } - return fileUtils; +public class FileUtils { + + private FileUtils() { + throw new AssertionError("No instances."); } /** * Makes a directory at the provided path. + * * @param path path of the directory to be created. * @return true if the directory already existed, or if it was successfully created; false if the directory could not be created. */ - public boolean makeDirectory(String path) { + public static boolean makeDirectory(@NotNull String path) { final File directory = new File(path); if (!directory.exists()) { return directory.mkdirs(); @@ -37,36 +33,35 @@ public boolean makeDirectory(String path) { return true; } - public boolean fileExists(String path) { + public static boolean fileExists(@NotNull String path) { final File file = new File(path); return file.exists(); } /** * Deletes a file at the provided path. + * * @param path path of the file to be deleted. * @return true if the file does not exist, or if it was successfully deleted; false if the file could not be deleted. */ - public boolean deleteFile(String path) { + public static boolean deleteFile(@NotNull String path) { final File file = new File(path); if (file.exists()) { return file.delete(); } return true; } + /** * Loads a list of file paths with a class loader. - * + *

* This is only available for versions 211.4961.30 and later. - * @param classLoader classloader that can be used as a UrlClassLoader to load the files. - * @param paths list of file paths to load. + * + * @param classLoader classloader that can be used as an UrlClassLoader to load the files. + * @param paths list of file paths to load. */ - public void loadPaths(ClassLoader classLoader, List paths) { - final UrlClassLoader urlClassLoader = (UrlClassLoader) classLoader; + public static void loadPaths(@NotNull ClassLoader classLoader, @NotNull List paths) { + final UrlClassLoader urlClassLoader = (UrlClassLoader)classLoader; urlClassLoader.addFiles(paths); } - - public static boolean isDartFile(@NotNull VirtualFile file) { - return file.getFileType().equals(DartFileType.INSTANCE); - } } diff --git a/flutter-idea/src/io/flutter/utils/FlutterModuleUtils.java b/flutter-idea/src/io/flutter/utils/FlutterModuleUtils.java index 03cc3a207b..eab5f873fb 100644 --- a/flutter-idea/src/io/flutter/utils/FlutterModuleUtils.java +++ b/flutter-idea/src/io/flutter/utils/FlutterModuleUtils.java @@ -49,6 +49,7 @@ public class FlutterModuleUtils { public static final String DEPRECATED_FLUTTER_MODULE_TYPE_ID = "WEB_MODULE"; private FlutterModuleUtils() { + throw new AssertionError("No instances."); } /** diff --git a/flutter-idea/src/io/flutter/utils/JsonUtils.java b/flutter-idea/src/io/flutter/utils/JsonUtils.java index d328723901..580b5b239d 100644 --- a/flutter-idea/src/io/flutter/utils/JsonUtils.java +++ b/flutter-idea/src/io/flutter/utils/JsonUtils.java @@ -6,6 +6,7 @@ package io.flutter.utils; import com.google.gson.*; + import java.util.HashSet; import java.util.Map; import java.util.Set; @@ -18,6 +19,7 @@ public class JsonUtils { private JsonUtils() { + throw new AssertionError("No instances."); } @Nullable diff --git a/flutter-idea/src/io/flutter/utils/OpenApiUtils.java b/flutter-idea/src/io/flutter/utils/OpenApiUtils.java index 9212ce473a..d9f901d82c 100644 --- a/flutter-idea/src/io/flutter/utils/OpenApiUtils.java +++ b/flutter-idea/src/io/flutter/utils/OpenApiUtils.java @@ -23,6 +23,10 @@ public class OpenApiUtils { + private OpenApiUtils() { + throw new AssertionError("No instances."); + } + public static @NotNull VirtualFile @NotNull [] getContentRoots(@NotNull Module module) { var moduleRootManager = ModuleRootManager.getInstance(module); return moduleRootManager == null ? VirtualFile.EMPTY_ARRAY : moduleRootManager.getContentRoots(); diff --git a/flutter-idea/src/io/flutter/utils/SystemUtils.java b/flutter-idea/src/io/flutter/utils/SystemUtils.java index f01eb0711c..92ac77d2a5 100644 --- a/flutter-idea/src/io/flutter/utils/SystemUtils.java +++ b/flutter-idea/src/io/flutter/utils/SystemUtils.java @@ -12,6 +12,7 @@ import com.intellij.execution.util.ExecUtil; import com.intellij.openapi.util.SystemInfo; import com.intellij.util.concurrency.AppExecutorUtil; +import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.io.File; @@ -19,6 +20,10 @@ public class SystemUtils { + private SystemUtils() { + throw new AssertionError("No instances."); + } + /** * Locate a given command-line tool given its name. *

@@ -26,7 +31,7 @@ public class SystemUtils { * binaries it will require more work, especially on Windows. */ @Nullable - public static String which(String toolName) { + public static String which(@NotNull String toolName) { final File gitExecutableFromPath = PathEnvironmentVariableUtil.findInPath(SystemInfo.isWindows ? toolName + ".exe" : toolName, getPath(), null); if (gitExecutableFromPath != null) { @@ -45,7 +50,7 @@ private static String getPath() { *

* This is a non-blocking equivalient to {@link ExecUtil#execAndGetOutput(GeneralCommandLine)}. */ - public static CompletableFuture execAndGetOutput(GeneralCommandLine cmd) { + public static CompletableFuture execAndGetOutput(@NotNull GeneralCommandLine cmd) { final CompletableFuture future = new CompletableFuture<>(); AppExecutorUtil.getAppExecutorService().submit(() -> { diff --git a/flutter-idea/src/io/flutter/utils/UIUtils.java b/flutter-idea/src/io/flutter/utils/UIUtils.java index 8b823a791d..dc83b61895 100644 --- a/flutter-idea/src/io/flutter/utils/UIUtils.java +++ b/flutter-idea/src/io/flutter/utils/UIUtils.java @@ -10,6 +10,11 @@ import org.jetbrains.annotations.NotNull; public class UIUtils { + + private UIUtils() { + throw new AssertionError("No instances."); + } + /** * All editor notifications in the Flutter plugin should get and set the background color from this method, which will ensure if any are * changed, they are all changed. diff --git a/flutter-idea/src/io/flutter/utils/UrlUtils.java b/flutter-idea/src/io/flutter/utils/UrlUtils.java index 10f55791dd..d93dd98ad5 100644 --- a/flutter-idea/src/io/flutter/utils/UrlUtils.java +++ b/flutter-idea/src/io/flutter/utils/UrlUtils.java @@ -4,19 +4,25 @@ import java.net.URL; public class UrlUtils { - public static String generateHtmlFragmentWithHrefTags(String input) { - StringBuilder builder = new StringBuilder(); - for (String token : input.split(" ")) { - if (!builder.isEmpty()) { - builder.append(" "); - } - try { - URL url = new URL(token); - builder.append("").append(url).append(""); - } catch(MalformedURLException e) { - builder.append(token); - } - } - return builder.toString(); + + private UrlUtils() { + throw new AssertionError("No instances."); + } + + public static String generateHtmlFragmentWithHrefTags(String input) { + StringBuilder builder = new StringBuilder(); + for (String token : input.split(" ")) { + if (!builder.isEmpty()) { + builder.append(" "); + } + try { + URL url = new URL(token); + builder.append("").append(url).append(""); + } + catch (MalformedURLException e) { + builder.append(token); + } } + return builder.toString(); + } } diff --git a/flutter-idea/testSrc/unit/io/flutter/jxbrowser/JxBrowserManagerTest.java b/flutter-idea/testSrc/unit/io/flutter/jxbrowser/JxBrowserManagerTest.java index 1434afe1e4..a1b1beb4e4 100644 --- a/flutter-idea/testSrc/unit/io/flutter/jxbrowser/JxBrowserManagerTest.java +++ b/flutter-idea/testSrc/unit/io/flutter/jxbrowser/JxBrowserManagerTest.java @@ -34,7 +34,7 @@ public void testSetUpIfKeyNotFound() throws FileNotFoundException { when(mockUtils.getJxBrowserKey()).thenThrow(new FileNotFoundException("Key not found")); // If the directory for JxBrowser files cannot be created, the installation should fail. - final JxBrowserManager manager = new JxBrowserManager(mockUtils, mock(FileUtils.class)); + final JxBrowserManager manager = new JxBrowserManager(mockUtils); manager.setUp(projectName); Assert.assertEquals(JxBrowserStatus.INSTALLATION_FAILED, manager.getStatus()); @@ -49,7 +49,7 @@ public void testSetUpIfDirectoryFails() throws FileNotFoundException { when(mockFileUtils.makeDirectory(DOWNLOAD_PATH)).thenReturn(false); // If the directory for JxBrowser files cannot be created, the installation should fail. - final JxBrowserManager manager = new JxBrowserManager(mockUtils, mockFileUtils); + final JxBrowserManager manager = new JxBrowserManager(mockUtils); manager.setUp(projectName); Assert.assertEquals(JxBrowserStatus.INSTALLATION_FAILED, manager.getStatus()); @@ -65,7 +65,7 @@ public void testSetUpIfPlatformFileNotFound() throws FileNotFoundException { when(mockFileUtils.makeDirectory(DOWNLOAD_PATH)).thenReturn(true); // If the system platform is not found among JxBrowser files, then the installation should fail. - final JxBrowserManager manager = new JxBrowserManager(mockUtils, mockFileUtils); + final JxBrowserManager manager = new JxBrowserManager(mockUtils); manager.setUp(projectName); Assert.assertEquals(JxBrowserStatus.INSTALLATION_FAILED, manager.getStatus()); @@ -84,7 +84,7 @@ public void testSetUpIfAllFilesExist() throws FileNotFoundException { when(mockFileUtils.fileExists(anyString())).thenReturn(true); // If all of the files are already downloaded, we should load the existing files. - final JxBrowserManager manager = new JxBrowserManager(mockUtils, mockFileUtils); + final JxBrowserManager manager = new JxBrowserManager(mockUtils); manager.setUp(projectName); final String[] expectedFileNames = {PLATFORM_FILE_NAME, API_FILE_NAME, SWING_FILE_NAME}; @@ -108,7 +108,7 @@ public void testSetUpIfFilesMissing() throws FileNotFoundException { when(mockFileUtils.deleteFile(anyString())).thenReturn(true); // If any of our required files do not exist, we want to delete any existing files and start a download of all of the required files. - final JxBrowserManager manager = new JxBrowserManager(mockUtils, mockFileUtils); + final JxBrowserManager manager = new JxBrowserManager(mockUtils); final JxBrowserManager spy = spy(manager); final String[] expectedFileNames = {PLATFORM_FILE_NAME, API_FILE_NAME, SWING_FILE_NAME}; doNothing().when(spy).downloadJxBrowser(expectedFileNames);