1 /* 2 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. 3 * Copyright (c) 2018 SAP SE. All rights reserved. 4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 5 * 6 * This code is free software; you can redistribute it and/or modify it 7 * under the terms of the GNU General Public License version 2 only, as 8 * published by the Free Software Foundation. 9 * 10 * This code is distributed in the hope that it will be useful, but WITHOUT 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 13 * version 2 for more details (a copy is included in the LICENSE file that 14 * accompanied this code). 15 * 16 * You should have received a copy of the GNU General Public License version 17 * 2 along with this work; if not, write to the Free Software Foundation, 18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 19 * 20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 21 * or visit www.oracle.com if you need additional information or have any 22 * questions. 23 */ 24 25 /** 26 * @test 27 * @bug 8199852 28 * @summary Test exception messages of LinkageError. Two class loaders load 29 * two different versions of a class. Should trigger exception in 30 * SystemDictionary::check_constraints(). 31 * @library /test/lib 32 * @compile D_ambgs.jasm 33 * @run driver ClassFileInstaller test.D_ambgs 34 * @compile ../common/PreemptingClassLoader.java 35 * test/D_ambgs.java Test.java test/B.java 36 * @run driver ClassFileInstaller test.B 37 * @run main/othervm Test 38 */ 39 40 import test.*; 41 42 import java.io.ByteArrayOutputStream; 43 import java.io.InputStream; 44 45 import java.io.*; 46 import java.nio.file.Files; 47 import java.nio.file.Path; 48 import java.nio.file.Paths; 49 50 public class Test { 51 52 // Force LinkageError. 53 // 54 // Derived from test runtime/6626217. 55 // 56 // Uses the specialized class loader PreemptingClassLoader. 57 // PreemptingClassLoader only loads files with names passed to it in its 58 // constructor. If it does not find it, it delegates to the super class loader. 59 // 60 // A // interface 61 // | 62 // B // Compiled to the current working directory so that it is found by our 63 // // special class loader. B uses D, so that loading B triggers loading D. 64 // 65 // C // An abstract class. 66 // | 67 // D // Class with two different implementations D1 and D2. D2 is 68 // // compiled to the current working directory so that it is found by our 69 // // special class loader. 70 // 71 // First, the bootstrap loader will load D1. It already has loaded interface A. 72 // Then, the second class loader PreemptingClassLoader will load B. Recursive, 73 // it tries to load interface A. As it does not find it (there is no A.impl2), 74 // it asks the super classloader for A. 75 // Then it loads the D2 variant of D from the current working directory and it's 76 // superclass C. This fails as D1 is already loaded with the same superclass. 77 78 // Break the expectedErrorMessage into 2 pieces since the loader name will include 79 // its identity hash and can not be compared against. 80 static String expectedErrorMessage_part1 = "loader constraint violation: loader PreemptingClassLoader @"; 81 static String expectedErrorMessage_part2 = " (instance of PreemptingClassLoader, " + 82 "child of 'app' jdk.internal.loader.ClassLoaders$AppClassLoader) wants to load " + 83 "class test.D_ambgs. A different class with the same name was previously loaded " + 84 "by 'app' (instance of jdk.internal.loader.ClassLoaders$AppClassLoader)."; 85 86 public static void test_access() throws Exception { 87 try { 88 // Make a Class 'D_ambgs' under the default loader. 89 // This uses the implementation from the .java file. 90 C c_1 = new D_ambgs(); 91 92 // Some classes under a new Loader, loader2, including, indirectly, 93 // another version of 'D_ambgs' 94 String[] classNames = {"test.B", "test.D_ambgs"}; 95 96 ClassLoader loader2 = new PreemptingClassLoader(null, classNames, false); 97 Class class2 = loader2.loadClass("test.B"); 98 A iface = (A)class2.newInstance(); 99 100 // Call D1.make() loaded by bootstrap loader with B loaded by Loader2. 101 D_ambgs[] x2 = c_1.make(iface); 102 103 throw new RuntimeException("Expected LinkageError was not thrown."); 104 } catch (LinkageError jle) { 105 String errorMsg = jle.getMessage(); 106 if (!errorMsg.contains(expectedErrorMessage_part1) || 107 !errorMsg.contains(expectedErrorMessage_part2)) { 108 System.out.println("Expected: " + expectedErrorMessage_part1 + "<id>" + expectedErrorMessage_part2 + "\n" + 109 "but got: " + errorMsg); 110 throw new RuntimeException("Wrong error message of LinkageError."); 111 } else { 112 System.out.println("Passed with message: " + errorMsg); 113 } 114 } 115 } 116 117 public static void main(String[] args) throws Exception { 118 test_access(); 119 } 120 } 121