1
1
/*
2
- * Copyright 2020 DiffPlug
2
+ * Copyright (C) 2020-2021 DiffPlug
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
23
23
import java .nio .charset .StandardCharsets ;
24
24
import java .util .Arrays ;
25
25
import java .util .Locale ;
26
+ import java .util .function .Function ;
26
27
27
28
/** Enum representing an OS and its underlying CPU architecture. */
28
29
public enum OS {
@@ -96,29 +97,45 @@ public String toSwt() {
96
97
97
98
/** Returns the native OS: 32-bit JVM on 64-bit Windows returns OS.WIN_64. */
98
99
public static OS getNative () {
100
+ detectPlatform ();
99
101
return NATIVE_OS ;
100
102
}
101
103
102
104
/** Returns the running OS: 32-bit JVM on 64-bit Windows returns OS.WIN_32. */
103
105
public static OS getRunning () {
106
+ detectPlatform ();
104
107
return RUNNING_OS ;
105
108
}
106
109
107
- private static final OS NATIVE_OS = calculateNative ();
110
+ /** Eagerly detects the native and running JVM properties. */
111
+ public static void detectPlatform (Function <String , String > systemProperty , Function <String , String > environmentVariable ) {
112
+ if (NATIVE_OS == null ) {
113
+ NATIVE_OS = calculateNative (systemProperty , environmentVariable );
114
+ RUNNING_OS = calculateRunning (systemProperty );
115
+ }
116
+ }
117
+
118
+ private static void detectPlatform () {
119
+ if (NATIVE_OS == null ) {
120
+ detectPlatform (System ::getProperty , System ::getenv );
121
+ }
122
+ }
123
+
124
+ private static OS NATIVE_OS ;
108
125
109
126
/** Calculates the native OS. */
110
- private static OS calculateNative () {
111
- String os_name = System . getProperty ("os.name" ).toLowerCase (Locale .getDefault ());
127
+ private static OS calculateNative (Function < String , String > systemProperty , Function < String , String > environmentVariable ) {
128
+ String os_name = systemProperty . apply ("os.name" ).toLowerCase (Locale .getDefault ());
112
129
boolean isWin = os_name .contains ("win" );
113
130
boolean isMac = os_name .contains ("mac" );
114
131
boolean isLinux = Arrays .asList ("nix" , "nux" , "aix" ).stream ().anyMatch (os_name ::contains );
115
132
if (isMac ) {
116
133
return exec ("uname" , "-a" ).contains ("_ARM64_" ) ? MAC_silicon : MAC_x64 ;
117
134
} else if (isWin ) {
118
- boolean is64bit = System . getenv ("ProgramFiles(x86)" ) != null ;
135
+ boolean is64bit = environmentVariable . apply ("ProgramFiles(x86)" ) != null ;
119
136
return is64bit ? WIN_x64 : WIN_x86 ;
120
137
} else if (isLinux ) {
121
- String os_arch = System . getProperty ("os.arch" );
138
+ String os_arch = systemProperty . apply ("os.arch" );
122
139
switch (os_arch ) {
123
140
case "i386" :
124
141
case "x86" :
@@ -153,11 +170,11 @@ private static void drain(InputStream input, OutputStream output) throws IOExcep
153
170
}
154
171
}
155
172
156
- private static final OS RUNNING_OS = calculateRunning () ;
173
+ private static OS RUNNING_OS ;
157
174
158
175
/** Calculates the running OS. */
159
- private static OS calculateRunning () {
160
- Arch runningArch = runningJvm ();
176
+ private static OS calculateRunning (Function < String , String > systemProperty ) {
177
+ Arch runningArch = runningJvm (systemProperty );
161
178
OS runningOs = NATIVE_OS .winMacLinux (
162
179
runningArch .x86x64arm64 (OS .WIN_x86 , OS .WIN_x64 , null ),
163
180
runningArch .x86x64arm64 (null , OS .MAC_x64 , OS .MAC_silicon ),
@@ -169,13 +186,13 @@ private static OS calculateRunning() {
169
186
}
170
187
171
188
/** Returns the arch of the currently running JVM. */
172
- private static Arch runningJvm () {
173
- String sunArchDataModel = System . getProperty ("sun.arch.data.model" );
189
+ private static Arch runningJvm (Function < String , String > systemProperty ) {
190
+ String sunArchDataModel = systemProperty . apply ("sun.arch.data.model" );
174
191
switch (sunArchDataModel ) {
175
192
case "32" :
176
193
return Arch .x86 ;
177
194
case "64" :
178
- return "aarch64" .equals (System . getProperty ("os.arch" )) ? Arch .arm64 : Arch .x64 ;
195
+ return "aarch64" .equals (systemProperty . apply ("os.arch" )) ? Arch .arm64 : Arch .x64 ;
179
196
default :
180
197
throw new IllegalArgumentException ("Expcted 32 or 64, was " + sunArchDataModel );
181
198
}
0 commit comments