Path: blob/master/test/jdk/java/lang/Class/forName/NonLinking/NonLinking.java
41159 views
/*1* Copyright (c) 2019, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.net.URL;24import java.net.URLClassLoader;25import java.nio.file.Path;26import java.nio.file.Paths;2728/*29* @test30* @bug 8231924 8233091 823327231* @summary Confirm load (but not link) behavior of Class.forName()32* @library /test/lib33*34* @compile MissingClass.java Container.java35*36* @run driver jdk.test.lib.helpers.ClassFileInstaller -jar classes.jar Container Container$137*38* @run main/othervm NonLinking init39* @run main/othervm NonLinking load40*/41/*42* The @compile and '@main ClassFileInstaller' tasks above create a classes.jar43* file containing the .class file for Container, but not MissingClass.44*/4546public class NonLinking {47public static void main(String[] args) throws Throwable {48Path jarPath = Paths.get("classes.jar");49URL url = jarPath.toUri().toURL();50URLClassLoader ucl1 = new URLClassLoader("UCL1",51new URL[] { url },52null); // Don't delegate53switch(args[0]) {54case "init":55try {56// Trying to initialize Container without MissingClass -> NCDFE57Class.forName("Container", true, ucl1);58throw new RuntimeException("Missed expected NoClassDefFoundError");59} catch (NoClassDefFoundError expected) {60final String CLASSNAME = "MissingClass";61Throwable cause = expected.getCause();62if (!cause.getMessage().contains(CLASSNAME)) {63throw new RuntimeException("Cause of NoClassDefFoundError does not contain \"" + CLASSNAME + "\"", cause);64}65}66break;67case "load":68// Loading (but not linking) Container will succeed.69// Before 8233091, this fails with NCDFE due to linking.70Class.forName("Container", false, ucl1);71break;72default:73throw new RuntimeException("Unknown command: " + args[0]);74}75}76}777879