private static void test(String s, Integer major, Integer minor, Integer sec, String pre, Integer build, String opt) { Version v = testParse(s); testStr(v.toString(), s); testInt(v.major(), major); testInt(v.minor(), minor); testInt(v.security(), sec); testStr((v.pre().isPresent() ? v.pre().get() : ""), pre); testInt((v.build().isPresent() ? v.build().get() : 0), build); testStr((v.optional().isPresent() ? v.optional().get() : ""), opt); testVersion(v.version(), s); }
public static void main(String[] args) { printSystemProperty("java.version"); printSystemProperty("java.runtime.version"); printSystemProperty("java.vm.version"); printSystemProperty("java.specification.version"); printSystemProperty("java.vm.specification.version"); Version version = Runtime.version(); System.out.println(); System.out.println("Reported by runtime: " + version); switch (version.major()) { case 9: System.out.println("Modularity!"); break; case 10: System.out.println("Value Types!"); break; } }
TaskFactory(JShell state) { this.state = state; this.compiler = ToolProvider.getSystemJavaCompiler(); if (compiler == null) { throw new UnsupportedOperationException("Compiler not available, must be run with full JDK 9."); } Version current = Version.parse(System.getProperty("java.specification.version")); if (INITIAL_SUPPORTED_VER.compareToIgnoreOptional(current) > 0) { throw new UnsupportedOperationException("Wrong compiler, must be run with full JDK 9."); } this.fileManager = new MemoryFileManager( compiler.getStandardFileManager(null, null, null), state); }
JarFileSystem(ZipFileSystemProvider provider, Path zfpath, Map<String,?> env) throws IOException { super(provider, zfpath, env); lookup = path -> path; // lookup needs to be set before isMultiReleaseJar is called // because it eventually calls getEntry if (isMultiReleaseJar()) { int version; Object o = env.get("multi-release"); if (o instanceof String) { String s = (String)o; if (s.equals("runtime")) { version = Runtime.version().major(); } else { version = Integer.parseInt(s); } } else if (o instanceof Integer) { version = (Integer)o; } else if (o instanceof Version) { version = ((Version)o).major(); } else { throw new IllegalArgumentException("env parameter must be String, Integer, " + "or Version"); } lookup = createVersionedLinks(version < 0 ? 0 : version); setReadOnly(); } }
private static void tryCatch(String s, Class<? extends Throwable> ex) { Throwable t = null; try { Version.parse(s); } catch (Throwable x) { if (ex.isAssignableFrom(x.getClass())) { t = x; } else x.printStackTrace(); } if ((t == null) && (ex != null)) fail(s, ex); else pass(); }
private static void testVersion() { Version current = Runtime.version(); String javaVer = System.getProperty("java.runtime.version"); // java.runtime.version == $VNUM(\-$PRE)?(\+$BUILD)?(-$OPT)? String [] jv = javaVer.split("\\+"); String [] ver = jv[0].split("-"); List<Integer> javaVerVNum = Arrays.stream(ver[0].split("\\.")) .map(Integer::parseInt) .collect(Collectors.toList()); if (!javaVerVNum.equals(current.version())) { fail("Runtime.version()", javaVerVNum.toString(), current.version().toString()); } else { pass(); } Optional<String> javaVerPre = (ver.length == 2) ? Optional.ofNullable(ver[1]) : Optional.empty(); if (!javaVerPre.equals(current.pre())) { fail("testCurrent() pre()", javaVerPre.toString(), current.pre().toString()); } else { pass(); } testEHC(current.toString(), javaVer, true, true, 0, 0); }
private static void testEHC(String s0, String s1, boolean eq, boolean eqNO, int cmp, int cmpNO) { Version v0 = Version.parse(s0); Version v1 = Version.parse(s1); testEquals(v0, v1, eq); testEqualsNO(v0, v1, eqNO); testHashCode(v0, v1, eq); testCompare(v0, v1, cmp); testCompareNO(v0, v1, cmpNO); }
private static void testEqualsNO(Version v0, Version v1, boolean eq) { if (eq == v0.equalsIgnoreOptional(v1)) { pass(); } else { fail("equalsIgnoreOptional() " + Boolean.toString(eq), v0.toString(), v1.toString()); } }
private static void testEquals(Version v0, Version v1, boolean eq) { if (eq == v0.equals(v1)) { pass(); } else { fail("equals() " + Boolean.toString(eq), v0.toString(), v1.toString()); } }
private static void testHashCode(Version v0, Version v1, boolean eq) { int h0 = v0.hashCode(); int h1 = v1.hashCode(); if (eq) { testInt(h0, h1); } else if (h0 == h1) { fail(String.format("hashCode() %s", h0), Integer.toString(h0), Integer.toString(h1)); } else { // !eq && (h0 != h1) pass(); } }
private static void checkCompare(Version v0, Version v1, int expected, int actual) { if (Integer.signum(expected) == Integer.signum(actual)) { pass(); } else { fail(String.format("compare() (actual = %s) (expected = %s)", actual, expected), v0.toString(), v1.toString()); } }
@DataProvider(name="versions") public Object[][] createVersions() { return new Object[][] { {Version.parse("8"), 8}, {Version.parse("9"), 9}, {Version.parse("10"), 10}, {Version.parse("11"), 10}, {Version.parse("100"), 10} }; }
TaskFactory(JShell state) { this.state = state; this.compiler = ToolProvider.getSystemJavaCompiler(); if (compiler == null) { throw new UnsupportedOperationException("Compiler not available, must be run with full JDK 9."); } Version current = Version.parse(System.getProperty("java.specification.version")); if (INITIAL_SUPPORTED_VER.compareToIgnoreOpt(current) > 0) { throw new UnsupportedOperationException("Wrong compiler, must be run with full JDK 9."); } this.fileManager = new MemoryFileManager( compiler.getStandardFileManager(null, null, null), state); }
private static void testVersion() { Version current = Runtime.version(); String javaVer = System.getProperty("java.runtime.version"); // java.runtime.version == $VNUM(\-$PRE)?(\+$BUILD)?(-$OPT)? String [] jv = javaVer.split("\\+"); String [] ver = jv[0].split("-"); List<Integer> javaVerVNum = Arrays.stream(ver[0].split("\\.")) .map(v -> Integer.parseInt(v)) .collect(Collectors.toList()); if (!javaVerVNum.equals(current.version())) { fail("Runtime.version()", javaVerVNum.toString(), current.version().toString()); } else { pass(); } Optional<String> javaVerPre = (ver.length == 2) ? Optional.ofNullable(ver[1]) : Optional.empty(); if (!javaVerPre.equals(current.pre())) { fail("testCurrent() pre()", javaVerPre.toString(), current.pre().toString()); } else { pass(); } testEHC(current.toString(), javaVer, true, true, 0, 0); }
private static void testEqualsNO(Version v0, Version v1, boolean eq) { if ((eq && !v0.equalsIgnoreOpt(v1)) || (!eq && v0.equalsIgnoreOpt(v1))) { fail("equalsIgnoreOpt() " + Boolean.toString(eq), v0.toString(), v1.toString()); } else { pass(); } }
private static void testEquals(Version v0, Version v1, boolean eq) { if ((eq && !v0.equals(v1)) || (!eq && v0.equals(v1))) { fail("equals() " + Boolean.toString(eq), v0.toString(), v1.toString()); } else { pass(); } }
private static void testCompareNO(Version v0, Version v1, int compare) { try { Method m = VERSION.getMethod("compareToIgnoreOpt", VERSION); int cmp = (int) m.invoke(v0, v1); checkCompare(v0, v1, compare, cmp); } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException ex) { fail(String.format("compareToIgnoreOpt() invocation: %s", ex.getClass()), null); } }
private static void testCompare(Version v0, Version v1, int compare) { try { Method m = VERSION.getMethod("compareTo", VERSION); int cmp = (int) m.invoke(v0, v1); checkCompare(v0, v1, compare, cmp); } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException ex) { fail(String.format("compareTo() invocation: %s", ex.getClass()), null); } }
private static void checkCompare(Version v0, Version v1, int compare, int cmp) { if (((cmp == 0) && (compare == 0)) || (compare == (cmp / Math.abs(cmp == 0 ? 1 : cmp)))) { pass(); } else { fail(String.format("compare() (cmp = %s) (compare = %s)", cmp, compare), v0.toString(), v1.toString()); } }
private static Version testParse(String s) { Version v = Version.parse(s); pass(); return v; }
private static void testCompareNO(Version v0, Version v1, int compare) { int cmp = v0.compareToIgnoreOptional(v1); checkCompare(v0, v1, compare, cmp); }
private static void testCompare(Version v0, Version v1, int compare) { int cmp = v0.compareTo(v1); checkCompare(v0, v1, compare, cmp); }
@Test(dataProvider="versions") public void testVersions(Version value, int expected) throws Throwable { versionEnv.put("multi-release", value); runTest(versionEnv, expected); }