java.lang.module.ModuleDescriptor.Version Java Examples

The following examples show how to use java.lang.module.ModuleDescriptor.Version. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Builder.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example #2
Source File: Module.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example #3
Source File: Module.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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();
    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);
}
 
Example #4
Source File: Builder.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example #5
Source File: VersionTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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);

}
 
Example #6
Source File: VersionTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@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);

}
 
Example #7
Source File: ModuleDescriptorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example #8
Source File: ModuleDescriptorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public void testVersion1() {
    Version v1 = Version.parse("1.0");
    Version v2 = ModuleDescriptor.newModule("foo")
            .version(v1)
            .build()
            .version()
            .get();
    assertEquals(v1, v2);
}
 
Example #9
Source File: ModuleDescriptorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example #10
Source File: ModuleDescriptorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
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");
}
 
Example #11
Source File: VersionTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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);

}
 
Example #12
Source File: ModuleDescriptorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private boolean isParsableVersion(String vs) {
    try {
        Version.parse(vs);
        return true;
    } catch (IllegalArgumentException e) {
        return false;
    }
}
 
Example #13
Source File: JmodTask.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public Version convert(String value) {
    try {
        return Version.parse(value);
    } catch (IllegalArgumentException x) {
        throw new CommandException("err.invalid.version", x.getMessage());
    }
}
 
Example #14
Source File: SystemModulesPlugin.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example #15
Source File: JavaLangModuleAccess.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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);
 
Example #16
Source File: ModuleFixerPlugin.java    From pro with GNU General Public License v3.0 5 votes vote down vote up
private static Path generateModuleInfo(ModuleReference ref,
                                       Map<String, RequireModifier> requires,
                                       Set<String> exports, Set<String> uses,
                                       Map<String, Set<String>> provides,
                                       Optional<Version> versionOpt,
                                       Path moduleDependencyFixerPath) throws IOException {
  var moduleName = ref.descriptor().name();
  
  //System.out.println(moduleName);
  //System.out.println("requires: " + requires);
  //System.out.println("exports: " + exports);
  //System.out.println("uses: " + uses);
  //System.out.println("provides: " + provides);
  
  var modulePatchPath = moduleDependencyFixerPath.resolve(moduleName);
  Files.createDirectories(modulePatchPath);
  
  var builder = ModuleDescriptor.newModule(moduleName, Set.of(Modifier.OPEN));
  ref.descriptor().version().ifPresent(version -> builder.version(version.toString()));
  requires.forEach((require, requireModifier) -> builder.requires(requireModifier == RequireModifier.STATIC? EnumSet.of(Requires.Modifier.STATIC): Set.of(), require));
  exports.forEach(export -> builder.exports(export.replace('/', '.')));
  uses.forEach(use -> builder.uses(use.replace('/', '.')));
  provides.forEach((service, providers) -> builder.provides(service, new ArrayList<>(providers)));
  versionOpt.ifPresent(version -> builder.version(version));
  
  var generatedModuleInfoPath = modulePatchPath.resolve("module-info.class");
  Files.write(generatedModuleInfoPath, ModuleHelper.moduleDescriptorToBinary(builder.build()));
  return generatedModuleInfoPath;
}
 
Example #17
Source File: ModuleFixerPlugin.java    From pro with GNU General Public License v3.0 5 votes vote down vote up
private static Optional<Object> extractManifestValue(String key, String value) {
  switch(key) {
  case "Implementation-Version":
    try {
      return Optional.of(Version.parse(value));
    } catch(@SuppressWarnings("unused") IllegalArgumentException e) { // not a valid version
      return Optional.empty();
    }
  default:
    return Optional.empty();
  }
}
 
Example #18
Source File: Builder.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example #19
Source File: JavaLangModuleAccess.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/**
 * 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);
 
Example #20
Source File: Builder.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example #21
Source File: Basic.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@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"));

}
 
Example #22
Source File: ModuleDescriptorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Test(expectedExceptions = NullPointerException.class )
public void testNullVersion1() {
    ModuleDescriptor.newModule("foo").version((Version) null);
}
 
Example #23
Source File: Main.java    From pro with GNU General Public License v3.0 4 votes vote down vote up
static void version() {
  System.out.println(
      "pro " + Main.class.getModule().getDescriptor().version().map(Version::toString).orElse("unknown") +
      " / jdk " + Runtime.version());
}
 
Example #24
Source File: ModuleDescriptorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
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);
}
 
Example #25
Source File: ModuleDescriptorTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
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);
}
 
Example #26
Source File: VersionTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Test(dataProvider = "invalidVersions",
      expectedExceptions = IllegalArgumentException.class )
public void testParseInvalidVersions(String vs, String ignore) {
    Version.parse(vs);
}
 
Example #27
Source File: VersionTest.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Test(dataProvider = "validVersions")
public void testParseValidVersions(String vs, String ignore) {
    Version v = Version.parse(vs);
    assertEquals(v.toString(), vs);
}
 
Example #28
Source File: ClassFileAttributes.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
public ModuleAttribute(Version v) {
    super(MODULE);
    this.replacementVersion = v;
}
 
Example #29
Source File: JavaLangModuleAccess.java    From openjdk-jdk9 with GNU General Public License v2.0 2 votes vote down vote up
/**
 * Returns a {@code ModuleDescriptor.Requires} of the given modifiers
 * and module name.
 */
Requires newRequires(Set<Requires.Modifier> ms, String mn, Version v);
 
Example #30
Source File: JavaLangModuleAccess.java    From Bytecoder with Apache License 2.0 2 votes vote down vote up
/**
 * Returns a {@code ModuleDescriptor.Requires} of the given modifiers
 * and module name.
 */
Requires newRequires(Set<Requires.Modifier> ms, String mn, Version v);