Skip to content

Commit

Permalink
Support JDK10+ version-string format
Browse files Browse the repository at this point in the history
As in https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Runtime.Version.html
Also add fix from puniverse#130

Change-Id: Id166fa885c45248d3baa03c4faec78c1156fbaa9
  • Loading branch information
Benoit Desire committed Dec 15, 2020
1 parent 9e2fe6e commit a9a6b2c
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 14 deletions.
34 changes: 20 additions & 14 deletions capsule/src/main/java/Capsule.java
Original file line number Diff line number Diff line change
Expand Up @@ -4278,7 +4278,7 @@ private static Path getJavaExecutable0(Path javaHome) {
return javaHome.resolve("bin").resolve(exec + (isWindows() ? ".exe" : ""));
}

private static final Pattern PAT_JAVA_VERSION_LINE = Pattern.compile(".*?\"(.+?)\"");
private static final Pattern PAT_JAVA_VERSION_LINE = Pattern.compile(".*?\"(.+?)\".*");

private static String getActualJavaVersion(Path javaHome) {
try {
Expand Down Expand Up @@ -4349,7 +4349,7 @@ protected static final int compareVersions(String a, String b) {
}

private static int compareVersions(int[] a, int[] b) {
return compareVersions(a, b, 5);
return compareVersions(a, b, Math.min(a.length, b.length));
}

private static int compareVersions(int[] a, int[] b, int n) {
Expand All @@ -4368,19 +4368,27 @@ private static boolean equals(int[] a, int[] b, int n) {
return true;
}

private static final Pattern PAT_JAVA_VERSION = Pattern.compile("(?<major>\\d+)(\\.(?<minor>\\d+))?(?:\\.(?<patch>\\d+))?(_(?<update>\\d+))?(-(?<pre>[^-]+))?(-(?<build>.+))?");
private static final Pattern PAT_JAVA_VERSION = Pattern.compile("(?<major>1)(\\.(?<minor>\\d+))?(?:\\.(?<patch>\\d+))?(_(?<update>\\d+))?(-(?<pre>[^-]+))?(-(?<build>.+))?");
// from https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/Runtime.Version.html
private static final Pattern PAT_JAVA_VERSION_JDK10 = Pattern.compile("(?<major>\\d+)(\\.(?<minor>\\d+))?(?:\\.(?<patch>\\d+))?(\\.(?<update>\\d+))?(-(?<pre>[^-]+))?.*");

// visible for testing
static int[] parseJavaVersion(String v) {
final Matcher m = PAT_JAVA_VERSION.matcher(v);
Matcher m = PAT_JAVA_VERSION.matcher(v);
if (!m.matches())
throw new IllegalArgumentException("Could not parse version: " + v);
{
m = PAT_JAVA_VERSION_JDK10.matcher(v);
if (!m.matches())
{
throw new IllegalArgumentException("Could not parse version: " + v);
}
}
final int[] ver = new int[5];
ver[0] = toInt(m.group("major"));
ver[1] = toInt(m.group("minor"));
ver[2] = toInt(m.group("patch"));
ver[3] = toInt(m.group("update"));
if (ver[0] > 1 && ver[1] == 0) {
if (ver[0] > 1 && ver[0] < 9 && ver[1] == 0) {
ver[1] = ver[0];
ver[0] = 1;
}
Expand All @@ -4403,7 +4411,10 @@ static String toJavaVersionString(int[] version) {
sb.append(version[1]).append('.');
sb.append(version[2]);
if (version.length > 3 && version[3] > 0)
sb.append('_').append(version[3]);
{
char sep = version[0]>9 ? '.':'_';
sb.append(sep).append(version[3]);
}
if (version.length > 4 && version[4] != 0) {
final String pre;
switch (version[4]) {
Expand Down Expand Up @@ -5593,14 +5604,9 @@ private void overridePlatformMBeanServer() {

if (platformMBeanServer instanceof com.sun.jmx.mbeanserver.JmxMBeanServer) {
final MBeanServer interceptor = (MBeanServer) Proxy.newProxyInstance(MY_CLASSLOADER, new Class<?>[]{MBeanServer.class}, this);

Field interceptorField = accessible(com.sun.jmx.mbeanserver.JmxMBeanServer.class.getDeclaredField("mbsInterceptor"));
// this.origMBeanServer = ((com.sun.jmx.mbeanserver.JmxMBeanServer) platformMBeanServer).getMBeanServerInterceptor();
this.origMBeanServer = (MBeanServer) interceptorField.get(platformMBeanServer);
// ((com.sun.jmx.mbeanserver.JmxMBeanServer) platformMBeanServer).setMBeanServerInterceptor(interceptor);
interceptorField.set(platformMBeanServer, interceptor);
this.origMBeanServer = ((com.sun.jmx.mbeanserver.JmxMBeanServer) platformMBeanServer).getMBeanServerInterceptor();
((com.sun.jmx.mbeanserver.JmxMBeanServer) platformMBeanServer).setMBeanServerInterceptor(interceptor);
}
// accessible(ManagementFactory.class.getDeclaredField("platformMBeanServer")).set(null, this);
} catch (Throwable e) { // JDK 9 throws an IllegalAccessError
log(LOG_VERBOSE, "Overriding platform MBeanServer failed: " + e.getMessage());
log(LOG_VERBOSE, e);
Expand Down
43 changes: 43 additions & 0 deletions capsule/src/test/java/CapsuleTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1317,13 +1317,56 @@ public void testParseJavaVersion() {
assertEquals("1.8.0_30-ea", Capsule.toJavaVersionString(ver));
}

@Test
public void testParseJavaVersion9plus()
{
int[] ver;

ver = Capsule.parseJavaVersion("11");
assertArrayEquals(ver, ints(11, 0, 0, 0, 0));
assertEquals("11.0.0", Capsule.toJavaVersionString(ver));

ver = Capsule.parseJavaVersion("11.0.9");
assertArrayEquals(ver, ints(11, 0, 9, 0, 0));
assertEquals("11.0.9", Capsule.toJavaVersionString(ver));

ver = Capsule.parseJavaVersion("11.0.9-ea");
assertArrayEquals(ver, ints(11, 0, 9, 0, -3));
assertEquals("11.0.9-ea", Capsule.toJavaVersionString(ver));

ver = Capsule.parseJavaVersion("11.0.9.1");
assertArrayEquals(ver, ints(11, 0, 9, 1, 0));
assertEquals("11.0.9.1", Capsule.toJavaVersionString(ver));

ver = Capsule.parseJavaVersion("11.2.9.1");
assertArrayEquals(ver, ints(11, 2, 9, 1, 0));
assertEquals("11.2.9.1", Capsule.toJavaVersionString(ver));

ver = Capsule.parseJavaVersion("11.0.9.1-ea");
assertArrayEquals(ver, ints(11, 0, 9, 1, -3));
assertEquals("11.0.9.1-ea", Capsule.toJavaVersionString(ver));

ver = Capsule.parseJavaVersion("11.0.9.1-ea+1-opt");
assertArrayEquals(ver, ints(11, 0, 9, 1, -3));
assertEquals("11.0.9.1-ea", Capsule.toJavaVersionString(ver));

ver = Capsule.parseJavaVersion("11.0.9.1+-opt");
assertArrayEquals(ver, ints(11, 0, 9, 1, 0));
assertEquals("11.0.9.1", Capsule.toJavaVersionString(ver));

ver = Capsule.parseJavaVersion("9.0.1+-opt");
assertArrayEquals(ver, ints(9, 0, 1, 0, 0));
assertEquals("9.0.1", Capsule.toJavaVersionString(ver));
}

@Test
public void testCompareVersions() {
assertTrue(Capsule.compareVersions("1.8.0_30-ea", "1.8.0_30") < 0);
assertTrue(Capsule.compareVersions("1.8.0_30-ea", "1.8.0_20") > 0);
assertTrue(Capsule.compareVersions("1.8.0-ea", "1.8.0_20") < 0);
assertTrue(Capsule.compareVersions("1.8.0-ea", "1.8.0") < 0);
assertTrue(Capsule.compareVersions("1.8.0-ea", "1.7.0") > 0);
assertTrue(Capsule.compareVersions("11", "10.0.1.0") > 0);
}

@Test
Expand Down

0 comments on commit a9a6b2c

Please sign in to comment.