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,177 @@
/*
* Copyright (c) 2023, 2025, 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 8310242 8328874
* @run junit ForNameNames
* @summary Verify class names for Class.forName
*/
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import static org.junit.jupiter.api.Assertions.*;
public class ForNameNames {
//Max length in Modified UTF-8 bytes for class names.
private static final int JAVA_CLASSNAME_MAX_LEN = 65535;
private static final String ONE_BYTE = "A"; // 1-byte UTF-8
private static final String TWO_BYTE = "\u0100"; // 2-byte UTF-8
private static final String THREE_BYTE = "\u2600"; // 3-byte UTF-8
private static final String ERR_MSG_IN_CORE = "Class name length exceeds limit of"; // check in corelib
private static final String ERR_MSG_IN_JVM = "Class name exceeds maximum length"; // check in jvm
static class Inner {}
static Stream<Arguments> testCases() {
return Stream.of(
Arguments.of("java.lang.String", String.class),
Arguments.of("[Ljava.lang.String;", String[].class),
Arguments.of("ForNameNames$Inner", Inner.class),
Arguments.of("[LForNameNames$Inner;", Inner[].class),
Arguments.of("[[I", int[][].class)
);
}
/*
* Test 1-arg and 3-arg Class::forName. Class::getName on the returned
* Class object returns the name passed to Class::forName.
*/
@ParameterizedTest
@MethodSource("testCases")
void testForName(String cn, Class<?> expected) throws ClassNotFoundException {
ClassLoader loader = ForNameNames.class.getClassLoader();
Class<?> c1 = Class.forName(cn, false, loader);
assertEquals(expected, c1);
assertEquals(cn, c1.getName());
Class<?> c2 = Class.forName(cn);
assertEquals(expected, c2);
assertEquals(cn, c2.getName());
}
static Stream<Arguments> invalidNames() {
return Stream.of(
Arguments.of("I"), // primitive type
Arguments.of("int[]"), // fully-qualified name of int array
Arguments.of("ForNameNames.Inner"), // fully-qualified name of nested type
Arguments.of("[java.lang.String"), // missing L and ;
Arguments.of("[Ljava.lang.String"), // missing ;
Arguments.of("[Ljava/lang/String;") // type descriptor
);
}
@ParameterizedTest
@MethodSource("invalidNames")
void testInvalidNames(String cn) {
ClassLoader loader = ForNameNames.class.getClassLoader();
assertThrows(ClassNotFoundException.class, () -> Class.forName(cn, false, loader));
}
@Test
void testModule() {
// Class.forName(Module, String) does not allow class name for array types
Class<?> c = Class.forName(Object.class.getModule(), "[Ljava.lang.String;");
assertNull(c);
}
static Stream<Arguments> validLen() {
return Stream.of(
// 1-byte character
Arguments.of(ONE_BYTE.repeat(JAVA_CLASSNAME_MAX_LEN - 1)),
Arguments.of(ONE_BYTE.repeat(JAVA_CLASSNAME_MAX_LEN)),
Arguments.of(ONE_BYTE.repeat(JAVA_CLASSNAME_MAX_LEN / 3 - 1)),
Arguments.of(ONE_BYTE.repeat(JAVA_CLASSNAME_MAX_LEN / 3)),
Arguments.of(ONE_BYTE.repeat(JAVA_CLASSNAME_MAX_LEN / 3 + 1)),
// 2-byte characters
Arguments.of(TWO_BYTE.repeat(JAVA_CLASSNAME_MAX_LEN / 2)),
Arguments.of(TWO_BYTE.repeat(JAVA_CLASSNAME_MAX_LEN / 6)),
Arguments.of(TWO_BYTE.repeat(JAVA_CLASSNAME_MAX_LEN / 6 + 1)),
// 3-byte characters
Arguments.of(THREE_BYTE.repeat(JAVA_CLASSNAME_MAX_LEN / 3 - 1)),
Arguments.of(THREE_BYTE.repeat(JAVA_CLASSNAME_MAX_LEN / 3)),
Arguments.of(THREE_BYTE.repeat(JAVA_CLASSNAME_MAX_LEN / 9)),
Arguments.of(THREE_BYTE.repeat(JAVA_CLASSNAME_MAX_LEN / 9 + 1))
);
}
/*
* Test class name length handling in 1-arg and 3-arg Class::forName
* with valid length.
*/
@ParameterizedTest
@MethodSource("validLen")
void testValidLen(String cn) {
ClassLoader loader = ForNameNames.class.getClassLoader();
// 3-arg Class.forName
ClassNotFoundException ex = assertThrows(ClassNotFoundException.class,
() -> Class.forName(cn, false, loader));
assertFalse(ex.getMessage().contains(ERR_MSG_IN_CORE)
|| ex.getMessage().contains(ERR_MSG_IN_JVM),
"Unexpected exception message");
// 1-arg Class.forName
ex = assertThrows(ClassNotFoundException.class,
() -> Class.forName(cn));
assertFalse(ex.getMessage().contains(ERR_MSG_IN_CORE)
|| ex.getMessage().contains(ERR_MSG_IN_JVM),
"Unexpected exception message");
}
static Stream<Arguments> invalidLen() {
return Stream.of(
// 1-byte characters over the limit
Arguments.of(ONE_BYTE.repeat(JAVA_CLASSNAME_MAX_LEN + 1)),
// 2-byte characters over the limit
Arguments.of(TWO_BYTE.repeat(JAVA_CLASSNAME_MAX_LEN / 2 + 1)),
// 3-byte characters over the limit
Arguments.of(THREE_BYTE.repeat(JAVA_CLASSNAME_MAX_LEN / 3 + 1))
);
}
/*
* Test class name length handling in 1-arg and 3-arg Class::forName
* with invalid (too long) length.
*/
@ParameterizedTest
@MethodSource("invalidLen")
void testInvalidLen(String cn) {
ClassLoader loader = ForNameNames.class.getClassLoader();
// 3-arg Class.forName
ClassNotFoundException ex = assertThrows(ClassNotFoundException.class,
() -> Class.forName(cn, false, loader));
assertTrue(ex.getMessage().contains(ERR_MSG_IN_CORE),
"Unexpected exception message");
// 1-arg Class.forName
ex = assertThrows(ClassNotFoundException.class,
() -> Class.forName(cn));
assertTrue(ex.getMessage().contains(ERR_MSG_IN_CORE),
"Unexpected exception message");
}
}

View file

@ -0,0 +1,52 @@
/*
* Copyright (c) 1998, 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 4131701
@summary This is a basic sanity test for the Class.forName
variant that the 'whether-initialize' arg.
*/
class x123 {
static {
InitArg.x123Initialized = true;
}
}
public class InitArg {
public static boolean x123Initialized = false;
public static void main(String[] args) throws Exception {
Class c = Class.forName("x123", false,
InitArg.class.getClassLoader());
if (x123Initialized) {
throw new Exception("forName should not run initializer");
}
Class d = Class.forName("x123", true,
InitArg.class.getClassLoader());
if (!x123Initialized) {
throw new Exception("forName not running initializer");
}
}
}

View file

@ -0,0 +1,42 @@
/*
* Copyright (c) 1998, 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 4104861
@summary forName is accepting methods with slashes
@author James Bond/007
*/
public class InvalidNameWithSlash {
public static void main(String[] args) throws Exception {
boolean exceptionOccurred = false;
try {
Class c = Class.forName("java/lang.Object");
} catch (Exception e) {
exceptionOccurred = true;
}
if (!exceptionOccurred) {
throw new Exception("forName accepting names with slashes?");
}
}
}

View file

@ -0,0 +1,160 @@
/*
* Copyright (c) 2003, 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.nio.file.Files;
import java.nio.file.Path;
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
/*
* @test
* @bug 4952558
* @library /test/lib
* @run testng/othervm NonJavaNames
* @summary Verify names that aren't legal Java names are accepted by forName.
*/
public class NonJavaNames {
public static class Baz {
public Baz(){}
}
public static interface myInterface {
}
NonJavaNames.myInterface create() {
// With target 1.5, this class's name will include a '+'
// instead of a '$'.
class Baz2 implements NonJavaNames.myInterface {
public Baz2() { }
}
return new Baz2();
}
private static final String SRC_DIR = System.getProperty("test.src");
private static final Path TEST_SRC = Path.of(SRC_DIR, "classes");
private static final Path TEST_CLASSES = Path.of(System.getProperty("test.classes", "."));
@BeforeClass
public void createInvalidNameClasses() throws IOException {
Path hyphenPath = TEST_SRC.resolve("hyphen.class");
Path commaPath = TEST_SRC.resolve("comma.class");
Path periodPath = TEST_SRC.resolve("period.class");
Path leftsquarePath = TEST_SRC.resolve("left-square.class");
Path rightsquarePath = TEST_SRC.resolve("right-square.class");
Path plusPath = TEST_SRC.resolve("plus.class");
Path semicolonPath = TEST_SRC.resolve("semicolon.class");
Path zeroPath = TEST_SRC.resolve("0.class");
Path threePath = TEST_SRC.resolve("3.class");
Path zadePath = TEST_SRC.resolve("Z.class");
Path dhyphenPath = TEST_CLASSES.resolve("-.class");
Path dcommaPath = TEST_CLASSES.resolve(",.class");
Path dperiodPath = TEST_CLASSES.resolve("..class");
Path dleftsquarePath = TEST_CLASSES.resolve("[.class");
Path drightsquarePath = TEST_CLASSES.resolve("].class");
Path dplusPath = TEST_CLASSES.resolve("+.class");
Path dsemicolonPath = TEST_CLASSES.resolve(";.class");
Path dzeroPath = TEST_CLASSES.resolve("0.class");
Path dthreePath = TEST_CLASSES.resolve("3.class");
Path dzadePath = TEST_CLASSES.resolve("Z.class");
Files.copy(hyphenPath, dhyphenPath, REPLACE_EXISTING);
Files.copy(commaPath, dcommaPath, REPLACE_EXISTING);
Files.copy(periodPath, dperiodPath, REPLACE_EXISTING);
Files.copy(leftsquarePath, dleftsquarePath, REPLACE_EXISTING);
Files.copy(rightsquarePath, drightsquarePath, REPLACE_EXISTING);
Files.copy(plusPath, dplusPath, REPLACE_EXISTING);
Files.copy(semicolonPath, dsemicolonPath, REPLACE_EXISTING);
Files.copy(zeroPath, dzeroPath, REPLACE_EXISTING);
Files.copy(threePath, dthreePath, REPLACE_EXISTING);
Files.copy(zadePath, dzadePath, REPLACE_EXISTING);
}
@Test
public void testForNameReturnsSameClass() throws ClassNotFoundException {
NonJavaNames.Baz bz = new NonJavaNames.Baz();
String name;
if (Class.forName(name=bz.getClass().getName()) != NonJavaNames.Baz.class) {
System.err.println("Class object from forName does not match object.class.");
System.err.println("Failures for class ``" + name + "''.");
throw new RuntimeException();
}
NonJavaNames.myInterface bz2 = (new NonJavaNames()).create();
if (Class.forName(name=bz2.getClass().getName()) != bz2.getClass()) {
System.err.println("Class object from forName does not match getClass.");
System.err.println("Failures for class ``" + name + "''.");
throw new RuntimeException();
}
}
@Test(dataProvider = "goodNonJavaClassNames")
public void testGoodNonJavaClassNames(String name) throws ClassNotFoundException {
System.out.println("Testing good class name ``" + name + "''");
Class.forName(name);
}
@Test(dataProvider = "badNonJavaClassNames")
public void testBadNonJavaClassNames(String name) {
System.out.println("Testing bad class name ``" + name + "''");
try {
Class.forName(name);
} catch (ClassNotFoundException e) {
// Expected behavior
return;
}
throw new RuntimeException("Bad class name ``" + name + "'' accepted.");
}
@DataProvider(name = "goodNonJavaClassNames")
Object[][] getGoodNonJavaClassNames() {
return new Object[][] {
{","},
{"+"},
{"-"},
{"0"},
{"3"},
// ":", These names won't work under windows.
// "<",
// ">",
{"Z"},
{"]"}
};
}
@DataProvider(name = "badNonJavaClassNames")
Object[][] getBadNonJavaClassNames() {
return new Object[][] {
{";"},
{"["},
{"."}
};
}
}

View file

@ -0,0 +1,31 @@
/*
* Copyright (c) 2019, 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.
*/
public class Container {
public Container(MissingClass m) {}
public Container() {
this(new MissingClass() {});
}
}

View file

@ -0,0 +1,24 @@
/*
* Copyright (c) 2019, 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.
*/
public class MissingClass {}

View file

@ -0,0 +1,77 @@
/*
* Copyright (c) 2019, 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.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Path;
import java.nio.file.Paths;
/*
* @test
* @bug 8231924 8233091 8233272
* @summary Confirm load (but not link) behavior of Class.forName()
* @library /test/lib
*
* @compile MissingClass.java Container.java
*
* @run driver jdk.test.lib.helpers.ClassFileInstaller -jar classes.jar Container Container$1
*
* @run main/othervm NonLinking init
* @run main/othervm NonLinking load
*/
/*
* The @compile and '@main ClassFileInstaller' tasks above create a classes.jar
* file containing the .class file for Container, but not MissingClass.
*/
public class NonLinking {
public static void main(String[] args) throws Throwable {
Path jarPath = Paths.get("classes.jar");
URL url = jarPath.toUri().toURL();
URLClassLoader ucl1 = new URLClassLoader("UCL1",
new URL[] { url },
null); // Don't delegate
switch(args[0]) {
case "init":
try {
// Trying to initialize Container without MissingClass -> NCDFE
Class.forName("Container", true, ucl1);
throw new RuntimeException("Missed expected NoClassDefFoundError");
} catch (NoClassDefFoundError expected) {
final String CLASSNAME = "MissingClass";
Throwable cause = expected.getCause();
if (!cause.getMessage().contains(CLASSNAME)) {
throw new RuntimeException("Cause of NoClassDefFoundError does not contain \"" + CLASSNAME + "\"", cause);
}
}
break;
case "load":
// Loading (but not linking) Container will succeed.
// Before 8233091, this fails with NCDFE due to linking.
Class.forName("Container", false, ucl1);
break;
default:
throw new RuntimeException("Unknown command: " + args[0]);
}
}
}

View file

@ -0,0 +1,32 @@
/*
* Copyright (c) 2003, 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.
*/
/*
* Source used to generated primordial class file for NonJavaNames
* test.
*/
public class Z {
public static void main(String argv[]) {
System.out.println("Hello world.");
}
}

View file

@ -0,0 +1,24 @@
/*
* Copyright (c) 2013, 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.
*/
public class Class1 {}

View file

@ -0,0 +1,24 @@
/*
* Copyright (c) 2013, 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.
*/
public class Class2 {}

View file

@ -0,0 +1,24 @@
/*
* Copyright (c) 2013, 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.
*/
public class Class3 {}

View file

@ -0,0 +1,24 @@
/*
* Copyright (c) 2013, 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.
*/
public class Class4 {}

View file

@ -0,0 +1,129 @@
/*
* Copyright (c) 2013, 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 7044282
* @build Class1 Class2 Class3 Class4
* @run main ExceedMaxDim
* @summary Make sure you can't get an array class of dimension > 255.
*/
// Class1, Class2, Class3 and Class4 should not have been loaded prior to the
// calls to forName
public class ExceedMaxDim {
//0123456789012345678901234567890123456789
private String brackets = "[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[" +
"[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[" +
"[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[" +
"[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[" +
"[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[" +
"[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[" +
"[[[[[[[[[[[[[[";
private String name254 = brackets + "Ljava.lang.String;";
private String name255 = "[" + name254;
private String name256 = "[" + name255;
private String name1 = "[Ljava.lang.String;";
private String bigName;
private int error = 0;
private static final ClassLoader IMPLICIT_LOADER = null;
public ExceedMaxDim() {
super();
StringBuilder sb = new StringBuilder(Short.MAX_VALUE + 50);
for (int i = 0; i < Short.MAX_VALUE + 20; i++)
sb.append('[');
sb.append("Ljava.lang.String;");
bigName = sb.toString();
if (name256.lastIndexOf('[') != 255) // 256:th [
throw new RuntimeException("Test broken");
}
public static void main(String[] args) throws Exception {
ExceedMaxDim test = new ExceedMaxDim();
test.testImplicitLoader();
test.testOtherLoader();
if (test.error != 0)
throw new RuntimeException("Test failed, was able to create array with dim > 255." +
" See log for details.");
}
private void testImplicitLoader() throws Exception {
// These four should succeed
assertSucceedForName(name1, IMPLICIT_LOADER);
assertSucceedForName(name254, IMPLICIT_LOADER);
assertSucceedForName(name255, IMPLICIT_LOADER);
assertSucceedForName(brackets + "[LClass1;", IMPLICIT_LOADER);
// The following three should fail
assertFailForName(name256, IMPLICIT_LOADER);
assertFailForName(bigName, IMPLICIT_LOADER);
assertFailForName(brackets + "[[LClass2;", IMPLICIT_LOADER);
}
private void testOtherLoader() throws Exception {
ClassLoader cl = ExceedMaxDim.class.getClassLoader();
// These four should succeed
assertSucceedForName(name1, cl);
assertSucceedForName(name254,cl);
assertSucceedForName(name255, cl);
assertSucceedForName(brackets + "[LClass3;", cl);
// The following three should fail
assertFailForName(name256, cl);
assertFailForName(bigName, cl);
assertFailForName(brackets + "[[Class4;", cl);
}
private void assertFailForName(String name, ClassLoader cl) {
Class<?> c;
try {
if (cl == null)
c = Class.forName(name);
else
c = Class.forName(name, true, cl);
error++;
System.err.println("ERROR: could create " + c);
} catch (ClassNotFoundException e) {
;// ok
}
}
private void assertSucceedForName(String name, ClassLoader cl) {
Class<?> c;
try {
if (cl == null)
c = Class.forName(name);
else
c = Class.forName(name, true, cl);
} catch (ClassNotFoundException e) {
error++;
System.err.println("ERROR: could not create " + name);
}
}
}

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View file

@ -0,0 +1,134 @@
/*
* Copyright (c) 2015, 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.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.stream.Stream;
import jdk.test.lib.util.FileUtils;
import jdk.test.lib.compiler.CompilerUtils;
import static jdk.test.lib.process.ProcessTools.*;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import static org.testng.Assert.assertTrue;
/**
* @test
* @bug 8087335
* @summary Tests for Class.forName(Module,String)
* @library /test/lib
* @modules jdk.compiler
* @build jdk.test.lib.Platform
* jdk.test.lib.util.FileUtils
* jdk.test.lib.compiler.CompilerUtils
* jdk.test.lib.process.ProcessTools
* TestDriver TestMain TestLayer
* @run testng TestDriver
*/
public class TestDriver {
private static final String TEST_SRC =
Paths.get(System.getProperty("test.src")).toString();
private static final String TEST_CLASSES =
Paths.get(System.getProperty("test.classes")).toString();
private static final Path MOD_SRC_DIR = Paths.get(TEST_SRC, "src");
private static final Path MOD_DEST_DIR = Paths.get("mods");
private static final String[] modules = new String[] {"m1", "m2"};
/**
* Compiles all modules used by the test.
*/
@BeforeClass
public void setup() throws Exception {
assertTrue(CompilerUtils.compile(
MOD_SRC_DIR, MOD_DEST_DIR,
"--module-source-path",
MOD_SRC_DIR.toString()));
copyDirectories(MOD_DEST_DIR.resolve("m1"), Paths.get("mods1"));
copyDirectories(MOD_DEST_DIR.resolve("m2"), Paths.get("mods2"));
}
@Test
public void test() throws Exception {
String[] options = new String[] {
"-cp", TEST_CLASSES,
"--module-path", MOD_DEST_DIR.toString(),
"--add-modules", String.join(",", modules),
"-m", "m2/p2.test.Main"
};
runTest(options);
}
@Test
public void testUnnamedModule() throws Exception {
String[] options = new String[] {
"-cp", TEST_CLASSES,
"--module-path", MOD_DEST_DIR.toString(),
"--add-modules", String.join(",", modules),
"TestMain"
};
runTest(options);
}
@Test
public void testLayer() throws Exception {
String[] options = new String[] {
"-cp", TEST_CLASSES,
"TestLayer"
};
runTest(options);
}
private void runTest(String[] options) throws Exception {
assertTrue(executeTestJava(options)
.outputTo(System.out)
.errorTo(System.err)
.getExitValue() == 0);
}
private void copyDirectories(Path source, Path dest) throws IOException {
if (Files.exists(dest))
FileUtils.deleteFileTreeWithRetry(dest);
Files.walk(source, Integer.MAX_VALUE)
.filter(Files::isRegularFile)
.forEach(p -> {
try {
Path to = dest.resolve(source.relativize(p));
Files.createDirectories(to.getParent());
Files.copy(p, to);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
});
}
}

View file

@ -0,0 +1,80 @@
/*
* 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.lang.module.Configuration;
import java.lang.module.ModuleFinder;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Set;
public class TestLayer {
private static final Path MODS_DIR = Paths.get("mods");
private static final Set<String> modules = Set.of("m1", "m2");
public static void main(String[] args) throws Exception {
ModuleFinder finder = ModuleFinder.of(MODS_DIR);
Configuration parent = ModuleLayer.boot().configuration();
Configuration cf = parent.resolveAndBind(ModuleFinder.of(),
finder,
modules);
ClassLoader scl = ClassLoader.getSystemClassLoader();
ModuleLayer layer = ModuleLayer.boot().defineModulesWithManyLoaders(cf, scl);
Module m1 = layer.findModule("m1").get();
Module m2 = layer.findModule("m2").get();
// find exported and non-exported class from a named module
findClass(m1, "p1.A");
findClass(m1, "p1.internal.B");
findClass(m2, "p2.C");
// find class from unnamed module
ClassLoader ld = TestLayer.class.getClassLoader();
findClass(ld.getUnnamedModule(), "TestDriver");
// check if clinit should not be initialized
// compile without module-path; so use reflection
Class<?> c = Class.forName(m1, "p1.Initializer");
Method m = c.getMethod("isInited");
Boolean isClinited = (Boolean) m.invoke(null);
if (isClinited.booleanValue()) {
throw new RuntimeException("clinit should not be invoked");
}
}
static Class<?> findClass(Module module, String cn) {
Class<?> c = Class.forName(module, cn);
if (c == null) {
throw new RuntimeException(cn + " not found in " + module);
}
if (c.getModule() != module) {
throw new RuntimeException(c.getModule() + " != " + module);
}
return c;
}
}

View file

@ -0,0 +1,61 @@
/*
* 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.
*/
import java.lang.reflect.Method;
public class TestMain {
public static void main(String[] args) throws Exception {
ModuleLayer boot = ModuleLayer.boot();
Module m1 = boot.findModule("m1").get();
Module m2 = boot.findModule("m2").get();
// find exported and non-exported class from a named module
findClass(m1, "p1.A");
findClass(m1, "p1.internal.B");
findClass(m2, "p2.C");
// find class from unnamed module
ClassLoader loader = TestMain.class.getClassLoader();
findClass(loader.getUnnamedModule(), "TestDriver");
// check if clinit should not be initialized
// compile without module-path; so use reflection
Class<?> c = Class.forName(m1, "p1.Initializer");
Method m = c.getMethod("isInited");
Boolean isClinited = (Boolean) m.invoke(null);
if (isClinited.booleanValue()) {
throw new RuntimeException("clinit should not be invoked");
}
}
static Class<?> findClass(Module module, String cn) {
Class<?> c = Class.forName(module, cn);
if (c == null) {
throw new RuntimeException(cn + " not found in " + module);
}
if (c.getModule() != module) {
throw new RuntimeException(c.getModule() + " != " + module);
}
return c;
}
}

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 m1 {
exports p1;
}

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.
*/
package p1;
public class A {
static {
Initializer.init();
System.out.println("p1.A is initialzed");
}
}

View file

@ -0,0 +1,34 @@
/*
* 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 p1;
public class Initializer {
private static boolean inited = false;
static synchronized void init() {
inited = true;
}
public static synchronized boolean isInited() {
return inited;
}
}

View file

@ -0,0 +1,28 @@
/*
* 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 p1.internal;
public class B {
public B() {}
}

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 p2;
}

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 p2;
public class C {
}

View file

@ -0,0 +1,58 @@
/*
* 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 p2.test;
public class Main {
public static void main(String... args) throws Exception {
ModuleLayer boot = ModuleLayer.boot();
Module m1 = boot.findModule("m1").get();
Module m2 = Main.class.getModule();
// find exported and non-exported class from a named module
findClass(m1, "p1.A");
findClass(m1, "p1.internal.B");
findClass(m2, "p2.C");
// find class from unnamed module
ClassLoader loader = m2.getClassLoader();
findClass(loader.getUnnamedModule(), "TestDriver");
try {
Class<?> c = findClass(m1, "p1.internal.B");
c.newInstance();
throw new RuntimeException(c.getName() + " should not be exported to m2");
} catch (IllegalAccessException e) {}
}
static Class<?> findClass(Module module, String cn) {
Class<?> c = Class.forName(module, cn);
if (c == null) {
throw new RuntimeException(cn + " not found in " + module);
}
if (c.getModule() != module) {
throw new RuntimeException(c.getModule() + " != " + module);
}
return c;
}
}