Path: blob/master/test/hotspot/jtreg/runtime/LoaderConstraints/differentLE/Test.java
41153 views
/*1* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.2* Copyright (c) 2018 SAP SE. All rights reserved.3* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.4*5* This code is free software; you can redistribute it and/or modify it6* under the terms of the GNU General Public License version 2 only, as7* published by the Free Software Foundation.8*9* This code is distributed in the hope that it will be useful, but WITHOUT10* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or11* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License12* version 2 for more details (a copy is included in the LICENSE file that13* accompanied this code).14*15* You should have received a copy of the GNU General Public License version16* 2 along with this work; if not, write to the Free Software Foundation,17* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.18*19* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA20* or visit www.oracle.com if you need additional information or have any21* questions.22*/2324/**25* @test26* @bug 819985227* @summary Test exception messages of LinkageError. Two class loaders load28* two different versions of a class. Should trigger exception in29* SystemDictionary::check_constraints().30* @library /test/lib31* @compile D_ambgs.jasm32* @run driver jdk.test.lib.helpers.ClassFileInstaller test.D_ambgs33* @compile ../common/PreemptingClassLoader.java34* test/D_ambgs.java Test.java test/B.java35* @run driver jdk.test.lib.helpers.ClassFileInstaller test.B36* @run main/othervm Test37*/3839import test.*;4041import java.io.ByteArrayOutputStream;42import java.io.InputStream;4344import java.io.*;45import java.nio.file.Files;46import java.nio.file.Path;47import java.nio.file.Paths;4849public class Test {5051// Force LinkageError.52//53// Derived from test runtime/6626217.54//55// Uses the specialized class loader PreemptingClassLoader.56// PreemptingClassLoader only loads files with names passed to it in its57// constructor. If it does not find it, it delegates to the super class loader.58//59// A // interface60// |61// B // Compiled to the current working directory so that it is found by our62// // special class loader. B uses D, so that loading B triggers loading D.63//64// C // An abstract class.65// |66// D // Class with two different implementations D1 and D2. D2 is67// // compiled to the current working directory so that it is found by our68// // special class loader.69//70// First, the bootstrap loader will load D1. It already has loaded interface A.71// Then, the second class loader PreemptingClassLoader will load B. Recursive,72// it tries to load interface A. As it does not find it (there is no A.impl2),73// it asks the super classloader for A.74// Then it loads the D2 variant of D from the current working directory and it's75// superclass C. This fails as D1 is already loaded with the same superclass.7677// Break the expectedErrorMessage into 2 pieces since the loader name will include78// its identity hash and can not be compared against.79static String expectedErrorMessage_part1 = "loader constraint violation: loader PreemptingClassLoader @";80static String expectedErrorMessage_part2 = " wants to load class test.D_ambgs. A different class " +81"with the same name was previously loaded by 'app'. " +82"(test.D_ambgs is in unnamed module of loader 'app')";83public static void test_access() throws Exception {84try {85// Make a Class 'D_ambgs' under the default loader.86// This uses the implementation from the .java file.87C c_1 = new D_ambgs();8889// Some classes under a new Loader, loader2, including, indirectly,90// another version of 'D_ambgs'91String[] classNames = {"test.B", "test.D_ambgs"};9293ClassLoader loader2 = new PreemptingClassLoader(null, classNames, false);94Class class2 = loader2.loadClass("test.B");95A iface = (A)class2.newInstance();9697// Call D1.make() loaded by bootstrap loader with B loaded by Loader2.98D_ambgs[] x2 = c_1.make(iface);99100throw new RuntimeException("Expected LinkageError was not thrown.");101} catch (LinkageError jle) {102String errorMsg = jle.getMessage();103if (!errorMsg.contains(expectedErrorMessage_part1) ||104!errorMsg.contains(expectedErrorMessage_part2)) {105System.out.println("Expected: " + expectedErrorMessage_part1 + "<id>" + expectedErrorMessage_part2 + "\n" +106"but got: " + errorMsg);107throw new RuntimeException("Wrong error message of LinkageError.");108} else {109System.out.println("Passed with message: " + errorMsg);110}111}112}113114public static void main(String[] args) throws Exception {115test_access();116}117}118119120