undefect. CWE-407 — 63 sites patched across 27 ecosystems

Authors: russell@unturf.com · brackishbert@gmail.com · foxhop.net · TimeHexOn.com

Patches, unit tests, benchmarks, whitepaper, and outreach briefs.
Public domain — no copyright claimed. Use freely.
This commit is contained in:
russell@unturf.com 2026-03-26 17:11:57 -04:00
commit 0a580b313d
70422 changed files with 17213626 additions and 0 deletions

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,811 @@
/*
* Copyright (c) 2015, 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.
*/
/**
* @test
* @library /test/lib
* @modules jdk.compiler
* @build LayerAndLoadersTest
* jdk.test.lib.compiler.CompilerUtils
* jdk.test.lib.util.ModuleUtils
* @run testng LayerAndLoadersTest
* @summary Tests for java.lang.ModuleLayer@defineModulesWithXXX methods
*/
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.lang.module.Configuration;
import java.lang.module.ModuleDescriptor;
import java.lang.module.ModuleFinder;
import java.lang.module.ModuleReference;
import java.lang.module.ResolvedModule;
import java.lang.reflect.Method;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.ServiceLoader;
import java.util.Set;
import java.util.stream.Collectors;
import jdk.test.lib.compiler.CompilerUtils;
import jdk.test.lib.util.ModuleUtils;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import static org.testng.Assert.*;
@Test
public class LayerAndLoadersTest {
private static final String TEST_SRC = System.getProperty("test.src");
private static final Path SRC_DIR = Paths.get(TEST_SRC, "src");
private static final Path MODS_DIR = Paths.get("mods");
@BeforeTest
public void setup() throws Exception {
// javac -d mods --module-source-path src src/**
assertTrue(CompilerUtils.compile(SRC_DIR, MODS_DIR,
"--module-source-path", SRC_DIR.toString()));
}
/**
* Basic test of ModuleLayer.defineModulesWithOneLoader
*
* Test scenario:
* m1 requires m2 and m3
*/
public void testWithOneLoader() throws Exception {
Configuration cf = resolve("m1");
ClassLoader scl = ClassLoader.getSystemClassLoader();
ModuleLayer layer = ModuleLayer.boot().defineModulesWithOneLoader(cf, scl);
checkLayer(layer, "m1", "m2", "m3");
ClassLoader cl1 = layer.findLoader("m1");
ClassLoader cl2 = layer.findLoader("m2");
ClassLoader cl3 = layer.findLoader("m3");
assertTrue(cl1.getParent() == scl);
assertTrue(cl2 == cl1);
assertTrue(cl3 == cl1);
invoke(layer, "m1", "p.Main");
}
/**
* Basic test of ModuleLayer.defineModulesWithManyLoaders
*
* Test scenario:
* m1 requires m2 and m3
*/
public void testWithManyLoaders() throws Exception {
Configuration cf = resolve("m1");
ClassLoader scl = ClassLoader.getSystemClassLoader();
ModuleLayer layer = ModuleLayer.boot().defineModulesWithManyLoaders(cf, scl);
checkLayer(layer, "m1", "m2", "m3");
ClassLoader cl1 = layer.findLoader("m1");
ClassLoader cl2 = layer.findLoader("m2");
ClassLoader cl3 = layer.findLoader("m3");
assertTrue(cl1.getParent() == scl);
assertTrue(cl2.getParent() == scl);
assertTrue(cl3.getParent() == scl);
assertTrue(cl2 != cl1);
assertTrue(cl3 != cl1);
assertTrue(cl3 != cl2);
invoke(layer, "m1", "p.Main");
}
/**
* Basic test of ModuleLayer.defineModulesWithOneLoader where one of the
* modules is a service provider module.
*
* Test scenario:
* m1 requires m2 and m3
* m1 uses S
* m4 provides S with ...
*/
public void testServicesWithOneLoader() throws Exception {
Configuration cf = resolveAndBind("m1");
ClassLoader scl = ClassLoader.getSystemClassLoader();
ModuleLayer layer = ModuleLayer.boot().defineModulesWithOneLoader(cf, scl);
checkLayer(layer, "m1", "m2", "m3", "m4");
ClassLoader cl1 = layer.findLoader("m1");
ClassLoader cl2 = layer.findLoader("m2");
ClassLoader cl3 = layer.findLoader("m3");
ClassLoader cl4 = layer.findLoader("m4");
assertTrue(cl1.getParent() == scl);
assertTrue(cl2 == cl1);
assertTrue(cl3 == cl1);
assertTrue(cl4 == cl1);
Class<?> serviceType = cl1.loadClass("p.Service");
assertTrue(serviceType.getClassLoader() == cl1);
Iterator<?> iter = ServiceLoader.load(serviceType, cl1).iterator();
Object provider = iter.next();
assertTrue(serviceType.isInstance(provider));
assertTrue(provider.getClass().getClassLoader() == cl1);
assertFalse(iter.hasNext());
}
/**
* Basic test of ModuleLayer.defineModulesWithManyLoaders where one of the
* modules is a service provider module.
*
* Test scenario:
* m1 requires m2 and m3
* m1 uses S
* m4 provides S with ...
*/
public void testServicesWithManyLoaders() throws Exception {
Configuration cf = resolveAndBind("m1");
ClassLoader scl = ClassLoader.getSystemClassLoader();
ModuleLayer layer = ModuleLayer.boot().defineModulesWithManyLoaders(cf, scl);
checkLayer(layer, "m1", "m2", "m3", "m4");
ClassLoader cl1 = layer.findLoader("m1");
ClassLoader cl2 = layer.findLoader("m2");
ClassLoader cl3 = layer.findLoader("m3");
ClassLoader cl4 = layer.findLoader("m4");
assertTrue(cl1.getParent() == scl);
assertTrue(cl2.getParent() == scl);
assertTrue(cl3.getParent() == scl);
assertTrue(cl4.getParent() == scl);
assertTrue(cl2 != cl1);
assertTrue(cl3 != cl1);
assertTrue(cl3 != cl2);
assertTrue(cl4 != cl1);
assertTrue(cl4 != cl2);
assertTrue(cl4 != cl3);
Class<?> serviceType = cl1.loadClass("p.Service");
assertTrue(serviceType.getClassLoader() == cl1);
// Test that the service provider can be located via any of
// the class loaders in the layer
for (Module m : layer.modules()) {
ClassLoader loader = m.getClassLoader();
Iterator<?> iter = ServiceLoader.load(serviceType, loader).iterator();
Object provider = iter.next();
assertTrue(serviceType.isInstance(provider));
assertTrue(provider.getClass().getClassLoader() == cl4);
assertFalse(iter.hasNext());
}
}
/**
* Tests that the class loaders created by defineModulesWithXXX delegate
* to the given parent class loader.
*/
public void testDelegationToParent() throws Exception {
Configuration cf = resolve("m1");
ClassLoader parent = this.getClass().getClassLoader();
String cn = this.getClass().getName();
// one loader
ModuleLayer layer = ModuleLayer.boot().defineModulesWithOneLoader(cf, parent);
testLoad(layer, cn);
// one loader with boot loader as parent
layer = ModuleLayer.boot().defineModulesWithOneLoader(cf, null);
testLoadFail(layer, cn);
// many loaders
layer = ModuleLayer.boot().defineModulesWithManyLoaders(cf, parent);
testLoad(layer, cn);
// many loader with boot loader as parent
layer = ModuleLayer.boot().defineModulesWithManyLoaders(cf, null);
testLoadFail(layer, cn);
}
/**
* Test defineModulesWithXXX when modules that have overlapping packages.
*
* Test scenario:
* m1 exports p
* m2 exports p
*/
public void testOverlappingPackages() {
ModuleDescriptor descriptor1
= ModuleDescriptor.newModule("m1").exports("p").build();
ModuleDescriptor descriptor2
= ModuleDescriptor.newModule("m2").exports("p").build();
ModuleFinder finder = ModuleUtils.finderOf(descriptor1, descriptor2);
Configuration cf = ModuleLayer.boot()
.configuration()
.resolve(finder, ModuleFinder.of(), Set.of("m1", "m2"));
// cannot define both module m1 and m2 to the same class loader
try {
ModuleLayer.boot().defineModulesWithOneLoader(cf, null);
assertTrue(false);
} catch (LayerInstantiationException expected) { }
// should be okay to have one module per class loader
ModuleLayer layer = ModuleLayer.boot().defineModulesWithManyLoaders(cf, null);
checkLayer(layer, "m1", "m2");
}
/**
* Test ModuleLayer.defineModulesWithXXX with split delegation.
*
* Test scenario:
* layer1: m1 exports p, m2 exports p
* layer2: m3 reads m1, m4 reads m2
*/
public void testSplitDelegation() {
ModuleDescriptor descriptor1
= ModuleDescriptor.newModule("m1").exports("p").build();
ModuleDescriptor descriptor2
= ModuleDescriptor.newModule("m2").exports("p").build();
ModuleFinder finder1 = ModuleUtils.finderOf(descriptor1, descriptor2);
Configuration cf1 = ModuleLayer.boot()
.configuration()
.resolve(finder1, ModuleFinder.of(), Set.of("m1", "m2"));
ModuleLayer layer1 = ModuleLayer.boot().defineModulesWithManyLoaders(cf1, null);
checkLayer(layer1, "m1", "m2");
ModuleDescriptor descriptor3
= ModuleDescriptor.newModule("m3").requires("m1").build();
ModuleDescriptor descriptor4
= ModuleDescriptor.newModule("m4").requires("m2").build();
ModuleFinder finder2 = ModuleUtils.finderOf(descriptor3, descriptor4);
Configuration cf2 = cf1.resolve(finder2, ModuleFinder.of(),
Set.of("m3", "m4"));
// package p cannot be supplied by two class loaders
try {
layer1.defineModulesWithOneLoader(cf2, null);
assertTrue(false);
} catch (LayerInstantiationException expected) { }
// no split delegation when modules have their own class loader
ModuleLayer layer2 = layer1.defineModulesWithManyLoaders(cf2, null);
checkLayer(layer2, "m3", "m4");
}
/**
* Test ModuleLayer.defineModulesWithXXX when the modules that override same
* named modules in the parent layer.
*
* Test scenario:
* layer1: m1, m2, m3 => same loader
* layer2: m1, m2, m4 => same loader
*/
public void testOverriding1() throws Exception {
Configuration cf1 = resolve("m1");
ModuleLayer layer1 = ModuleLayer.boot().defineModulesWithOneLoader(cf1, null);
checkLayer(layer1, "m1", "m2", "m3");
ModuleFinder finder = ModuleFinder.of(MODS_DIR);
Configuration cf2 = cf1.resolve(finder, ModuleFinder.of(),
Set.of("m1"));
ModuleLayer layer2 = layer1.defineModulesWithOneLoader(cf2, null);
checkLayer(layer2, "m1", "m2", "m3");
invoke(layer1, "m1", "p.Main");
ClassLoader loader1 = layer1.findLoader("m1");
ClassLoader loader2 = layer1.findLoader("m2");
ClassLoader loader3 = layer1.findLoader("m3");
ClassLoader loader4 = layer2.findLoader("m1");
ClassLoader loader5 = layer2.findLoader("m2");
ClassLoader loader6 = layer2.findLoader("m3");
assertTrue(loader1 == loader2);
assertTrue(loader1 == loader3);
assertTrue(loader4 == loader5);
assertTrue(loader4 == loader6);
assertTrue(loader4 != loader1);
assertTrue(loader1.loadClass("p.Main").getClassLoader() == loader1);
assertTrue(loader1.loadClass("q.Hello").getClassLoader() == loader1);
assertTrue(loader1.loadClass("w.Hello").getClassLoader() == loader1);
assertTrue(loader4.loadClass("p.Main").getClassLoader() == loader4);
assertTrue(loader4.loadClass("q.Hello").getClassLoader() == loader4);
assertTrue(loader4.loadClass("w.Hello").getClassLoader() == loader4);
}
/**
* Test Layer defineModulesWithXXX when the modules that override same
* named modules in the parent layer.
*
* Test scenario:
* layer1: m1, m2, m3 => loader pool
* layer2: m1, m2, m3 => loader pool
*/
public void testOverriding2() throws Exception {
Configuration cf1 = resolve("m1");
ModuleLayer layer1 = ModuleLayer.boot().defineModulesWithManyLoaders(cf1, null);
checkLayer(layer1, "m1", "m2", "m3");
ModuleFinder finder = ModuleFinder.of(MODS_DIR);
Configuration cf2 = cf1.resolve(finder, ModuleFinder.of(),
Set.of("m1"));
ModuleLayer layer2 = layer1.defineModulesWithManyLoaders(cf2, null);
checkLayer(layer2, "m1", "m2", "m3");
invoke(layer1, "m1", "p.Main");
ClassLoader loader1 = layer1.findLoader("m1");
ClassLoader loader2 = layer1.findLoader("m2");
ClassLoader loader3 = layer1.findLoader("m3");
ClassLoader loader4 = layer2.findLoader("m1");
ClassLoader loader5 = layer2.findLoader("m2");
ClassLoader loader6 = layer2.findLoader("m3");
assertTrue(loader4 != loader1);
assertTrue(loader5 != loader2);
assertTrue(loader6 != loader3);
assertTrue(loader1.loadClass("p.Main").getClassLoader() == loader1);
assertTrue(loader1.loadClass("q.Hello").getClassLoader() == loader2);
assertTrue(loader1.loadClass("w.Hello").getClassLoader() == loader3);
// p.Main is not visible via loader2
try {
loader2.loadClass("p.Main");
assertTrue(false);
} catch (ClassNotFoundException expected) { }
// w.Hello is not visible via loader2
try {
loader2.loadClass("w.Hello");
assertTrue(false);
} catch (ClassNotFoundException expected) { }
// p.Main is not visible via loader3
try {
loader3.loadClass("p.Main");
assertTrue(false);
} catch (ClassNotFoundException expected) { }
// q.Hello is not visible via loader3
try {
loader3.loadClass("q.Hello");
assertTrue(false);
} catch (ClassNotFoundException expected) { }
assertTrue(loader4.loadClass("p.Main").getClassLoader() == loader4);
assertTrue(loader5.loadClass("q.Hello").getClassLoader() == loader5);
assertTrue(loader6.loadClass("w.Hello").getClassLoader() == loader6);
// p.Main is not visible via loader5
try {
loader5.loadClass("p.Main");
assertTrue(false);
} catch (ClassNotFoundException expected) { }
// w.Hello is not visible via loader5
try {
loader5.loadClass("w.Hello");
assertTrue(false);
} catch (ClassNotFoundException expected) { }
// p.Main is not visible via loader6
try {
loader6.loadClass("p.Main");
assertTrue(false);
} catch (ClassNotFoundException expected) { }
// q.Hello is not visible via loader6
try {
loader6.loadClass("q.Hello");
assertTrue(false);
} catch (ClassNotFoundException expected) { }
}
/**
* Test ModuleLayer.defineModulesWithXXX when the modules that override same
* named modules in the parent layer.
*
* layer1: m1, m2, m3 => same loader
* layer2: m1, m3 => same loader
*/
public void testOverriding3() throws Exception {
Configuration cf1 = resolve("m1");
ModuleLayer layer1 = ModuleLayer.boot().defineModulesWithOneLoader(cf1, null);
checkLayer(layer1, "m1", "m2", "m3");
ModuleFinder finder = finderFor("m1", "m3");
Configuration cf2 = cf1.resolve(finder, ModuleFinder.of(),
Set.of("m1"));
ModuleLayer layer2 = layer1.defineModulesWithOneLoader(cf2, null);
checkLayer(layer2, "m1", "m3");
invoke(layer1, "m1", "p.Main");
ClassLoader loader1 = layer1.findLoader("m1");
ClassLoader loader2 = layer2.findLoader("m1");
assertTrue(loader1.loadClass("p.Main").getClassLoader() == loader1);
assertTrue(loader1.loadClass("q.Hello").getClassLoader() == loader1);
assertTrue(loader1.loadClass("w.Hello").getClassLoader() == loader1);
assertTrue(loader2.loadClass("p.Main").getClassLoader() == loader2);
assertTrue(loader2.loadClass("q.Hello").getClassLoader() == loader1);
assertTrue(loader2.loadClass("w.Hello").getClassLoader() == loader2);
}
/**
* Test Layer defineModulesWithXXX when the modules that override same
* named modules in the parent layer.
*
* layer1: m1, m2, m3 => loader pool
* layer2: m1, m3 => loader pool
*/
public void testOverriding4() throws Exception {
Configuration cf1 = resolve("m1");
ModuleLayer layer1 = ModuleLayer.boot().defineModulesWithManyLoaders(cf1, null);
checkLayer(layer1, "m1", "m2", "m3");
ModuleFinder finder = finderFor("m1", "m3");
Configuration cf2 = cf1.resolve(finder, ModuleFinder.of(),
Set.of("m1"));
ModuleLayer layer2 = layer1.defineModulesWithManyLoaders(cf2, null);
checkLayer(layer2, "m1", "m3");
invoke(layer1, "m1", "p.Main");
ClassLoader loader1 = layer1.findLoader("m1");
ClassLoader loader2 = layer1.findLoader("m2");
ClassLoader loader3 = layer1.findLoader("m3");
ClassLoader loader4 = layer2.findLoader("m1");
ClassLoader loader5 = layer2.findLoader("m2");
ClassLoader loader6 = layer2.findLoader("m3");
assertTrue(loader4 != loader1);
assertTrue(loader5 == loader2); // m2 not overridden
assertTrue(loader6 != loader3);
assertTrue(loader1.loadClass("p.Main").getClassLoader() == loader1);
assertTrue(loader1.loadClass("q.Hello").getClassLoader() == loader2);
assertTrue(loader1.loadClass("w.Hello").getClassLoader() == loader3);
assertTrue(loader2.loadClass("q.Hello").getClassLoader() == loader2);
assertTrue(loader3.loadClass("w.Hello").getClassLoader() == loader3);
assertTrue(loader4.loadClass("p.Main").getClassLoader() == loader4);
assertTrue(loader4.loadClass("q.Hello").getClassLoader() == loader2);
assertTrue(loader4.loadClass("w.Hello").getClassLoader() == loader6);
assertTrue(loader6.loadClass("w.Hello").getClassLoader() == loader6);
}
/**
* Basic test for locating resources with a class loader created by
* defineModulesWithOneLoader.
*/
public void testResourcesWithOneLoader() throws Exception {
testResourcesWithOneLoader(ClassLoader.getSystemClassLoader());
testResourcesWithOneLoader(null);
}
/**
* Test locating resources with the class loader created by
* defineModulesWithOneLoader. The class loader has the given class
* loader as its parent.
*/
void testResourcesWithOneLoader(ClassLoader parent) throws Exception {
Configuration cf = resolve("m1");
ModuleLayer layer = ModuleLayer.boot().defineModulesWithOneLoader(cf, parent);
ClassLoader loader = layer.findLoader("m1");
assertNotNull(loader);
assertTrue(loader.getParent() == parent);
// check that getResource and getResources are consistent
URL url1 = loader.getResource("module-info.class");
URL url2 = loader.getResources("module-info.class").nextElement();
assertEquals(url1.toURI(), url2.toURI());
// use getResources to find module-info.class resources
Enumeration<URL> urls = loader.getResources("module-info.class");
List<String> list = readModuleNames(urls);
// m1, m2, ... should be first (order not specified)
int count = cf.modules().size();
cf.modules().stream()
.map(ResolvedModule::name)
.forEach(mn -> assertTrue(list.indexOf(mn) < count));
// java.base should be after m1, m2, ...
assertTrue(list.indexOf("java.base") >= count);
// check resources(String)
List<String> list2 = loader.resources("module-info.class")
.map(this::readModuleName)
.collect(Collectors.toList());
assertEquals(list2, list);
// check nulls
try {
loader.getResource(null);
assertTrue(false);
} catch (NullPointerException e) { }
try {
loader.getResources(null);
assertTrue(false);
} catch (NullPointerException e) { }
try {
loader.resources(null);
assertTrue(false);
} catch (NullPointerException e) { }
}
/**
* Basic test for locating resources with class loaders created by
* defineModulesWithManyLoaders.
*/
public void testResourcesWithManyLoaders() throws Exception {
testResourcesWithManyLoaders(ClassLoader.getSystemClassLoader());
testResourcesWithManyLoaders(null);
}
/**
* Test locating resources with class loaders created by
* defineModulesWithManyLoaders. The class loaders have the given class
* loader as their parent.
*/
void testResourcesWithManyLoaders(ClassLoader parent) throws Exception {
Configuration cf = resolve("m1");
ModuleLayer layer = ModuleLayer.boot().defineModulesWithManyLoaders(cf, parent);
for (Module m : layer.modules()) {
String name = m.getName();
ClassLoader loader = m.getClassLoader();
assertNotNull(loader);
assertTrue(loader.getParent() == parent);
// getResource should find the module-info.class for the module
URL url = loader.getResource("module-info.class");
assertEquals(readModuleName(url), name);
// list of modules names read from module-info.class
Enumeration<URL> urls = loader.getResources("module-info.class");
List<String> list = readModuleNames(urls);
// module should be the first element
assertTrue(list.indexOf(name) == 0);
// the module-info.class for the other modules in the layer
// should not be found
layer.modules().stream()
.map(Module::getName)
.filter(mn -> !mn.equals(name))
.forEach(mn -> assertTrue(list.indexOf(mn) < 0));
// java.base cannot be the first element
assertTrue(list.indexOf("java.base") > 0);
// check resources(String)
List<String> list2 = loader.resources("module-info.class")
.map(this::readModuleName)
.collect(Collectors.toList());
assertEquals(list2, list);
// check nulls
try {
loader.getResource(null);
assertTrue(false);
} catch (NullPointerException e) { }
try {
loader.getResources(null);
assertTrue(false);
} catch (NullPointerException e) { }
try {
loader.resources(null);
assertTrue(false);
} catch (NullPointerException e) { }
}
}
private List<String> readModuleNames(Enumeration<URL> e) {
List<String> list = new ArrayList<>();
while (e.hasMoreElements()) {
URL url = e.nextElement();
list.add(readModuleName(url));
}
return list;
}
private String readModuleName(URL url) {
try (InputStream in = url.openStream()) {
ModuleDescriptor descriptor = ModuleDescriptor.read(in);
return descriptor.name();
} catch (IOException ioe) {
throw new UncheckedIOException(ioe);
}
}
// -- supporting methods --
/**
* Resolve the given modules, by name, and returns the resulting
* Configuration.
*/
private static Configuration resolve(String... roots) {
ModuleFinder finder = ModuleFinder.of(MODS_DIR);
return ModuleLayer.boot()
.configuration()
.resolve(finder, ModuleFinder.of(), Set.of(roots));
}
/**
* Resolve the given modules, by name, and returns the resulting
* Configuration.
*/
private static Configuration resolveAndBind(String... roots) {
ModuleFinder finder = ModuleFinder.of(MODS_DIR);
return ModuleLayer.boot()
.configuration()
.resolveAndBind(finder, ModuleFinder.of(), Set.of(roots));
}
/**
* Invokes the static void main(String[]) method on the given class
* in the given module.
*/
private static void invoke(ModuleLayer layer, String mn, String mc) throws Exception {
ClassLoader loader = layer.findLoader(mn);
Class<?> c = loader.loadClass(mc);
Method mainMethod = c.getMethod("main", String[].class);
mainMethod.invoke(null, (Object)new String[0]);
}
/**
* Checks that the given layer contains exactly the expected modules
* (by name).
*/
private void checkLayer(ModuleLayer layer, String ... expected) {
Set<String> names = layer.modules().stream()
.map(Module::getName)
.collect(Collectors.toSet());
assertTrue(names.size() == expected.length);
for (String name : expected) {
assertTrue(names.contains(name));
}
}
/**
* Test that a class can be loaded via the class loader of all modules
* in the given layer.
*/
static void testLoad(ModuleLayer layer, String cn) throws Exception {
for (Module m : layer.modules()) {
ClassLoader l = m.getClassLoader();
l.loadClass(cn);
}
}
/**
* Test that a class cannot be loaded via any of the class loaders of
* the modules in the given layer.
*/
static void testLoadFail(ModuleLayer layer, String cn) throws Exception {
for (Module m : layer.modules()) {
ClassLoader l = m.getClassLoader();
try {
l.loadClass(cn);
assertTrue(false);
} catch (ClassNotFoundException expected) { }
}
}
/**
* Returns a ModuleFinder that only finds the given test modules
*/
static ModuleFinder finderFor(String... names) {
ModuleFinder finder = ModuleFinder.of(MODS_DIR);
Map<String, ModuleReference> mrefs = new HashMap<>();
for (String name : names) {
Optional<ModuleReference> omref = finder.find(name);
assert omref.isPresent();
mrefs.put(name, omref.get());
}
return new ModuleFinder() {
@Override
public Optional<ModuleReference> find(String name) {
ModuleReference mref = mrefs.get(name);
return Optional.ofNullable(mref);
}
@Override
public Set<ModuleReference> findAll() {
return mrefs.values().stream().collect(Collectors.toSet());
}
};
}
}

View file

@ -0,0 +1,261 @@
/*
* Copyright (c) 2016, 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.
*/
/**
* @test
* @library /test/lib
* @build LayerControllerTest
* jdk.test.lib.util.ModuleUtils
* @run testng LayerControllerTest
* @summary Basic tests for java.lang.ModuleLayer.Controller
*/
import java.lang.module.Configuration;
import java.lang.module.ModuleDescriptor;
import java.lang.module.ModuleFinder;
import java.util.List;
import java.util.Set;
import jdk.test.lib.util.ModuleUtils;
import org.testng.annotations.Test;
import static org.testng.Assert.*;
@Test
public class LayerControllerTest {
/**
* Creates a Controller for a module layer containing modules m1 and m2.
* Module m1 contains p1, reads java.base, does not export/open any package
* Module m2 contains p2, reads java.base, does not export/open any package
*/
private ModuleLayer.Controller createTestLayer() {
ModuleDescriptor descriptor1
= ModuleDescriptor.newModule("m1")
.packages(Set.of("p1"))
.requires("java.base")
.build();
ModuleDescriptor descriptor2
= ModuleDescriptor.newModule("m2")
.requires("java.base")
.packages(Set.of("p2"))
.build();
ModuleDescriptor descriptor3
= ModuleDescriptor.newModule("m3")
.requires("java.base")
.packages(Set.of("p3"))
.build();
ModuleFinder finder = ModuleUtils.finderOf(descriptor1, descriptor2, descriptor3);
ModuleLayer bootLayer = ModuleLayer.boot();
Configuration cf = bootLayer.configuration()
.resolve(finder, ModuleFinder.of(), Set.of("m1", "m2", "m3"));
ClassLoader scl = ClassLoader.getSystemClassLoader();
ModuleLayer.Controller controller
= ModuleLayer.defineModulesWithOneLoader(cf, List.of(bootLayer), scl);
ModuleLayer layer = controller.layer();
assertTrue(layer.modules().size() == 3);
assertTrue(layer.findModule("m1").isPresent());
assertTrue(layer.findModule("m2").isPresent());
assertTrue(layer.findModule("m3").isPresent());
return controller;
}
/**
* Basic test of Controller to update modules m1 and m2 to read and
* open packages to each other.
*/
public void testBasic() {
ModuleLayer.Controller controller = createTestLayer();
ModuleLayer layer = controller.layer();
Module m1 = layer.findModule("m1").orElseThrow(RuntimeException::new);
Module m2 = layer.findModule("m2").orElseThrow(RuntimeException::new);
Module m3 = layer.findModule("m3").orElseThrow(RuntimeException::new);
assertFalse(m1.canRead(m2));
assertFalse(m1.canRead(m3));
assertFalse(m1.isExported("p1"));
assertFalse(m1.isOpen("p1"));
assertFalse(m1.isExported("p1", m2));
assertFalse(m1.isExported("p1", m3));
assertFalse(m1.isOpen("p1", m2));
assertFalse(m1.isOpen("p1", m3));
assertFalse(m2.canRead(m1));
assertFalse(m2.canRead(m3));
assertFalse(m2.isExported("p2"));
assertFalse(m2.isOpen("p2"));
assertFalse(m2.isExported("p2", m1));
assertFalse(m2.isExported("p2", m3));
assertFalse(m2.isOpen("p2", m1));
assertFalse(m2.isOpen("p2", m3));
assertFalse(m3.canRead(m1));
assertFalse(m3.canRead(m2));
assertFalse(m3.isExported("p3"));
assertFalse(m3.isOpen("p3"));
assertFalse(m3.isExported("p3", m1));
assertFalse(m3.isExported("p3", m2));
assertFalse(m3.isOpen("p3", m1));
assertFalse(m3.isOpen("p3", m2));
// update m1 to read m2
assertTrue(controller.addReads(m1, m2) == controller);
assertTrue(m1.canRead(m2));
assertFalse(m2.canRead(m1));
// update m2 to read m1
assertTrue(controller.addReads(m2, m1) == controller);
assertTrue(m1.canRead(m2));
assertTrue(m1.canRead(m1));
// update m1 to export p1 to m2
assertTrue(controller.addExports(m1, "p1", m2) == controller);
assertTrue(m1.isExported("p1", m2));
assertFalse(m1.isOpen("p1", m2));
assertFalse(m1.isExported("p1"));
assertFalse(m1.isOpen("p1"));
// update m3 to open p3 to m1
assertTrue(controller.addExports(m3, "p3", m1) == controller);
assertTrue(m3.isExported("p3", m1));
assertFalse(m3.isOpen("p3", m1));
assertFalse(m3.isExported("p3"));
assertFalse(m3.isOpen("p3"));
// update m1 to open p1 to m3
assertTrue(controller.addOpens(m1, "p1", m3) == controller);
assertTrue(m1.isExported("p1", m3));
assertTrue(m1.isOpen("p1", m3));
assertFalse(m1.isExported("p1"));
assertFalse(m1.isOpen("p1"));
// update m3 to open p3 to m1
assertTrue(controller.addOpens(m3, "p3", m1) == controller);
assertTrue(m3.isExported("p3", m1));
assertTrue(m3.isOpen("p3", m1));
assertFalse(m3.isExported("p3"));
assertFalse(m3.isOpen("p3"));
}
/**
* Test invalid argument handling
*/
public void testBadArguments() {
ModuleLayer.Controller controller = createTestLayer();
ModuleLayer layer = controller.layer();
Module m1 = layer.findModule("m1").orElseThrow(RuntimeException::new);
Module m2 = layer.findModule("m2").orElseThrow(RuntimeException::new);
Module base = Object.class.getModule();
// java.base is not in layer
try {
controller.addReads(base, m2);
assertTrue(false);
} catch (IllegalArgumentException expected) { }
// java.base is not in layer
try {
controller.addExports(base, "java.lang", m2);
assertTrue(false);
} catch (IllegalArgumentException expected) { }
// m1 does not contain java.lang
try {
controller.addExports(m1, "java.lang", m2);
assertTrue(false);
} catch (IllegalArgumentException expected) { }
// java.base is not in layer
try {
controller.addOpens(base, "java.lang", m2);
assertTrue(false);
} catch (IllegalArgumentException expected) { }
// m1 does not contain java.lang
try {
controller.addOpens(m1, "java.lang", m2);
assertTrue(false);
} catch (IllegalArgumentException expected) { }
}
/**
* Test null handling
*/
public void testNulls() {
ModuleLayer.Controller controller = createTestLayer();
ModuleLayer layer = controller.layer();
Module m1 = layer.findModule("m1").orElseThrow(RuntimeException::new);
Module m2 = layer.findModule("m2").orElseThrow(RuntimeException::new);
assertTrue(m1 != null);
assertTrue(m2 != null);
try {
controller.addReads(null, m2);
assertTrue(false);
} catch (NullPointerException expected) { }
try {
controller.addReads(m1, null);
assertTrue(false);
} catch (NullPointerException expected) { }
try {
controller.addExports(null, "p1", m2);
assertTrue(false);
} catch (NullPointerException expected) { }
try {
controller.addExports(m1, null, m2);
assertTrue(false);
} catch (NullPointerException expected) { }
try {
controller.addExports(m1, "p1", null);
assertTrue(false);
} catch (NullPointerException expected) { }
try {
controller.addOpens(null, "p1", m2);
assertTrue(false);
} catch (NullPointerException expected) { }
try {
controller.addOpens(m1, null, m2);
assertTrue(false);
} catch (NullPointerException expected) { }
try {
controller.addOpens(m1, "p1", null);
assertTrue(false);
} catch (NullPointerException expected) { }
}
}

View file

@ -0,0 +1,152 @@
/*
* Copyright (c) 2018, 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.
*/
/**
* @test
* @bug 8211825
* @modules jdk.compiler
* @library /test/lib
* @build jdk.test.lib.compiler.CompilerUtils jdk.test.lib.util.JarUtils
* @run testng/othervm AutomaticModulesTest
* @summary Tests automatic modules in module layers
*/
import java.nio.file.Files;
import java.nio.file.Path;
import java.lang.ModuleLayer.Controller;
import java.lang.module.*;
import java.lang.reflect.Method;
import java.util.List;
import java.util.Set;
import jdk.test.lib.compiler.CompilerUtils;
import jdk.test.lib.util.JarUtils;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import static org.testng.Assert.*;
/**
* This test uses two modules:
* m requires alib and has an entry point p.Main
* alib is an automatic module
*/
@Test
public class AutomaticModulesTest {
private static final String TEST_SRC = System.getProperty("test.src");
private static final Path CLASSES = Path.of("classes");
private static final Path LIB = Path.of("lib");
private static final Path MODS = Path.of("mods");
@BeforeTest
public void setup() throws Exception {
// javac -d classes src/alib/**
// jar cf lib/alib.jar -C classes .
Files.createDirectory(CLASSES);
assertTrue(CompilerUtils.compile(Path.of(TEST_SRC, "src", "alib"), CLASSES));
JarUtils.createJarFile(LIB.resolve("alib.jar"), CLASSES);
// javac -p lib -d mods/m - src/m/**
Path src = Path.of(TEST_SRC, "src", "m");
Path output = Files.createDirectories(MODS.resolve("m"));
assertTrue(CompilerUtils.compile(src, output, "-p", LIB.toString()));
}
/**
* Create a module layer with modules m and alib mapped to the same class
* loader.
*/
public void testOneLoader() throws Exception {
Configuration cf = ModuleLayer.boot()
.configuration()
.resolve(ModuleFinder.of(), ModuleFinder.of(MODS, LIB), Set.of("m"));
ResolvedModule m = cf.findModule("m").orElseThrow();
ResolvedModule alib = cf.findModule("alib").orElseThrow();
assertTrue(m.reads().contains(alib));
assertTrue(alib.reference().descriptor().isAutomatic());
ModuleLayer bootLayer = ModuleLayer.boot();
ClassLoader scl = ClassLoader.getSystemClassLoader();
Controller controller = ModuleLayer.defineModulesWithOneLoader(cf, List.of(bootLayer), scl);
invokeMain(controller, "m/p.Main");
}
/**
* Create a module layer with modules m and alib mapped to different class
* loaders. This will test that L(m) delegates to L(alib) in the same layer.
*/
public void testManyLoaders() throws Exception {
Configuration cf = ModuleLayer.boot()
.configuration()
.resolve(ModuleFinder.of(), ModuleFinder.of(MODS, LIB), Set.of("m"));
ResolvedModule m = cf.findModule("m").orElseThrow();
ResolvedModule alib = cf.findModule("alib").orElseThrow();
assertTrue(m.reads().contains(alib));
assertTrue(alib.reference().descriptor().isAutomatic());
ModuleLayer bootLayer = ModuleLayer.boot();
ClassLoader scl = ClassLoader.getSystemClassLoader();
Controller controller = ModuleLayer.defineModulesWithManyLoaders(cf, List.of(bootLayer), scl);
invokeMain(controller, "m/p.Main");
}
/**
* Create a module layer with alib and another module layer with m.
* This will test that L(m) delegates to L(alib) in a parent layer.
*/
public void testAutomaticModuleInParent() throws Exception {
ModuleLayer bootLayer = ModuleLayer.boot();
ClassLoader scl = ClassLoader.getSystemClassLoader();
// configuration/layer containing alib
Configuration cf1 = bootLayer
.configuration()
.resolve(ModuleFinder.of(), ModuleFinder.of(LIB), Set.of("alib"));
ModuleLayer layer1 = bootLayer.defineModulesWithOneLoader(cf1, scl);
// configuration/layer containing m
Configuration cf2 = cf1.resolve(ModuleFinder.of(), ModuleFinder.of(MODS), Set.of("m"));
Controller controller = ModuleLayer.defineModulesWithOneLoader(cf2, List.of(layer1), scl);
invokeMain(controller, "m/p.Main");
}
/**
* Invokes the main method of the given entry point (module-name/class-name)
*/
private void invokeMain(Controller controller, String entry) throws Exception {
String[] s = entry.split("/");
String moduleName = s[0];
String className = s[1];
int pos = className.lastIndexOf('.');
String packageName = className.substring(0, pos);
ModuleLayer layer = controller.layer();
Module module = layer.findModule(moduleName).orElseThrow();
controller.addExports(module, packageName, this.getClass().getModule());
ClassLoader loader = layer.findLoader(moduleName);
Class<?> c = loader.loadClass(className);
Method m = c.getMethod("main", String[].class);
m.invoke(null, (Object)new String[0]);
}
}

View file

@ -0,0 +1,31 @@
/*
* Copyright (c) 2018, 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.
*/
package q;
public class Lib {
private Lib() { }
public static String generate() {
return "something useful";
}
}

View file

@ -0,0 +1,25 @@
/*
* Copyright (c) 2018, 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.
*/
module m {
requires alib;
}

View file

@ -0,0 +1,32 @@
/*
* Copyright (c) 2018, 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.
*/
package p;
import q.Lib;
public class Main {
public static void main(String[] args) {
String s = Lib.generate();
System.out.println(s);
}
}

View file

@ -0,0 +1,31 @@
/*
* Copyright (c) 2015, 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.
*/
/**
* Supporting class for tests of java.lang.ModuleLayer.
*/
package layertest;
public class Test { }

View file

@ -0,0 +1,29 @@
/*
* Copyright (c) 2015, 2016, 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.
*/
module m1 {
requires m2;
requires m3;
exports p;
uses p.Service;
}

View file

@ -0,0 +1,39 @@
/*
* Copyright (c) 2015, 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.
*/
package p;
import java.net.URL;
public class Main {
public static void main(String[] args) {
URL url1 = Main.class.getResource("Main.class");
if (url1 == null) throw new RuntimeException();
URL url2 = Main.class.getResource("/p/Main.class");
if (url2 == null) throw new RuntimeException();
q.Hello.hello();
w.Hello.hello();
}
}

View file

@ -0,0 +1,26 @@
/*
* Copyright (c) 2015, 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.
*/
package p;
public interface Service { }

View file

@ -0,0 +1,26 @@
/*
* Copyright (c) 2015, 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.
*/
module m2 {
exports q;
}

View file

@ -0,0 +1,40 @@
/*
* Copyright (c) 2015, 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.
*/
package q;
import java.net.URL;
public class Hello {
public static void hello() {
URL url1 = Hello.class.getResource("Hello.class");
if (url1 == null) throw new RuntimeException();
URL url2 = Hello.class.getResource("/q/Hello.class");
if (url2 == null) throw new RuntimeException();
System.out.println("Hello!");
}
}

View file

@ -0,0 +1,27 @@
/*
* Copyright (c) 2015, 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.
*/
module m3 {
// qualified export
exports w to m1;
}

View file

@ -0,0 +1,40 @@
/*
* Copyright (c) 2015, 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.
*/
package w;
import java.net.URL;
public class Hello {
public static void hello() {
URL url1 = Hello.class.getResource("Hello.class");
if (url1 == null) throw new RuntimeException();
URL url2 = Hello.class.getResource("/w/Hello.class");
if (url2 == null) throw new RuntimeException();
System.out.println("Hello!");
}
}

View file

@ -0,0 +1,27 @@
/*
* Copyright (c) 2015, 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.
*/
package impl;
public class ServiceImpl implements p.Service {
}

View file

@ -0,0 +1,27 @@
/*
* Copyright (c) 2015, 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.
*/
module m4 {
requires m1;
provides p.Service with impl.ServiceImpl;
}