From 37251d15a105700a126174c182dbc2460b1700d2 Mon Sep 17 00:00:00 2001 From: dima_dencep Date: Sat, 24 Sep 2022 00:31:45 +0700 Subject: [PATCH] idea cleanup launchercore --- .../java/pro/gravit/utils/Downloader.java | 1 + .../utils/command/JLineCommandHandler.java | 2 +- .../pro/gravit/utils/helper/CommonHelper.java | 2 - .../pro/gravit/utils/helper/IOHelper.java | 2 +- .../pro/gravit/utils/helper/JVMHelper.java | 44 +++++++------------ .../pro/gravit/utils/helper/JavaHelper.java | 15 ++++--- .../java11/pro/gravit/utils/Downloader.java | 1 + .../pro/gravit/utils/helper/JVMHelper.java | 44 +++++++------------ 8 files changed, 46 insertions(+), 65 deletions(-) diff --git a/LauncherCore/src/main/java/pro/gravit/utils/Downloader.java b/LauncherCore/src/main/java/pro/gravit/utils/Downloader.java index b36da07f..fb9f486c 100644 --- a/LauncherCore/src/main/java/pro/gravit/utils/Downloader.java +++ b/LauncherCore/src/main/java/pro/gravit/utils/Downloader.java @@ -12,6 +12,7 @@ public class Downloader { private final CompletableFuture future; private final AsyncDownloader asyncDownloader; + private Downloader(CompletableFuture future, AsyncDownloader downloader) { this.future = future; this.asyncDownloader = downloader; diff --git a/LauncherCore/src/main/java/pro/gravit/utils/command/JLineCommandHandler.java b/LauncherCore/src/main/java/pro/gravit/utils/command/JLineCommandHandler.java index e8a48d0b..ce833455 100644 --- a/LauncherCore/src/main/java/pro/gravit/utils/command/JLineCommandHandler.java +++ b/LauncherCore/src/main/java/pro/gravit/utils/command/JLineCommandHandler.java @@ -77,7 +77,7 @@ public void complete(LineReader reader, ParsedLine line, List candida }); } else { Command target = findCommand(line.words().get(0)); - if(target == null) { + if (target == null) { return; } List words = line.words(); diff --git a/LauncherCore/src/main/java/pro/gravit/utils/helper/CommonHelper.java b/LauncherCore/src/main/java/pro/gravit/utils/helper/CommonHelper.java index 43b6db98..ccaeb8ea 100644 --- a/LauncherCore/src/main/java/pro/gravit/utils/helper/CommonHelper.java +++ b/LauncherCore/src/main/java/pro/gravit/utils/helper/CommonHelper.java @@ -4,8 +4,6 @@ import pro.gravit.utils.command.CommandException; import javax.script.ScriptEngine; -import javax.script.ScriptEngineFactory; -import javax.script.ScriptEngineManager; import java.lang.reflect.Type; import java.util.Base64; import java.util.Collection; diff --git a/LauncherCore/src/main/java/pro/gravit/utils/helper/IOHelper.java b/LauncherCore/src/main/java/pro/gravit/utils/helper/IOHelper.java index e74afed9..aed442fb 100644 --- a/LauncherCore/src/main/java/pro/gravit/utils/helper/IOHelper.java +++ b/LauncherCore/src/main/java/pro/gravit/utils/helper/IOHelper.java @@ -41,6 +41,7 @@ public final class IOHelper { public static final FileSystem FS = FileSystems.getDefault(); // Platform-dependent public static final String PLATFORM_SEPARATOR = FS.getSeparator(); + private static final Pattern PLATFORM_SEPARATOR_PATTERN = Pattern.compile(PLATFORM_SEPARATOR, Pattern.LITERAL); public static final boolean POSIX = FS.supportedFileAttributeViews().contains("posix") || FS.supportedFileAttributeViews().contains("Posix"); public static final Path JVM_DIR = Paths.get(System.getProperty("java.home")); public static final Path HOME_DIR = Paths.get(System.getProperty("user.home")); @@ -56,7 +57,6 @@ public final class IOHelper { private static final Set WALK_OPTIONS = Collections.singleton(FileVisitOption.FOLLOW_LINKS); // Other constants private static final Pattern CROSS_SEPARATOR_PATTERN = Pattern.compile(CROSS_SEPARATOR, Pattern.LITERAL); - private static final Pattern PLATFORM_SEPARATOR_PATTERN = Pattern.compile(PLATFORM_SEPARATOR, Pattern.LITERAL); private IOHelper() { } diff --git a/LauncherCore/src/main/java/pro/gravit/utils/helper/JVMHelper.java b/LauncherCore/src/main/java/pro/gravit/utils/helper/JVMHelper.java index ac161aa9..43acfbe7 100644 --- a/LauncherCore/src/main/java/pro/gravit/utils/helper/JVMHelper.java +++ b/LauncherCore/src/main/java/pro/gravit/utils/helper/JVMHelper.java @@ -20,12 +20,10 @@ public final class JVMHelper { public static final OperatingSystemMXBean OPERATING_SYSTEM_MXBEAN = ManagementFactory.getOperatingSystemMXBean(); public static final OS OS_TYPE = OS.byName(OPERATING_SYSTEM_MXBEAN.getName()); - // System properties - public static final String OS_VERSION = OPERATING_SYSTEM_MXBEAN.getVersion(); - @Deprecated public static final int OS_BITS = getCorrectOSArch(); - + // System properties + public static final String OS_VERSION = OPERATING_SYSTEM_MXBEAN.getVersion(); public static final ARCH ARCH_TYPE = getArch(System.getProperty("os.arch")); public static final int JVM_BITS = Integer.parseInt(System.getProperty("sun.arch.data.model")); public static final SecurityManager SECURITY_MANAGER = System.getSecurityManager(); @@ -46,21 +44,11 @@ public final class JVMHelper { private JVMHelper() { } - public enum ARCH { - X86("x86"), X86_64("x86-64"), ARM64("arm64"), ARM32("arm32"); - - public final String name; - - ARCH(String name) { - this.name = name; - } - } - public static ARCH getArch(String arch) { - if(arch.equals("amd64") || arch.equals("x86-64") || arch.equals("x86_64")) return ARCH.X86_64; - if(arch.equals("i386") || arch.equals("i686") || arch.equals("x86")) return ARCH.X86; - if(arch.startsWith("armv8") || arch.startsWith("aarch64")) return ARCH.ARM64; - if(arch.startsWith("arm") || arch.startsWith("aarch32")) return ARCH.ARM32; + if (arch.equals("amd64") || arch.equals("x86-64") || arch.equals("x86_64")) return ARCH.X86_64; + if (arch.equals("i386") || arch.equals("i686") || arch.equals("x86")) return ARCH.X86; + if (arch.startsWith("armv8") || arch.startsWith("aarch64")) return ARCH.ARM64; + if (arch.startsWith("arm") || arch.startsWith("aarch32")) return ARCH.ARM32; throw new InternalError(String.format("Unsupported arch '%s'", arch)); } @@ -110,19 +98,16 @@ public static Class firstClass(String... names) throws ClassNotFoundException throw new ClassNotFoundException(Arrays.toString(names)); } - public static void fullGC() { RUNTIME.gc(); RUNTIME.runFinalization(); LogHelper.debug("Used heap: %d MiB", RUNTIME.totalMemory() - RUNTIME.freeMemory() >> 20); } - public static String[] getClassPath() { return System.getProperty("java.class.path").split(File.pathSeparator); } - public static URL[] getClassPathURL() { String[] cp = System.getProperty("java.class.path").split(File.pathSeparator); URL[] list = new URL[cp.length]; @@ -164,35 +149,29 @@ private static int getCorrectOSArch() { return System.getProperty("os.arch").contains("64") ? 64 : 32; } - public static String getEnvPropertyCaseSensitive(String name) { return System.getenv().get(name); } - @Deprecated public static boolean isJVMMatchesSystemArch() { return JVM_BITS == OS_BITS; } - public static String jvmProperty(String name, String value) { return String.format("-D%s=%s", name, value); } - public static String systemToJvmProperty(String name) { return String.format("-D%s=%s", name, System.getProperties().getProperty(name)); } - public static void addSystemPropertyToArgs(Collection args, String name) { String property = System.getProperty(name); if (property != null) args.add(String.format("-D%s=%s", name, property)); } - public static void verifySystemProperties(Class mainClass, boolean requireSystem) { Locale.setDefault(Locale.US); // Verify class loader @@ -204,6 +183,17 @@ public static void verifySystemProperties(Class mainClass, boolean requireSys LogHelper.debug("Verifying JVM architecture"); } + + public enum ARCH { + X86("x86"), X86_64("x86-64"), ARM64("arm64"), ARM32("arm32"); + + public final String name; + + ARCH(String name) { + this.name = name; + } + } + public enum OS { MUSTDIE("mustdie"), LINUX("linux"), MACOSX("macosx"); diff --git a/LauncherCore/src/main/java/pro/gravit/utils/helper/JavaHelper.java b/LauncherCore/src/main/java/pro/gravit/utils/helper/JavaHelper.java index 8676f82e..73be2837 100644 --- a/LauncherCore/src/main/java/pro/gravit/utils/helper/JavaHelper.java +++ b/LauncherCore/src/main/java/pro/gravit/utils/helper/JavaHelper.java @@ -12,6 +12,7 @@ public class JavaHelper { private static List javaVersionsCache; + public static Path tryGetOpenJFXPath(Path jvmDir) { String dirName = jvmDir.getFileName().toString(); Path parent = jvmDir.getParent(); @@ -57,7 +58,7 @@ public static boolean tryAddModule(List paths, String moduleName, StringBu } public synchronized static List findJava() { - if(javaVersionsCache != null) { + if (javaVersionsCache != null) { return javaVersionsCache; } List javaPaths = new ArrayList<>(4); @@ -106,11 +107,11 @@ public synchronized static List findJava() { } private static JavaVersion tryFindJavaByPath(Path path) { - if(javaVersionsCache == null) { + if (javaVersionsCache == null) { return null; } - for(JavaVersion version : javaVersionsCache) { - if(version.jvmDir.equals(path)) { + for (JavaVersion version : javaVersionsCache) { + if (version.jvmDir.equals(path)) { return version; } } @@ -156,8 +157,8 @@ public static JavaVersionAndBuild getJavaVersion(String version) { result.build = Integer.parseInt(version.substring(dot + 1)); } else { try { - if(version.endsWith("-ea")) { - version = version.substring(0, version.length()-3); + if (version.endsWith("-ea")) { + version = version.substring(0, version.length() - 3); } result.version = Integer.parseInt(version); result.build = 0; @@ -232,7 +233,7 @@ private static boolean isCurrentJavaSupportJavaFX() { public static JavaVersion getByPath(Path jvmDir) throws IOException { { JavaVersion version = JavaHelper.tryFindJavaByPath(jvmDir); - if(version != null) { + if (version != null) { return version; } } diff --git a/LauncherCore/src/main/java11/pro/gravit/utils/Downloader.java b/LauncherCore/src/main/java11/pro/gravit/utils/Downloader.java index 7f3445cf..b57e765f 100644 --- a/LauncherCore/src/main/java11/pro/gravit/utils/Downloader.java +++ b/LauncherCore/src/main/java11/pro/gravit/utils/Downloader.java @@ -30,6 +30,7 @@ public class Downloader { protected final ExecutorService executor; protected final LinkedList tasks = new LinkedList<>(); protected CompletableFuture future; + protected Downloader(HttpClient client, ExecutorService executor) { this.client = client; this.executor = executor; diff --git a/LauncherCore/src/main/java11/pro/gravit/utils/helper/JVMHelper.java b/LauncherCore/src/main/java11/pro/gravit/utils/helper/JVMHelper.java index 756e7fc3..63babbfb 100644 --- a/LauncherCore/src/main/java11/pro/gravit/utils/helper/JVMHelper.java +++ b/LauncherCore/src/main/java11/pro/gravit/utils/helper/JVMHelper.java @@ -20,12 +20,10 @@ public final class JVMHelper { public static final OperatingSystemMXBean OPERATING_SYSTEM_MXBEAN = ManagementFactory.getOperatingSystemMXBean(); public static final OS OS_TYPE = OS.byName(OPERATING_SYSTEM_MXBEAN.getName()); - // System properties - public static final String OS_VERSION = OPERATING_SYSTEM_MXBEAN.getVersion(); - @Deprecated public static final int OS_BITS = getCorrectOSArch(); - + // System properties + public static final String OS_VERSION = OPERATING_SYSTEM_MXBEAN.getVersion(); public static final ARCH ARCH_TYPE = getArch(System.getProperty("os.arch")); public static final int JVM_BITS = Integer.parseInt(System.getProperty("sun.arch.data.model")); @@ -46,21 +44,11 @@ public final class JVMHelper { private JVMHelper() { } - public enum ARCH { - X86("x86"), X86_64("x86-64"), ARM64("arm64"), ARM32("arm32"); - - public final String name; - - ARCH(String name) { - this.name = name; - } - } - public static ARCH getArch(String arch) { - if(arch.equals("amd64") || arch.equals("x86-64") || arch.equals("x86_64")) return ARCH.X86_64; - if(arch.equals("i386") || arch.equals("i686") || arch.equals("x86")) return ARCH.X86; - if(arch.startsWith("armv8") || arch.startsWith("aarch64")) return ARCH.ARM64; - if(arch.startsWith("arm") || arch.startsWith("aarch32")) return ARCH.ARM32; + if (arch.equals("amd64") || arch.equals("x86-64") || arch.equals("x86_64")) return ARCH.X86_64; + if (arch.equals("i386") || arch.equals("i686") || arch.equals("x86")) return ARCH.X86; + if (arch.startsWith("armv8") || arch.startsWith("aarch64")) return ARCH.ARM64; + if (arch.startsWith("arm") || arch.startsWith("aarch32")) return ARCH.ARM32; throw new InternalError(String.format("Unsupported arch '%s'", arch)); } @@ -87,19 +75,16 @@ public static Class firstClass(String... names) throws ClassNotFoundException throw new ClassNotFoundException(Arrays.toString(names)); } - public static void fullGC() { RUNTIME.gc(); RUNTIME.runFinalization(); LogHelper.debug("Used heap: %d MiB", RUNTIME.totalMemory() - RUNTIME.freeMemory() >> 20); } - public static String[] getClassPath() { return System.getProperty("java.class.path").split(File.pathSeparator); } - public static URL[] getClassPathURL() { String[] cp = System.getProperty("java.class.path").split(File.pathSeparator); URL[] list = new URL[cp.length]; @@ -141,35 +126,29 @@ private static int getCorrectOSArch() { return System.getProperty("os.arch").contains("64") ? 64 : 32; } - public static String getEnvPropertyCaseSensitive(String name) { return System.getenv().get(name); } - @Deprecated public static boolean isJVMMatchesSystemArch() { return JVM_BITS == OS_BITS; } - public static String jvmProperty(String name, String value) { return String.format("-D%s=%s", name, value); } - public static String systemToJvmProperty(String name) { return String.format("-D%s=%s", name, System.getProperties().getProperty(name)); } - public static void addSystemPropertyToArgs(Collection args, String name) { String property = System.getProperty(name); if (property != null) args.add(String.format("-D%s=%s", name, property)); } - public static void verifySystemProperties(Class mainClass, boolean requireSystem) { Locale.setDefault(Locale.US); // Verify class loader @@ -181,6 +160,17 @@ public static void verifySystemProperties(Class mainClass, boolean requireSys LogHelper.debug("Verifying JVM architecture"); } + + public enum ARCH { + X86("x86"), X86_64("x86-64"), ARM64("arm64"), ARM32("arm32"); + + public final String name; + + ARCH(String name) { + this.name = name; + } + } + public enum OS { MUSTDIE("mustdie"), LINUX("linux"), MACOSX("macosx");