Path: blob/master/test/hotspot/jtreg/runtime/LoaderConstraints/vtableLdrConstraint/Test.java
40948 views
/*1* Copyright (c) 2017, 2018, 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*/2223/*24* @test25* @bug 8186092 819985226* @compile ../common/Foo.java27* ../common/J.java28* I.java29* ../common/C.jasm30* Task.java31* ../common/PreemptingClassLoader.java32* @run main/othervm Test33*/3435public class Test {3637// Break expected error messages into 3 parts since the loader name includes its identity38// hash which is unique and can't be compared against.39static String expectedErrorMessage1_part1 = "loader constraint violation for class test.Task: when " +40"selecting overriding method 'test.Foo test.Task.m()' the " +41"class loader PreemptingClassLoader @";42static String expectedErrorMessage1_part2 = " of the selected method's type test.Task, and the class " +43"loader 'app' for its super type test.J have different Class objects " +44"for the type test.Foo used in the signature (test.Task is in unnamed " +45"module of loader PreemptingClassLoader @";46static String expectedErrorMessage1_part3 = ", parent loader 'app'; test.J is in unnamed module of loader 'app')";4748static String expectedErrorMessage2_part1 = "loader constraint violation for class test.Task: when " +49"selecting overriding method 'test.Foo test.Task.m()' the " +50"class loader 'VtableLdrCnstrnt_Test_Loader' @";51static String expectedErrorMessage2_part2 = " of the selected method's type test.Task, and the class " +52"loader 'app' for its super type test.J have different Class objects " +53"for the type test.Foo used in the signature (test.Task is in unnamed " +54"module of loader 'VtableLdrCnstrnt_Test_Loader' @";55static String expectedErrorMessage2_part3 = ", parent loader 'app'; test.J is in unnamed module of loader 'app')";5657// Test that the error message is correct when a loader constraint error is58// detected during vtable creation.59//60// In this test, during vtable creation for class Task, method "Task.m()LFoo;"61// overrides "J.m()LFoo;". But, Task's class Foo and super type J's class Foo62// are different. So, a LinkageError exception should be thrown because the63// loader constraint check will fail.64public static void test(String loaderName,65String expectedErrorMessage_part1,66String expectedErrorMessage_part2,67String expectedErrorMessage_part3) throws Exception {68Class<?> c = test.Foo.class; // Forces standard class loader to load Foo.69String[] classNames = {"test.Task", "test.Foo", "test.I"};70ClassLoader l = new PreemptingClassLoader(loaderName, classNames);71l.loadClass("test.Foo");72try {73l.loadClass("test.Task").newInstance();74throw new RuntimeException("Expected LinkageError exception not thrown");75} catch (LinkageError e) {76String errorMsg = e.getMessage();77if (!errorMsg.contains(expectedErrorMessage_part1) ||78!errorMsg.contains(expectedErrorMessage_part2) ||79!errorMsg.contains(expectedErrorMessage_part3)) {80System.out.println("Expected: " + expectedErrorMessage_part1 + "<id>" + expectedErrorMessage_part2 + "\n" +81"but got: " + errorMsg);82throw new RuntimeException("Wrong LinkageError exception thrown: " + errorMsg);83}84System.out.println("Passed with message: " + errorMsg);85}86}8788public static void main(String args[]) throws Exception {89test(null, expectedErrorMessage1_part1,90expectedErrorMessage1_part2, expectedErrorMessage1_part3);91test("VtableLdrCnstrnt_Test_Loader", expectedErrorMessage2_part1,92expectedErrorMessage2_part2, expectedErrorMessage2_part3);93}94}959697