/* * Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
Path jar = new JarBuilder(name)
.moduleInfo("module-info.class", descriptor)
.resource("p/Main.class")
.resource("p/Helper.class")
.resource("META-INF/versions/" + VERSION + "/p/Helper.class")
.resource("META-INF/versions/" + VERSION + "/p/internal/Helper.class")
.build();
// find the module
ModuleFinder finder = ModuleFinder.of(jar);
Optional<ModuleReference> omref = finder.find(name);
assertTrue((omref.isPresent()));
ModuleReference mref = omref.get();
/** * Test a multi-release JAR with a module-info.class in the versioned * section of the JAR.
*/ publicvoid testModuleInfoInVersionedSection() throws Exception {
String name = "m1";
Path jar = new JarBuilder(name)
.moduleInfo(MODULE_INFO, descriptor1)
.resource("p/Main.class")
.resource("p/Helper.class")
.moduleInfo("META-INF/versions/" + VERSION + "/" + MODULE_INFO, descriptor2)
.resource("META-INF/versions/" + VERSION + "/p/Helper.class")
.resource("META-INF/versions/" + VERSION + "/p/internal/Helper.class")
.build();
// find the module
ModuleFinder finder = ModuleFinder.of(jar);
Optional<ModuleReference> omref = finder.find(name);
assertTrue((omref.isPresent()));
ModuleReference mref = omref.get();
// ensure that the right module-info.class is loaded
ModuleDescriptor descriptor = mref.descriptor();
assertEquals(descriptor.name(), name); if (MULTI_RELEASE) {
assertEquals(descriptor.requires(), descriptor2.requires());
} else {
assertEquals(descriptor.requires(), descriptor1.requires());
}
}
/** * Test multi-release JAR as an automatic module.
*/ publicvoid testAutomaticModule() throws Exception {
String name = "m";
Path jar = new JarBuilder(name)
.resource("p/Main.class")
.resource("p/Helper.class")
.resource("META-INF/versions/" + VERSION + "/p/Helper.class")
.resource("META-INF/versions/" + VERSION + "/p/internal/Helper.class")
.build();
// find the module
ModuleFinder finder = ModuleFinder.of(jar);
Optional<ModuleReference> omref = finder.find(name);
assertTrue((omref.isPresent()));
ModuleReference mref = omref.get();
/** * Check that two ModuleDescriptor have the same requires
*/ staticvoid checkRequires(ModuleDescriptor md1, ModuleDescriptor md2) {
assertEquals(md1.requires(), md2.requires());
}
/** * A builder of multi-release JAR files.
*/ staticclass JarBuilder { private String name; private Set<String> resources = new HashSet<>(); private Map<String, ModuleDescriptor> descriptors = new HashMap<>();
JarBuilder(String name) { this.name = name;
}
/** * Adds a module-info.class to the JAR file.
*/
JarBuilder moduleInfo(String name, ModuleDescriptor descriptor) {
descriptors.put(name, descriptor); returnthis;
}
/** * Adds a dummy resource to the JAR file.
*/
JarBuilder resource(String name) {
resources.add(name); returnthis;
}
/** * Create the multi-release JAR, returning its file path.
*/
Path build() throws Exception {
Path dir = Files.createTempDirectory(Paths.get(""), "jar");
List<Path> files = new ArrayList<>();
// write the module-info.class for (Map.Entry<String, ModuleDescriptor> e : descriptors.entrySet()) {
String name = e.getKey();
ModuleDescriptor descriptor = e.getValue();
Path mi = Paths.get(name.replace('/', File.separatorChar));
Path parent = dir.resolve(mi).getParent(); if (parent != null)
Files.createDirectories(parent); try (OutputStream out = Files.newOutputStream(dir.resolve(mi))) {
ModuleInfoWriter.write(descriptor, out);
}
files.add(mi);
}
// write the dummy resources for (String name : resources) {
Path file = Paths.get(name.replace('/', File.separatorChar)); // create dummy resource
Path parent = dir.resolve(file).getParent(); if (parent != null)
Files.createDirectories(parent);
Files.createFile(dir.resolve(file));
files.add(file);
}
Manifest man = new Manifest();
Attributes attrs = man.getMainAttributes();
attrs.put(Attributes.Name.MANIFEST_VERSION, "1.0");
attrs.put(Attributes.Name.MULTI_RELEASE, "true");
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.