mirror of
https://github.com/Card-Forge/forge.git
synced 2025-11-20 20:58:03 +00:00
Fix parsing of Java version strings
This commit is contained in:
1
.gitattributes
vendored
1
.gitattributes
vendored
@@ -268,6 +268,7 @@ forge-core/src/main/java/forge/util/MyRandom.java svneol=native#text/plain
|
||||
forge-core/src/main/java/forge/util/NameGenerator.java -text
|
||||
forge-core/src/main/java/forge/util/PredicateString.java -text
|
||||
forge-core/src/main/java/forge/util/ReflectionUtil.java -text
|
||||
forge-core/src/main/java/forge/util/RuntimeVersion.java -text
|
||||
forge-core/src/main/java/forge/util/Settable.java -text
|
||||
forge-core/src/main/java/forge/util/TextUtil.java -text
|
||||
forge-core/src/main/java/forge/util/ThreadUtil.java -text
|
||||
|
||||
124
forge-core/src/main/java/forge/util/RuntimeVersion.java
Normal file
124
forge-core/src/main/java/forge/util/RuntimeVersion.java
Normal file
@@ -0,0 +1,124 @@
|
||||
package forge.util;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class RuntimeVersion {
|
||||
|
||||
private static Pattern versionNumberPattern = Pattern.compile("([1-9][0-9]*((\\.0)*\\.[1-9][0-9]*)*)");
|
||||
private static Pattern preReleasePattern = Pattern.compile("([a-zA-Z0-9]+)");
|
||||
private static Pattern buildNumberPattern = Pattern.compile("(0|[1-9][0-9]*)");
|
||||
private static Pattern buildInformationPattern = Pattern.compile("([-a-zA-Z0-9.]+)");
|
||||
|
||||
private static Pattern versionStringPattern1 = Pattern.compile(versionNumberPattern + "(-" + preReleasePattern + ")?\\+" + buildNumberPattern + "(-" + buildInformationPattern + ")?");
|
||||
private static Pattern versionStringPattern2 = Pattern.compile(versionNumberPattern + "-" + preReleasePattern + "(-" + buildInformationPattern + ")?");
|
||||
private static Pattern versionStringPattern3 = Pattern.compile(versionNumberPattern + "(+-" + buildInformationPattern + ")?");
|
||||
private static Pattern versionStringPattern4 = Pattern.compile(versionNumberPattern + "(-" + preReleasePattern + ")?");
|
||||
|
||||
private int major;
|
||||
private int minor;
|
||||
private int securityLevel;
|
||||
|
||||
private String preReleaseIdentifier;
|
||||
private int buildNumber;
|
||||
private String buildInformation;
|
||||
|
||||
@SuppressWarnings("ResultOfMethodCallIgnored")
|
||||
private RuntimeVersion(final String versionString) {
|
||||
|
||||
Matcher matcher = versionNumberPattern.matcher(versionString);
|
||||
|
||||
if (!matcher.find()) {
|
||||
throw new IllegalArgumentException("Improperly formatted version string provided: " + versionString);
|
||||
}
|
||||
|
||||
String[] versionNumbers = matcher.group().split("\\.");
|
||||
|
||||
if (versionNumbers.length >= 1) {
|
||||
major = Integer.parseInt(versionNumbers[0]);
|
||||
}
|
||||
|
||||
if (versionNumbers.length >= 2) {
|
||||
minor = Integer.parseInt(versionNumbers[1]);
|
||||
}
|
||||
|
||||
if (versionNumbers.length >= 3) {
|
||||
securityLevel = Integer.parseInt(versionNumbers[2]);
|
||||
}
|
||||
|
||||
if (versionStringPattern1.matcher(versionString).find()) {
|
||||
|
||||
Matcher infoMatcher = preReleasePattern.matcher(versionString);
|
||||
if (infoMatcher.find()) {
|
||||
preReleaseIdentifier = infoMatcher.group();
|
||||
}
|
||||
|
||||
infoMatcher = buildNumberPattern.matcher(versionString);
|
||||
infoMatcher.find();
|
||||
buildNumber = Integer.parseInt(infoMatcher.group());
|
||||
|
||||
infoMatcher = buildInformationPattern.matcher(versionString);
|
||||
if (infoMatcher.find()) {
|
||||
buildInformation = infoMatcher.group();
|
||||
}
|
||||
|
||||
} else if (versionStringPattern2.matcher(versionString).find()) {
|
||||
|
||||
Matcher infoMatcher = preReleasePattern.matcher(versionString);
|
||||
infoMatcher.find();
|
||||
preReleaseIdentifier = infoMatcher.group();
|
||||
|
||||
infoMatcher = buildInformationPattern.matcher(versionString);
|
||||
if (infoMatcher.find()) {
|
||||
buildInformation = infoMatcher.group();
|
||||
}
|
||||
|
||||
} else if (versionStringPattern3.matcher(versionString).find()) {
|
||||
|
||||
Matcher infoMatcher = buildInformationPattern.matcher(versionString);
|
||||
if (infoMatcher.find()) {
|
||||
buildInformation = infoMatcher.group();
|
||||
}
|
||||
|
||||
} else if (versionStringPattern4.matcher(versionString).find()) {
|
||||
|
||||
Matcher infoMatcher = preReleasePattern.matcher(versionString);
|
||||
if (infoMatcher.find()) {
|
||||
preReleaseIdentifier = infoMatcher.group();
|
||||
}
|
||||
|
||||
} else {
|
||||
throw new IllegalArgumentException("Improperly formatted version string provided: " + versionString);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static RuntimeVersion of(final String versionString) {
|
||||
return new RuntimeVersion(versionString);
|
||||
}
|
||||
|
||||
public int getMajor() {
|
||||
return major;
|
||||
}
|
||||
|
||||
public int getMinor() {
|
||||
return minor;
|
||||
}
|
||||
|
||||
public int getSecurityLevel() {
|
||||
return securityLevel;
|
||||
}
|
||||
|
||||
public String getPreReleaseIdentifier() {
|
||||
return preReleaseIdentifier;
|
||||
}
|
||||
|
||||
public int getBuildNumber() {
|
||||
return buildNumber;
|
||||
}
|
||||
|
||||
public String getBuildInformation() {
|
||||
return buildInformation;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -12,6 +12,7 @@ import forge.screens.home.IVSubmenu;
|
||||
import forge.screens.home.VHomeUI;
|
||||
import forge.toolbox.*;
|
||||
import forge.util.FileUtil;
|
||||
import forge.util.RuntimeVersion;
|
||||
import net.miginfocom.swing.MigLayout;
|
||||
|
||||
import javax.swing.*;
|
||||
@@ -107,12 +108,9 @@ public enum VSubmenuDownloaders implements IVSubmenu<CSubmenuDownloaders> {
|
||||
|
||||
private boolean javaRecentEnough() {
|
||||
|
||||
String fullJavaVersion = System.getProperty("java.version");
|
||||
RuntimeVersion javaVersion = RuntimeVersion.of(System.getProperty("java.version"));
|
||||
|
||||
int majorVersion = Integer.parseInt(fullJavaVersion.split("_")[0].split("\\.")[1]);
|
||||
int minorVersion = Integer.parseInt(fullJavaVersion.split("_")[1]);
|
||||
|
||||
return majorVersion > 8 || (majorVersion == 8 && minorVersion >= 101);
|
||||
return javaVersion.getMajor() > 8 || (javaVersion.getMajor() == 8 && javaVersion.getSecurityLevel() >= 101);
|
||||
|
||||
}
|
||||
|
||||
@@ -274,4 +272,5 @@ public enum VSubmenuDownloaders implements IVSubmenu<CSubmenuDownloaders> {
|
||||
return new FLabel.Builder().fontAlign(SwingConstants.CENTER)
|
||||
.text(label).fontStyle(Font.ITALIC).build();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user