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

View file

@ -0,0 +1,126 @@
/*
* 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
* @modules java.base/jdk.internal.misc
* java.desktop
* @run main/othervm --add-exports=java.desktop/sun.awt=java.base AddExportsTest
* @run main/othervm --add-exports=java.desktop/sun.awt=ALL-UNNAMED AddExportsTest
* @summary Test Module isExported methods with exports changed by -AddExportsTest
*/
import java.util.Optional;
import java.util.stream.Stream;
import jdk.internal.misc.VM;
public class AddExportsTest {
/*
* jtreg sets -Dtest.modules system property to the internal APIs
* specified at @modules tag. The test will exclude --add-exports set
* for @modules.
*/
private static final String TEST_MODULES = System.getProperty("test.modules");
public static void main(String[] args) {
Optional<String> oaddExports = Stream.of(VM.getRuntimeArguments())
.filter(arg -> arg.startsWith("--add-exports="))
.filter(arg -> !arg.equals("--add-exports=" + TEST_MODULES + "=ALL-UNNAMED"))
.map(arg -> arg.substring("--add-exports=".length(), arg.length()))
.findFirst();
assertTrue(oaddExports.isPresent());
ModuleLayer bootLayer = ModuleLayer.boot();
Module unnamedModule = AddExportsTest.class.getModule();
assertFalse(unnamedModule.isNamed());
for (String expr : oaddExports.get().split(",")) {
String[] s = expr.split("=");
assertTrue(s.length == 2);
// $MODULE/$PACKAGE
String[] moduleAndPackage = s[0].split("/");
assertTrue(moduleAndPackage.length == 2);
String mn = moduleAndPackage[0];
String pn = moduleAndPackage[1];
// source module
Module source;
Optional<Module> om = bootLayer.findModule(mn);
assertTrue(om.isPresent(), mn + " not in boot layer");
source = om.get();
// package should not be exported unconditionally
assertFalse(source.isExported(pn),
pn + " should not be exported unconditionally");
// $TARGET
String tn = s[1];
if ("ALL-UNNAMED".equals(tn)) {
// package is exported to all unnamed modules
assertTrue(source.isExported(pn, unnamedModule),
pn + " should be exported to all unnamed modules");
} else {
om = bootLayer.findModule(tn);
assertTrue(om.isPresent());
Module target = om.get();
// package should be exported to target module
assertTrue(source.isExported(pn, target),
pn + " should be exported to " + target);
// package should not be exported to unnamed modules
assertFalse(source.isExported(pn, unnamedModule),
pn + " should not be exported to unnamed modules");
}
}
}
static void assertTrue(boolean cond) {
if (!cond) throw new RuntimeException();
}
static void assertTrue(boolean cond, String msg) {
if (!cond) throw new RuntimeException(msg);
}
static void assertFalse(boolean cond) {
if (cond) throw new RuntimeException();
}
static void assertFalse(boolean cond, String msg) {
if (cond) throw new RuntimeException(msg);
}
}

View file

@ -0,0 +1,207 @@
/*
* Copyright (c) 2016, 2024, 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.
*/
import java.io.IOException;
import java.io.OutputStream;
import java.lang.annotation.Annotation;
import java.lang.classfile.AnnotationElement;
import java.lang.classfile.AnnotationValue;
import java.lang.classfile.ClassBuilder;
import java.lang.classfile.ClassElement;
import java.lang.classfile.ClassFile;
import java.lang.classfile.ClassTransform;
import java.lang.classfile.attribute.RuntimeVisibleAnnotationsAttribute;
import java.lang.module.Configuration;
import java.lang.module.ModuleDescriptor;
import java.lang.module.ModuleFinder;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import jdk.test.lib.util.ModuleInfoWriter;
import org.testng.annotations.Test;
import static org.testng.Assert.*;
/**
* @test
* @modules java.base/jdk.internal.module
* @library /test/lib
* @build jdk.test.lib.util.ModuleInfoWriter
* @run testng AnnotationsTest
* @summary Basic test of annotations on modules
*/
public class AnnotationsTest {
/**
* Test that there are no annotations on an unnamed module.
*/
@Test
public void testUnnamedModule() {
Module module = this.getClass().getModule();
assertTrue(module.getAnnotations().length == 0);
assertTrue(module.getDeclaredAnnotations().length == 0);
}
/**
* Test reflectively reading the annotations on a named module.
*/
@Test
public void testNamedModule() throws IOException {
Path mods = Files.createTempDirectory(Path.of(""), "mods");
// @Deprecated(since="9", forRemoval=true) module foo { }
ModuleDescriptor descriptor = ModuleDescriptor.newModule("foo").build();
byte[] classBytes = ModuleInfoWriter.toBytes(descriptor);
classBytes = addDeprecated(classBytes, true, "9");
Files.write(mods.resolve("module-info.class"), classBytes);
// create module layer with module foo
Module module = loadModule(mods, "foo");
// check the annotation is present
assertTrue(module.isAnnotationPresent(Deprecated.class));
Deprecated d = module.getAnnotation(Deprecated.class);
assertNotNull(d, "@Deprecated not found");
assertTrue(d.forRemoval());
assertEquals(d.since(), "9");
Annotation[] a = module.getAnnotations();
assertTrue(a.length == 1);
assertTrue(a[0] instanceof Deprecated);
assertEquals(module.getDeclaredAnnotations(), a);
}
/**
* Test reflectively reading annotations on a named module where the module
* is mapped to a class loader that can locate a module-info.class.
*/
@Test
public void testWithModuleInfoResourceXXXX() throws IOException {
Path mods = Files.createTempDirectory(Path.of(""), "mods");
// classes directory with module-info.class
Path classes = Files.createTempDirectory(Path.of("."), "classes");
Path mi = classes.resolve("module-info.class");
try (OutputStream out = Files.newOutputStream(mi)) {
ModuleDescriptor descriptor = ModuleDescriptor.newModule("lurker").build();
ModuleInfoWriter.write(descriptor, out);
}
// URLClassLoader that can locate a module-info.class resource
URL url = classes.toUri().toURL();
URLClassLoader loader = new URLClassLoader(new URL[] { url });
assertTrue(loader.findResource("module-info.class") != null);
// module foo { }
ModuleDescriptor descriptor = ModuleDescriptor.newModule("foo").build();
byte[] classBytes = ModuleInfoWriter.toBytes(descriptor);
Files.write(mods.resolve("module-info.class"), classBytes);
// create module layer with module foo
Module foo = loadModule(mods, "foo", loader);
// check the annotation is not present
assertFalse(foo.isAnnotationPresent(Deprecated.class));
// @Deprecated(since="11", forRemoval=true) module bar { }
descriptor = ModuleDescriptor.newModule("bar").build();
classBytes = ModuleInfoWriter.toBytes(descriptor);
classBytes = addDeprecated(classBytes, true, "11");
Files.write(mods.resolve("module-info.class"), classBytes);
// create module layer with module bar
Module bar = loadModule(mods, "bar", loader);
// check the annotation is present
assertTrue(bar.isAnnotationPresent(Deprecated.class));
}
/**
* Adds the Deprecated annotation to the given module-info class file.
*/
static byte[] addDeprecated(byte[] bytes, boolean forRemoval, String since) {
var cf = ClassFile.of();
var oldModel = cf.parse(bytes);
return cf.transformClass(oldModel, new ClassTransform() {
boolean rvaaFound = false;
@Override
public void accept(ClassBuilder builder, ClassElement element) {
if (!rvaaFound && element instanceof RuntimeVisibleAnnotationsAttribute rvaa) {
rvaaFound = true;
var res = new ArrayList<java.lang.classfile.Annotation>(rvaa.annotations().size() + 1);
res.addAll(rvaa.annotations());
res.add(createDeprecated());
builder.accept(RuntimeVisibleAnnotationsAttribute.of(res));
return;
}
builder.accept(element);
}
@Override
public void atEnd(ClassBuilder builder) {
if (!rvaaFound) {
builder.accept(RuntimeVisibleAnnotationsAttribute.of(List.of(createDeprecated())));
}
}
private java.lang.classfile.Annotation createDeprecated() {
return java.lang.classfile.Annotation.of(
Deprecated.class.describeConstable().orElseThrow(),
AnnotationElement.of("forRemoval", AnnotationValue.ofBoolean(forRemoval)),
AnnotationElement.of("since", AnnotationValue.ofString(since))
);
}
});
}
/**
* Load the module of the given name in the given directory into a
* child layer with the given class loader as the parent class loader.
*/
static Module loadModule(Path dir, String name, ClassLoader parent)
throws IOException
{
ModuleFinder finder = ModuleFinder.of(dir);
ModuleLayer bootLayer = ModuleLayer.boot();
Configuration cf = bootLayer.configuration()
.resolve(finder, ModuleFinder.of(), Set.of(name));
ModuleLayer layer = bootLayer.defineModulesWithOneLoader(cf, parent);
Module module = layer.findModule(name).orElse(null);
assertNotNull(module, name + " not loaded");
return module;
}
static Module loadModule(Path dir, String name) throws IOException {
return loadModule(dir, name, ClassLoader.getSystemClassLoader());
}
}

View file

@ -0,0 +1,329 @@
/*
* Copyright (c) 2014, 2021, 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.
*/
import java.lang.module.ModuleDescriptor.Exports;
import java.lang.module.ResolvedModule;
import java.nio.file.spi.FileSystemProvider; // service type in java.base
import java.util.function.Predicate;
import java.util.stream.Stream;
import javax.print.PrintServiceLookup; // service type in java.desktop
import org.testng.annotations.Test;
import static org.testng.Assert.*;
/*
* @test
* @summary Basic test of java.lang.Module
* @modules java.desktop java.xml
* @run testng/othervm BasicModuleTest
*/
public class BasicModuleTest {
/**
* Tests that the given module reads all modules in the boot layer.
*/
private void testReadsAllBootModules(Module m) {
ModuleLayer bootLayer = ModuleLayer.boot();
bootLayer.configuration()
.modules()
.stream()
.map(ResolvedModule::name)
.map(bootLayer::findModule)
.forEach(target -> assertTrue(m.canRead(target.get())));
}
/**
* Returns a {@code Predicate} to test if a package is exported.
*/
private Predicate<Exports> doesExport(String pn) {
return e -> (e.source().equals(pn) && !e.isQualified());
}
@Test
public void testThisModule() {
Module thisModule = BasicModuleTest.class.getModule();
Module baseModule = Object.class.getModule();
assertFalse(thisModule.isNamed());
assertTrue(thisModule.getName() == null);
assertTrue(thisModule.getDescriptor() == null);
assertTrue(thisModule.getLayer() == null);
assertTrue(thisModule.toString().startsWith("unnamed module "));
ClassLoader thisLoader = BasicModuleTest.class.getClassLoader();
assertTrue(thisLoader == thisModule.getClassLoader());
assertTrue(thisLoader.getUnnamedModule() == thisModule);
// unnamed modules read all other modules
ClassLoader cl;
cl = ClassLoader.getPlatformClassLoader();
assertTrue(thisModule.canRead(cl.getUnnamedModule()));
cl = ClassLoader.getSystemClassLoader();
assertTrue(thisModule.canRead(cl.getUnnamedModule()));
testReadsAllBootModules(thisModule);
// unnamed modules export all packages
assertTrue(thisModule.isExported(""));
assertTrue(thisModule.isExported("", thisModule));
assertTrue(thisModule.isExported("", baseModule));
assertTrue(thisModule.isExported("p"));
assertTrue(thisModule.isExported("p", thisModule));
assertTrue(thisModule.isExported("p", baseModule));
// this test is in the unnamed package
assertTrue(thisModule.getPackages().contains(""));
}
@Test
public void testUnnamedModules() {
Module thisModule = BasicModuleTest.class.getModule();
Module baseModule = Object.class.getModule();
ClassLoader loader1 = ClassLoader.getSystemClassLoader();
ClassLoader loader2 = loader1.getParent();
Module m1 = loader1.getUnnamedModule();
Module m2 = loader2.getUnnamedModule();
assertTrue(m1 != m2);
assertFalse(m1.isNamed());
assertFalse(m2.isNamed());
assertTrue(m1.getLayer() == null);
assertTrue(m2.getLayer() == null);
assertTrue(m1.toString().startsWith("unnamed module "));
assertTrue(m2.toString().startsWith("unnamed module "));
// unnamed module reads all modules
assertTrue(m1.canRead(m2));
assertTrue(m2.canRead(m1));
testReadsAllBootModules(m1);
testReadsAllBootModules(m2);
assertTrue(m1.isExported(""));
assertTrue(m1.isExported("", thisModule));
assertTrue(m1.isExported("", baseModule));
assertTrue(m1.isExported("p"));
assertTrue(m1.isExported("p", thisModule));
assertTrue(m1.isExported("p", baseModule));
}
@Test
public void testBaseModule() {
Module base = Object.class.getModule();
Module thisModule = BasicModuleTest.class.getModule();
// getName
assertTrue(base.getName().equals("java.base"));
// getDescriptor
assertTrue(base.getDescriptor().exports().stream()
.anyMatch(doesExport("java.lang")));
// getClassLoader
assertTrue(base.getClassLoader() == null);
// getLayer
assertTrue(base.getLayer() == ModuleLayer.boot());
// toString
assertEquals(base.toString(), "module java.base");
// getPackages
assertTrue(base.getPackages().contains("java.lang"));
// canRead
assertTrue(base.canRead(base));
assertFalse(base.canRead(thisModule));
// addReads
try {
base.addReads(thisModule);
assertTrue(false);
} catch (IllegalCallerException expected) { }
assertFalse(base.canRead(thisModule));
// isExported
assertTrue(base.isExported("java.lang"));
assertTrue(base.isExported("java.lang", thisModule));
assertTrue(base.isExported("java.lang", base));
assertFalse(base.isExported("jdk.internal.misc"));
assertFalse(base.isExported("jdk.internal.misc", thisModule));
assertTrue(base.isExported("jdk.internal.misc", base));
assertFalse(base.isExported("java.wombat"));
assertFalse(base.isExported("java.wombat", thisModule));
assertFalse(base.isExported("java.wombat", base));
// addExports
try {
base.addExports("java.lang", thisModule);
assertTrue(false);
} catch (IllegalCallerException expected) { }
try {
base.addExports("jdk.internal.misc", thisModule);
assertTrue(false);
} catch (IllegalCallerException expected) { }
assertFalse(base.isExported("jdk.internal.misc"));
assertFalse(base.isExported("jdk.internal.misc", thisModule));
// isOpen
assertFalse(base.isOpen("java.lang"));
assertFalse(base.isOpen("java.lang", thisModule));
assertTrue(base.isOpen("java.lang", base));
assertFalse(base.isOpen("jdk.internal.misc"));
assertFalse(base.isOpen("jdk.internal.misc", thisModule));
assertTrue(base.isOpen("jdk.internal.misc", base));
assertFalse(base.isOpen("java.wombat"));
assertFalse(base.isOpen("java.wombat", thisModule));
assertFalse(base.isOpen("java.wombat", base));
// addOpens
try {
base.addOpens("jdk.internal.misc", thisModule);
assertTrue(false);
} catch (IllegalCallerException expected) { }
assertFalse(base.isOpen("jdk.internal.misc"));
assertFalse(base.isOpen("jdk.internal.misc", thisModule));
// canUse
assertTrue(base.canUse(FileSystemProvider.class));
assertFalse(base.canUse(Thread.class));
// addUses
try {
base.addUses(FileSystemProvider.class);
assertTrue(false);
} catch (IllegalCallerException expected) { }
try {
base.addUses(Thread.class);
assertTrue(false);
} catch (IllegalCallerException expected) { }
assertFalse(base.canUse(Thread.class));
}
@Test
public void testDesktopModule() {
Module desktop = java.awt.Component.class.getModule();
Module base = Object.class.getModule();
Module xml = javax.xml.XMLConstants.class.getModule();
Module thisModule = BasicModuleTest.class.getModule();
// name
assertTrue(desktop.getName().equals("java.desktop"));
// descriptor
assertTrue(desktop.getDescriptor().exports().stream()
.anyMatch(doesExport("java.awt")));
// getClassLoader
assertTrue(desktop.getClassLoader() == null);
// getLayer
assertTrue(desktop.getLayer() == ModuleLayer.boot());
// toString
assertEquals(desktop.toString(), "module java.desktop");
// getPackages
assertTrue(desktop.getPackages().contains("java.awt"));
assertTrue(desktop.getPackages().contains("sun.awt"));
// canRead
assertTrue(desktop.canRead(base));
assertTrue(desktop.canRead(xml));
// addReads
try {
desktop.addReads(thisModule);
assertTrue(false);
} catch (IllegalCallerException expected) { }
assertFalse(desktop.canRead(thisModule));
// isExported
assertTrue(desktop.isExported("java.awt"));
assertTrue(desktop.isExported("java.awt", thisModule));
assertFalse(desktop.isExported("sun.awt"));
assertFalse(desktop.isExported("sun.awt", thisModule));
assertTrue(desktop.isExported("sun.awt", desktop));
assertFalse(desktop.isExported("java.wombat"));
assertFalse(desktop.isExported("java.wombat", thisModule));
assertFalse(desktop.isExported("java.wombat", base));
// addExports
try {
desktop.addExports("java.awt", thisModule);
assertTrue(false);
} catch (IllegalCallerException expected) { }
try {
desktop.addExports("sun.awt", thisModule);
assertTrue(false);
} catch (IllegalCallerException expected) { }
assertFalse(desktop.isExported("sun.awt"));
assertFalse(desktop.isExported("sun.awt", thisModule));
// isOpen
assertFalse(desktop.isOpen("java.awt"));
assertFalse(desktop.isOpen("java.awt", thisModule));
assertTrue(desktop.isOpen("java.awt", desktop));
assertFalse(desktop.isOpen("sun.awt"));
assertFalse(desktop.isOpen("sun.awt", thisModule));
assertTrue(desktop.isOpen("sun.awt", desktop));
assertFalse(desktop.isOpen("java.wombat"));
assertFalse(desktop.isOpen("java.wombat", thisModule));
assertFalse(desktop.isOpen("java.wombat", desktop));
// addOpens
try {
base.addOpens("sun.awt", thisModule);
assertTrue(false);
} catch (IllegalCallerException expected) { }
assertFalse(desktop.isOpen("sun.awt"));
assertFalse(desktop.isOpen("sun.awt", thisModule));
// canUse
assertTrue(base.canUse(FileSystemProvider.class));
assertFalse(base.canUse(Thread.class));
// addUses
try {
desktop.addUses(PrintServiceLookup.class);
assertTrue(false);
} catch (IllegalCallerException expected) { }
try {
desktop.addUses(Thread.class);
assertTrue(false);
} catch (IllegalCallerException expected) { }
assertFalse(desktop.canUse(Thread.class));
}
}

View file

@ -0,0 +1,2 @@
# disabled till JDK-8219408 is fixed
allowSmartActionArgs=false

View file

@ -0,0 +1,87 @@
/*
* Copyright (c) 2015, 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.
*/
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;
import jdk.test.lib.compiler.CompilerUtils;
import static jdk.test.lib.process.ProcessTools.executeTestJava;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import static org.testng.Assert.*;
/**
* @test
* @library /test/lib
* @modules jdk.compiler
* @build AccessTest jdk.test.lib.compiler.CompilerUtils
* @run testng AccessTest
* @summary Driver for test that checks access to access to types in
* exported and non-exported packages.
*/
@Test
public class AccessTest {
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");
// the names of the modules in this test
private static List<String> modules = Arrays.asList("test", "target");
/**
* Compiles all modules used by the test
*/
@BeforeTest
public void compileAll() throws Exception {
for (String mn : modules) {
Path src = SRC_DIR.resolve(mn);
Path mods = MODS_DIR.resolve(mn);
assertTrue(CompilerUtils.compile(src, mods));
}
}
/**
* Run the test
*/
public void runTest() throws Exception {
int exitValue
= executeTestJava("--module-path", MODS_DIR.toString(),
"--add-modules", "target",
"-Dsun.reflect.enableStrictMode=true",
"-m", "test/test.Main")
.outputTo(System.out)
.errorTo(System.out)
.getExitValue();
assertTrue(exitValue == 0);
}
}

View file

@ -0,0 +1,27 @@
/*
* 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 target {
exports p1;
exports p2;
}

View file

@ -0,0 +1,40 @@
/*
* 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.
*/
package p1;
/**
* Helper class in target module to allow test invoke addExports[Private]
*/
public class Helper {
Helper() { }
public static void addExports(String pn, Module who) {
Helper.class.getModule().addExports(pn, who);
}
public static void addOpens(String pn, Module who) {
Helper.class.getModule().addOpens(pn, who);
}
}

View file

@ -0,0 +1,45 @@
/*
* 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.
*/
package p1;
public class Public {
// public constructor
public Public() { }
// non-public constructor
private Public(Void ignore) { }
// public field
public static Object f1;
// non-public field
private static Object f2;
// public method
public static void foo() { }
// non-public method
private static void bar() { }
}

View file

@ -0,0 +1,45 @@
/*
* 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.
*/
package p2;
class NonPublic {
// public constructor
public NonPublic() { }
// non-public constructor
private NonPublic(Void ignore) { }
// public field
public static Object f1;
// non-public field
private static Object f2;
// public method
public static void foo() { }
// non-public method
private static void bar() { }
}

View file

@ -0,0 +1,45 @@
/*
* 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.
*/
package q1;
public class Public {
// public constructor
public Public() { }
// non-public constructor
private Public(Void ignore) { }
// public field
public static Object f1;
// non-public field
private static Object f2;
// public method
public static void foo() { }
// non-public method
private static void bar() { }
}

View file

@ -0,0 +1,45 @@
/*
* 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.
*/
package q2;
class NonPublic {
// public constructor
public NonPublic() { }
// non-public constructor
private NonPublic(Void ignore) { }
// public field
public static Object f1;
// non-public field
private static Object f2;
// public method
public static void foo() { }
// non-public method
private static void bar() { }
}

View file

@ -0,0 +1,24 @@
/*
* 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 test { }

View file

@ -0,0 +1,377 @@
/*
* 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.
*/
package test;
import java.lang.reflect.AccessibleObject;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
/**
* Test access to public/non-public members of public/non-public classes in
* exported and non-exported packages.
*/
public class Main {
public static void main(String[] args) throws Exception {
testPublicClassInExportedPackage();
testNonPublicClassInExportedPackage();
testPublicClassInNonExportedPackage();
testNonPublicClassInNonExportedPackage();
}
static void testPublicClassInExportedPackage() throws Exception {
Module thisModule = Main.class.getModule();
Module targetModule = getTargetModule();
assertTrue(targetModule.isExported("p1"));
assertTrue(targetModule.isExported("p1", thisModule));
assertTrue(targetModule.isExported("p1", targetModule));
assertFalse(targetModule.isOpen("p1"));
assertFalse(targetModule.isOpen("p1", thisModule));
assertTrue(targetModule.isOpen("p1", targetModule));
Class<?> clazz = Class.forName("p1.Public");
Constructor<?> ctor1 = clazz.getConstructor(); // public
Constructor<?> ctor2 = clazz.getDeclaredConstructor(Void.class); // non-public
Field f1 = clazz.getField("f1"); // public
Field f2 = clazz.getDeclaredField("f2"); // non-public
Method m1 = clazz.getMethod("foo"); // public
Method m2 = clazz.getDeclaredMethod("bar"); // non-public
tryAccessConstructor(ctor1, true);
tryAccessConstructor(ctor2, false);
tryAccessMethod(m1, true);
tryAccessMethod(m2, false);
tryAccessObjectField(f1, true);
tryAccessObjectField(f2, false);
trySetAccessible(ctor1, true);
trySetAccessible(ctor2, false);
trySetAccessible(m1, true);
trySetAccessible(m2, false);
trySetAccessible(f1, true);
trySetAccessible(f2, false);
targetAddOpens("p1", thisModule);
tryAccessConstructor(ctor1, true);
tryAccessConstructor(ctor2, false);
tryAccessMethod(m1, true);
tryAccessMethod(m2, false);
tryAccessObjectField(f1, true);
tryAccessObjectField(f2, false);
trySetAccessible(ctor1, true);
trySetAccessible(ctor2, true);
trySetAccessible(m1, true);
trySetAccessible(m2, true);
trySetAccessible(f1, true);
trySetAccessible(f2, true);
}
static void testNonPublicClassInExportedPackage() throws Exception {
Module thisModule = Main.class.getModule();
Module targetModule = getTargetModule();
assertTrue(targetModule.isExported("p2"));
assertTrue(targetModule.isExported("p2", thisModule));
assertTrue(targetModule.isExported("p2", targetModule));
assertFalse(targetModule.isOpen("p2"));
assertFalse(targetModule.isOpen("p2", thisModule));
assertTrue(targetModule.isOpen("p1", targetModule));
Class<?> clazz = Class.forName("p2.NonPublic");
Constructor<?> ctor1 = clazz.getConstructor();
Constructor<?> ctor2 = clazz.getDeclaredConstructor(Void.class);
Field f1 = clazz.getField("f1"); // public
Field f2 = clazz.getDeclaredField("f2"); // non-public
Method m1 = clazz.getMethod("foo"); // public
Method m2 = clazz.getDeclaredMethod("bar"); // non-public
tryAccessConstructor(ctor1, false);
tryAccessConstructor(ctor2, false);
tryAccessMethod(m1, false);
tryAccessMethod(m2, false);
tryAccessObjectField(f1, false);
tryAccessObjectField(f2, false);
trySetAccessible(ctor1, false);
trySetAccessible(ctor2, false);
trySetAccessible(m1, false);
trySetAccessible(m2, false);
trySetAccessible(f1, false);
trySetAccessible(f2, false);
targetAddExports("p2", thisModule);
tryAccessConstructor(ctor1, false);
tryAccessConstructor(ctor2, false);
tryAccessMethod(m1, false);
tryAccessMethod(m2, false);
tryAccessObjectField(f1, false);
tryAccessObjectField(f2, false);
trySetAccessible(ctor1, false);
trySetAccessible(ctor2, false);
trySetAccessible(m1, false);
trySetAccessible(m2, false);
trySetAccessible(f1, false);
trySetAccessible(f2, false);
targetAddOpens("p2", thisModule);
tryAccessConstructor(ctor1, false);
tryAccessConstructor(ctor2, false);
tryAccessMethod(m1, false);
tryAccessMethod(m2, false);
tryAccessObjectField(f1, false);
tryAccessObjectField(f2, false);
trySetAccessible(ctor1, true);
trySetAccessible(ctor2, true);
trySetAccessible(m1, true);
trySetAccessible(m2, true);
trySetAccessible(f1, true);
trySetAccessible(f2, true);
}
static void testPublicClassInNonExportedPackage() throws Exception {
Module thisModule = Main.class.getModule();
Module targetModule = getTargetModule();
assertFalse(targetModule.isExported("q1"));
assertFalse(targetModule.isExported("q1", thisModule));
assertTrue(targetModule.isExported("q1", targetModule));
assertFalse(targetModule.isOpen("q1"));
assertFalse(targetModule.isOpen("q1", thisModule));
assertTrue(targetModule.isOpen("q1", targetModule));
Class<?> clazz = Class.forName("q1.Public");
Constructor<?> ctor1 = clazz.getConstructor(); // public
Constructor<?> ctor2 = clazz.getDeclaredConstructor(Void.class); // non-public
Field f1 = clazz.getField("f1"); // public
Field f2 = clazz.getDeclaredField("f2"); // non-public
Method m1 = clazz.getMethod("foo"); // public
Method m2 = clazz.getDeclaredMethod("bar"); // non-public
tryAccessConstructor(ctor1, false);
tryAccessConstructor(ctor2, false);
tryAccessMethod(m1, false);
tryAccessMethod(m2, false);
tryAccessObjectField(f1, false);
tryAccessObjectField(f2, false);
trySetAccessible(ctor1, false);
trySetAccessible(ctor2, false);
trySetAccessible(m1, false);
trySetAccessible(m2, false);
trySetAccessible(f1, false);
trySetAccessible(f2, false);
targetAddExports("q1", thisModule);
tryAccessConstructor(ctor1, true);
tryAccessConstructor(ctor2, false);
tryAccessMethod(m1, true);
tryAccessMethod(m2, false);
tryAccessObjectField(f1, true);
tryAccessObjectField(f2, false);
trySetAccessible(ctor1, true);
trySetAccessible(ctor2, false);
trySetAccessible(m1, true);
trySetAccessible(m2, false);
trySetAccessible(f1, true);
trySetAccessible(f2, false);
targetAddOpens("q1", thisModule);
tryAccessConstructor(ctor1, true);
tryAccessConstructor(ctor1, false);
tryAccessMethod(m1, true);
tryAccessMethod(m2, false);
tryAccessObjectField(f1, true);
tryAccessObjectField(f2, false);
trySetAccessible(ctor1, true);
trySetAccessible(ctor2, true);
trySetAccessible(m1, true);
trySetAccessible(m2, true);
trySetAccessible(f1, true);
trySetAccessible(f2, true);
}
static void testNonPublicClassInNonExportedPackage() throws Exception {
Module thisModule = Main.class.getModule();
Module targetModule = getTargetModule();
assertFalse(targetModule.isExported("q2"));
assertFalse(targetModule.isExported("q2", thisModule));
assertTrue(targetModule.isExported("q2", targetModule));
assertFalse(targetModule.isOpen("q2"));
assertFalse(targetModule.isOpen("q2", thisModule));
assertTrue(targetModule.isOpen("q2", targetModule));
Class<?> clazz = Class.forName("q2.NonPublic");
Constructor<?> ctor1 = clazz.getConstructor(); // public
Constructor<?> ctor2 = clazz.getDeclaredConstructor(Void.class); // non-public
Field f1 = clazz.getField("f1"); // public
Field f2 = clazz.getDeclaredField("f2"); // non-public
Method m1 = clazz.getMethod("foo"); // public
Method m2 = clazz.getDeclaredMethod("bar"); // non-public
tryAccessConstructor(ctor1, false);
tryAccessConstructor(ctor2, false);
tryAccessMethod(m1, false);
tryAccessMethod(m2, false);
tryAccessObjectField(f1, false);
tryAccessObjectField(f2, false);
trySetAccessible(ctor1, false);
trySetAccessible(ctor2, false);
trySetAccessible(m1, false);
trySetAccessible(m2, false);
trySetAccessible(f1, false);
trySetAccessible(f2, false);
targetAddExports("q2", thisModule);
tryAccessConstructor(ctor1, false);
tryAccessConstructor(ctor2, false);
tryAccessMethod(m1, false);
tryAccessMethod(m2, false);
tryAccessObjectField(f1, false);
tryAccessObjectField(f2, false);
trySetAccessible(ctor1, false);
trySetAccessible(ctor2, false);
trySetAccessible(m1, false);
trySetAccessible(m2, false);
trySetAccessible(f1, false);
trySetAccessible(f2, false);
targetAddOpens("q2", thisModule);
tryAccessConstructor(ctor1, false);
tryAccessConstructor(ctor2, false);
tryAccessMethod(m1, false);
tryAccessMethod(m2, false);
tryAccessObjectField(f1, false);
tryAccessObjectField(f2, false);
trySetAccessible(ctor1, true);
trySetAccessible(m1, true);
trySetAccessible(m2, true);
trySetAccessible(f1, true);
trySetAccessible(f2, true);
}
static Module getTargetModule() {
return ModuleLayer.boot().findModule("target").get();
}
static void tryAccessConstructor(Constructor<?> ctor, boolean shouldSucceed) {
try {
ctor.newInstance();
assertTrue(shouldSucceed);
} catch (Exception e) {
assertFalse(shouldSucceed);
}
}
static void tryAccessMethod(Method method, boolean shouldSucceed) {
try {
method.invoke(null);
assertTrue(shouldSucceed);
} catch (Exception e) {
e.printStackTrace();
assertFalse(shouldSucceed);
}
}
static void tryAccessObjectField(Field f, boolean shouldSucceed) {
try {
f.get(null);
assertTrue(shouldSucceed);
} catch (Exception e) {
assertFalse(shouldSucceed);
}
try {
f.set(null, new Object());
assertTrue(shouldSucceed);
} catch (Exception e) {
assertFalse(shouldSucceed);
}
}
static void trySetAccessible(AccessibleObject ao, boolean shouldSucceed) {
try {
ao.setAccessible(true);
assertTrue(shouldSucceed);
} catch (Exception e) {
assertFalse(shouldSucceed);
}
}
/**
* Update target module to export a package to the given module.
*/
static void targetAddExports(String pn, Module who) throws Exception {
Class<?> helper = Class.forName("p1.Helper");
Method m = helper.getMethod("addExports", String.class, Module.class);
m.invoke(null, pn, who);
}
/**
* Update target module to open a package to the given module.
*/
static void targetAddOpens(String pn, Module who) throws Exception {
Class<?> helper = Class.forName("p1.Helper");
Method m = helper.getMethod("addOpens", String.class, Module.class);
m.invoke(null, pn, who);
}
static void assertTrue(boolean expr) {
if (!expr) throw new RuntimeException();
}
static void assertFalse(boolean expr) {
assertTrue(!expr);
}
}

View file

@ -0,0 +1,29 @@
/*
* Copyright (c) 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
* @build test/* m1/* m2/* m3/* m4/*
* @run testng/othervm test/test.Main
* @summary Basic test case for Module::addXXX methods
*/

View file

@ -0,0 +1,25 @@
/*
* Copyright (c) 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.
*/
module m1 {
exports p1;
}

View file

@ -0,0 +1,27 @@
/*
* Copyright (c) 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.
*/
package p1;
public class C {
public C() { }
}

View file

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

View file

@ -0,0 +1,30 @@
/*
* Copyright (c) 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.
*/
package p2;
public class C {
public static void export(String pn, Module m) {
C.class.getModule().addExports(pn, m);
}
}

View file

@ -0,0 +1,26 @@
/*
* Copyright (c) 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.
*/
package p2.internal;
public class C {
}

View file

@ -0,0 +1,26 @@
/*
* Copyright (c) 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.
*/
module m3 {
exports p3 to test;
opens p3 to test;
}

View file

@ -0,0 +1,26 @@
/*
* Copyright (c) 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.
*/
package p3;
public class C {
}

View file

@ -0,0 +1,25 @@
/*
* Copyright (c) 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.
*/
module m4 {
exports p4;
}

View file

@ -0,0 +1,32 @@
/*
* Copyright (c) 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.
*/
package p4;
import java.lang.reflect.Constructor;
public class C {
public static Object tryNewInstance(Class<?> clazz) throws Exception {
Constructor<?> ctor = clazz.getDeclaredConstructor();
return ctor.newInstance();
}
}

View file

@ -0,0 +1,30 @@
/*
* Copyright (c) 2017, 2021, 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 test {
exports test to org.testng;
requires m2;
requires m3;
requires m4;
requires org.testng;
}

View file

@ -0,0 +1,26 @@
/*
* Copyright (c) 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.
*/
package test;
public class C { }

View file

@ -0,0 +1,195 @@
/*
* Copyright (c) 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.
*/
package test;
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.reflect.Constructor;
import java.util.ServiceConfigurationError;
import java.util.ServiceLoader;
import org.testng.annotations.Test;
import static org.testng.Assert.*;
/**
* Basic test case for Module::addXXXX methods
*/
@Test
public class Main {
/**
* Test Module::addReads
*
* module test { }
*
* module m1 {
* exports p1;
* }
*/
public void testAddReads() throws Throwable {
Module thisModule = Main.class.getModule();
Class<?> clazz = Class.forName("p1.C");
Module m1 = clazz.getModule();
// test does not read m1
assertFalse(thisModule.canRead(m1));
MethodHandles.Lookup lookup = MethodHandles.lookup();
MethodType mt = MethodType.methodType(void.class);
try {
lookup.findConstructor(clazz, mt);
assertTrue(false);
} catch (IllegalAccessException expected) { }
// update test to read m1
Module result = thisModule.addReads(m1);
assertTrue(result== thisModule);
assertTrue(thisModule.canRead(m1));
MethodHandle mh = lookup.findConstructor(clazz, mt);
Object obj = mh.invoke();
// attempt to update m1 to read test
try {
m1.addReads(thisModule);
assertTrue(false);
} catch (IllegalCallerException expected) { }
}
/**
* Test Module::addExports
*
* module test {
* requires m2;
* }
* module m2 {
* exports p2;
* contains package p2.internal;
* }
*/
public void testAddExports() throws Exception {
Module thisModule = Main.class.getModule();
Module m2 = p2.C.class.getModule();
Class<?> targetClass = Class.forName("p2.internal.C");
String p2Internal = targetClass.getPackageName();
assertTrue(targetClass.getModule() == m2);
// m2 does not export p2.internal to test
assertFalse(m2.isExported(p2Internal, thisModule));
Constructor<?> ctor = targetClass.getDeclaredConstructor();
try {
ctor.newInstance();
assertTrue(false);
} catch (IllegalAccessException expected) { }
// update m2 to export p2.internal to test
p2.C.export(p2Internal, thisModule);
assertTrue(m2.isExported(p2Internal, thisModule));
ctor.newInstance(); // should succeed
// attempt to update m2 to export a package to test
try {
m2.addExports("p2.other", thisModule);
assertTrue(false);
} catch (IllegalCallerException expected) { }
}
/**
* Test Module::addOpens
*
* module test {
* requires m3;
* requires m4;
* }
*
* module m3 {
* exports p3 to test;
* opens p3 to test;
* }
*
* module m4 {
* exports p4;
* }
*/
public void testAddOpens() throws Exception {
Module thisModule = Main.class.getModule();
Module m3 = p3.C.class.getModule();
Module m4 = p4.C.class.getModule();
// test does not open package test to m4
assertFalse(thisModule.isOpen("test", m4));
try {
p4.C.tryNewInstance(test.C.class);
assertTrue(false);
} catch (IllegalAccessException expected) { }
// open test to m4
thisModule.addOpens("test", m4);
p4.C.tryNewInstance(test.C.class); // should succeed
// m3 does not open p3 to m4
assertFalse(m3.isOpen("p3", m4));
try {
p4.C.tryNewInstance(p3.C.class);
assertTrue(false);
} catch (IllegalAccessException expected) { }
// m3 opens p3 to test => test allowed to open m3/p3 to m4
assertTrue(m3.isOpen("p3", thisModule));
m3.addOpens("p3", m4);
assertTrue(m3.isOpen("p3", m4));
p4.C.tryNewInstance(p3.C.class); // should succeed
// attempt to update m4 to open package to m3
try {
m4.addOpens("p4", m3);
assertTrue(false);
} catch (IllegalCallerException expected) { }
}
/**
* Test Module::addUses
*/
public void testAddUses() {
Module thisModule = Main.class.getModule();
assertFalse(thisModule.canUse(Service.class));
try {
ServiceLoader.load(Service.class);
assertTrue(false);
} catch (ServiceConfigurationError expected) { }
Module result = thisModule.addUses(Service.class);
assertTrue(result== thisModule);
assertTrue(thisModule.canUse(Service.class));
ServiceLoader.load(Service.class); // no exception
}
}

View file

@ -0,0 +1,29 @@
/*
* Copyright (c) 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.
*/
package test;
/**
* Simple service type
*/
public interface Service { }

View file

@ -0,0 +1,98 @@
/*
* 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.
*/
/**
* @test
* @library src
* @build m/* Basic
* @run testng/othervm Basic
* @summary Basic test for annotations on modules
*/
import java.lang.annotation.Annotation;
import p.annotation.Foo;
import p.annotation.Bar;
import p.annotation.Baz;
import org.testng.annotations.Test;
import static org.testng.Assert.*;
public class Basic {
final Module module = Foo.class.getModule();
/**
* {@code @Foo} does not have RUNTIME retention policy.
*/
@Test
public void testInvisibleAnnotation() {
assertFalse(module.isAnnotationPresent(Foo.class));
assertNull(module.getAnnotation(Foo.class));
}
/**
* {@code @Bar} has RUNTIME retention policy and value "bar"
*/
@Test
public void testBarAnnotation() {
assertTrue(module.isAnnotationPresent(Bar.class));
Bar bar = module.getAnnotation(Bar.class);
assertNotNull(bar);
assertEquals(bar.value(), "bar");
}
/**
* {@code @Baz} has RUNTIME retention policy has a repeating value
*/
@Test
public void testBazAnnotation() {
assertTrue(module.isAnnotationPresent(Baz.class));
Baz baz = module.getAnnotation(Baz.class);
assertNotNull(baz);
String[] expected = { "one", "two", "three" };
assertEquals(baz.value(), expected);
}
/**
* Test annotations with RUNTIME retention policy
*/
@Test
public void testRuntimeAnnotations() {
Annotation[] a = module.getAnnotations();
assertEquals(a, module.getDeclaredAnnotations());
assertTrue(a.length == 2);
Bar bar;
Baz baz;
if (a[0] instanceof Bar) {
bar = (Bar)a[0];
baz = (Baz)a[1];
} else {
bar = (Bar)a[1];
baz = (Baz)a[0];
}
assertEquals(bar.value(), "bar");
String[] expected = { "one", "two", "three"};
assertEquals(baz.value(), expected);
}
}

View file

@ -0,0 +1,31 @@
/*
* 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.
*/
import p.annotation.*;
@Foo
@Bar("bar")
@Baz({"one", "two", "three"})
module m {
exports p.annotation;
}

View file

@ -0,0 +1,35 @@
/*
* 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.
*/
package p.annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.MODULE;
@Retention(RetentionPolicy.RUNTIME)
@Target(value={MODULE})
public @interface Bar {
String value();
}

View file

@ -0,0 +1,35 @@
/*
* 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.
*/
package p.annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.MODULE;
@Target(value={MODULE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Baz {
String[] value();
}

View file

@ -0,0 +1,30 @@
/*
* 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.
*/
package p.annotation;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.MODULE;
@Target(value={MODULE})
public @interface Foo {}