/** * Creates a new named Module. The resulting Module will be defined to the * VM but will not read any other modules, will not have any exports setup * and will not be registered in the service catalog. */ Module(ModuleLayer layer, ClassLoader loader, ModuleDescriptor descriptor, URI uri) { this.layer = layer; this.name = descriptor.name(); this.loader = loader; this.descriptor = descriptor; // define module to VM boolean isOpen = descriptor.isOpen() || descriptor.isAutomatic(); Version version = descriptor.version().orElse(null); String vs = Objects.toString(version, null); String loc = Objects.toString(uri, null); String[] packages = descriptor.packages().toArray(new String[0]); defineModule0(this, isOpen, vs, loc, packages); }
/** * Returns a {@link Requires} for a dependence on a module with the given * (and possibly empty) set of modifiers, and optionally the version * recorded at compile time. */ public static Requires newRequires(Set<Requires.Modifier> mods, String mn, String compiledVersion) { Version version = null; if (compiledVersion != null) { // use the cached version if the same version string Version ver = cachedVersion; if (ver != null && compiledVersion.equals(ver.toString())) { version = ver; } else { version = Version.parse(compiledVersion); } } return JLMA.newRequires(mods, mn, version); }
@Test(dataProvider = "validVersions") public void testEqualsAndHashCode(String vs, String ignore) { Version v1 = Version.parse(vs); Version v2 = Version.parse(vs); assertEquals(v1, v2); assertEquals(v2, v1); assertEquals(v1.hashCode(), v2.hashCode()); Version v3 = Version.parse("1.0-rhubarb"); assertNotEquals(v1, v3); assertNotEquals(v2, v3); assertNotEquals(v3, v1); assertNotEquals(v3, v2); }
/** * Test compareTo with equal versions. */ @Test(dataProvider = "equalVersions") public void testCompareEqualsVersions(String vs1, String vs2) { Version v1 = Version.parse(vs1); assertTrue(v1.compareTo(v1) == 0); Version v2 = Version.parse(vs2); assertTrue(v2.compareTo(v2) == 0); assertTrue(v1.compareTo(v2) == 0); assertTrue(v2.compareTo(v1) == 0); assertEquals(v1, v2); assertEquals(v2, v1); }
/** * Creates a new named Module. The resulting Module will be defined to the * VM but will not read any other modules, will not have any exports setup * and will not be registered in the service catalog. */ private Module(Layer layer, ClassLoader loader, ModuleDescriptor descriptor, URI uri) { this.layer = layer; this.name = descriptor.name(); this.loader = loader; this.descriptor = descriptor; // define module to VM Set<String> packages = descriptor.packages(); int n = packages.size(); String[] array = new String[n]; int i = 0; for (String pn : packages) { array[i++] = pn.replace('.', '/'); } Version version = descriptor.version().orElse(null); String vs = Objects.toString(version, null); String loc = Objects.toString(uri, null); defineModule0(this, vs, loc, array); }
/** * Returns a new {@code ModuleDescriptor} instance. */ ModuleDescriptor newModuleDescriptor(String name, boolean automatic, boolean synthetic, Set<Requires> requires, Set<String> uses, Set<Exports> exports, Map<String, Provides> provides, Version version, String mainClass, String osName, String osArch, String osVersion, Set<String> conceals, Set<String> packages, ModuleHashes hashes);
/** * Sets the module version. * * @throws IllegalArgumentException if {@code v} is null or cannot be * parsed as a version string * * @see Version#parse(String) */ public Builder version(String v) { Version ver = cachedVersion; if (ver != null && v.equals(ver.toString())) { version = ver; } else { cachedVersion = version = Version.parse(v); } return this; }
/** * Returns a new {@code ModuleDescriptor} instance. */ ModuleDescriptor newModuleDescriptor(String name, Version version, Set<ModuleDescriptor.Modifier> ms, Set<Requires> requires, Set<Exports> exports, Set<Opens> opens, Set<String> uses, Set<Provides> provides, Set<String> packages, String mainClass, int hashCode);
void version(Version v) { mv.visitVarInsn(ALOAD, BUILDER_VAR); mv.visitLdcInsn(v.toString()); mv.visitMethodInsn(INVOKEVIRTUAL, MODULE_DESCRIPTOR_BUILDER, "version", STRING_SIG, false); mv.visitInsn(POP); }
@Override public Version convert(String value) { try { return Version.parse(value); } catch (IllegalArgumentException x) { throw new CommandException("err.invalid.version", x.getMessage()); } }
/** * Test compareTo with ordered versions. */ @Test(dataProvider = "orderedVersions") public void testCompareOrderedVersions(String vs1, String vs2) { Version v1 = Version.parse(vs1); assertTrue(v1.compareTo(v1) == 0); Version v2 = Version.parse(vs2); assertTrue(v2.compareTo(v2) == 0); // v1 < v2 assertTrue(v1.compareTo(v2) < 0); assertTrue(v2.compareTo(v1) > 0); }
public void testRequiresWithCompiledVersion() { Version v = Version.parse("1.0"); Requires r = requires(Set.of(), "foo", v); assertEquals(r, r); assertTrue(r.compareTo(r) == 0); assertEquals(r.modifiers(), Set.of()); assertEquals(r.name(), "foo"); assertTrue(r.compiledVersion().isPresent()); assertEquals(r.compiledVersion().get().toString(), "1.0"); }
public void testRequiresEqualsAndHashCode() { Requires r1 = requires("foo"); Requires r2 = requires("foo"); assertEquals(r1, r2); assertTrue(r1.hashCode() == r2.hashCode()); r1 = requires(EnumSet.allOf(Requires.Modifier.class), "foo"); r2 = requires(EnumSet.allOf(Requires.Modifier.class), "foo"); assertEquals(r1, r2); assertTrue(r1.hashCode() == r2.hashCode()); r1 = requires("foo"); r2 = requires("bar"); assertNotEquals(r1, r2); r1 = requires(EnumSet.allOf(Requires.Modifier.class), "foo"); r2 = requires(Set.of(), "foo"); assertNotEquals(r1, r2); Version v1 = Version.parse("1.0"); r1 = requires(EnumSet.allOf(Requires.Modifier.class), "foo", v1); r2 = requires(EnumSet.allOf(Requires.Modifier.class), "foo", v1); assertEquals(r1, r2); assertTrue(r1.hashCode() == r2.hashCode()); Version v2 = Version.parse("2.0"); r1 = requires(EnumSet.allOf(Requires.Modifier.class), "foo", v1); r2 = requires(EnumSet.allOf(Requires.Modifier.class), "foo", v2); assertNotEquals(r1, r2); }
public void testVersion1() { Version v1 = Version.parse("1.0"); Version v2 = ModuleDescriptor.newModule("foo") .version(v1) .build() .version() .get(); assertEquals(v1, v2); }
public void testVersion2() { String vs = "1.0"; Version v1 = ModuleDescriptor.newModule("foo") .version(vs) .build() .version() .get(); Version v2 = Version.parse(vs); assertEquals(v1, v2); }
private boolean isParsableVersion(String vs) { try { Version.parse(vs); return true; } catch (IllegalArgumentException e) { return false; } }
/** * Sets the module version. * * @throws IllegalArgumentException if {@code v} is null or cannot be * parsed as a version string * @throws IllegalStateException if the module version is already set * * @see Version#parse(String) */ public Builder version(String v) { if (version != null) throw new IllegalStateException("module version already set"); Version ver = cachedVersion; if (ver != null && v.equals(ver.toString())) { version = ver; } else { cachedVersion = version = Version.parse(v); } return this; }
@Override protected Attribute read(ClassReader cr, int off, int len, char[] buf, int codeOff, Label[] labels) { String value = cr.readUTF8(off, buf); return new VersionAttribute(Version.parse(value)); }
public ModuleAttribute(Version v) { super(MODULE); this.replacementVersion = v; }
@Test(dataProvider = "validVersions") public void testParseValidVersions(String vs, String ignore) { Version v = Version.parse(vs); assertEquals(v.toString(), vs); }
@Test(dataProvider = "invalidVersions", expectedExceptions = IllegalArgumentException.class ) public void testParseInvalidVersions(String vs, String ignore) { Version.parse(vs); }
public void testRequiresCompareWithSameCompiledVersion() { Requires r1 = requires(Set.of(), "foo", Version.parse("2.0")); Requires r2 = requires(Set.of(), "foo", Version.parse("2.0")); assertTrue(r1.compareTo(r2) == 0); assertTrue(r2.compareTo(r1) == 0); }
public void testRequiresCompareWithDifferentCompiledVersion() { Requires r1 = requires(Set.of(), "foo", Version.parse("1.0")); Requires r2 = requires(Set.of(), "foo", Version.parse("2.0")); assertTrue(r1.compareTo(r2) < 0); assertTrue(r2.compareTo(r1) > 0); }
@Test(expectedExceptions = NullPointerException.class ) public void testNullVersion1() { ModuleDescriptor.newModule("foo").version((Version) null); }
@Test public void test6() throws IOException { // create a directory for this tests special files Files.createDirectory(Paths.get("test6")); Files.createDirectory(Paths.get("test6-v9")); // compile the classes directory Path src = testsrc.resolve("src").resolve("classes"); Path dst = Paths.get("test6"); javac(src, dst); byte[] mdBytes = Files.readAllBytes(Paths.get("module-info.class")); ModuleInfoExtender mie = ModuleInfoExtender.newExtender( new ByteArrayInputStream(mdBytes)); mie.mainClass("p.Main"); mie.version(Version.parse("1.0")); ByteArrayOutputStream baos = new ByteArrayOutputStream(); mie.write(baos); Files.write(Paths.get("test6", "module-info.class"), baos.toByteArray()); Files.write(Paths.get("test6-v9", "module-info.class"), baos.toByteArray()); int rc = jar("--create --file mmr.jar -C test6 . --release 9 -C test6-v9 ."); Assert.assertEquals(rc, 0); // different main-class mie = ModuleInfoExtender.newExtender(new ByteArrayInputStream(mdBytes)); mie.mainClass("p.Main2"); mie.version(Version.parse("1.0")); baos.reset(); mie.write(baos); Files.write(Paths.get("test6-v9", "module-info.class"), baos.toByteArray()); rc = jar("--create --file mmr.jar -C test6 . --release 9 -C test6-v9 ."); Assert.assertEquals(rc, 1); Assert.assertTrue(Message.CONTAINS_DIFFERENT_MAINCLASS.match( new String(errbytes.toByteArray()), "META-INF/versions/9/module-info.class")); // different version mie = ModuleInfoExtender.newExtender(new ByteArrayInputStream(mdBytes)); mie.mainClass("p.Main"); mie.version(Version.parse("2.0")); baos.reset(); mie.write(baos); Files.write(Paths.get("test6-v9", "module-info.class"), baos.toByteArray()); rc = jar("--create --file mmr.jar -C test6 . --release 9 -C test6-v9 ."); Assert.assertEquals(rc, 1); Assert.assertTrue(Message.CONTAINS_DIFFERENT_VERSION.match( new String(errbytes.toByteArray()), "META-INF/versions/9/module-info.class")); }
VersionAttribute(Version version) { super(VERSION); this.version = version; }
public void testVersion1() { Version v1 = Version.parse("1.0"); Version v2 = new Builder("foo").version(v1).build().version().get(); assertEquals(v1, v2); }
public void testVersion2() { String vs = "1.0"; Version v1 = new Builder("foo").version(vs).build().version().get(); Version v2 = Version.parse(vs); assertEquals(v1, v2); }
@Test(expectedExceptions = NullPointerException.class ) public void testNullVersion1() { new Builder("foo").version((Version)null); }
@Test(expectedExceptions = IllegalStateException.class) public void testDuplicateVersion1() { Version v = Version.parse("2.0"); new Builder("foo").version("1.0").version(v); }
static void version() { System.out.println( "pro " + Main.class.getModule().getDescriptor().version().map(Version::toString).orElse("unknown") + " / jdk " + Runtime.version()); }
/** * Returns a {@code ModuleDescriptor.Requires} of the given modifiers * and module name. */ Requires newRequires(Set<Requires.Modifier> ms, String mn, Version v);
/** * Returns a {@code ModuleDescriptor.Version} of the given version. */ Version newVersion(String v);
@Override public Class<Version> valueType() { return Version.class; }