Skip to content

Commit

Permalink
Initialization timing issue for static variable (#1671)
Browse files Browse the repository at this point in the history
* Initialization timing issue for static variable

currentPlatform variable initialized in static block of this class.
But it's also accessed via constructor for validate unsupported environment.
This variable possible not initialized yet when access make a instance use constructor.
I'm already met this kind of issue when I run unit test use this library.
I think one more variable init checking required, at least If constructor want to check unsupported or not.

* Initialize static variable from static method instead of static block

Co-authored-by: sam <sam@jennifersoft.com>
  • Loading branch information
KyongSik-Yoon and sam committed Jul 9, 2021
1 parent d6e49c4 commit 455185c
Showing 1 changed file with 10 additions and 10 deletions.
20 changes: 10 additions & 10 deletions oshi-core/src/main/java/oshi/SystemInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,25 +66,25 @@ public class SystemInfo {

// The platform isn't going to change, and making this static enables easy
// access from outside this class
private static final PlatformEnum currentPlatform;
private static final PlatformEnum currentPlatform = queryCurrentPlatform();

static {
private static PlatformEnum queryCurrentPlatform() {
if (Platform.isWindows()) {
currentPlatform = WINDOWS;
return WINDOWS;
} else if (Platform.isLinux()) {
currentPlatform = LINUX;
return LINUX;
} else if (Platform.isMac()) {
currentPlatform = MACOS;
return MACOS;
} else if (Platform.isSolaris()) {
currentPlatform = SOLARIS;
return SOLARIS;
} else if (Platform.isFreeBSD()) {
currentPlatform = FREEBSD;
return FREEBSD;
} else if (Platform.isAIX()) {
currentPlatform = AIX;
return AIX;
} else if (Platform.isOpenBSD()) {
currentPlatform = OPENBSD;
return OPENBSD;
} else {
currentPlatform = UNKNOWN;
return UNKNOWN;
}
}

Expand Down

0 comments on commit 455185c

Please sign in to comment.