Path: blob/master/test/hotspot/jtreg/runtime/LoaderConstraints/itableLdrConstraint/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 in interface itable initialization for " +40"class test.C: when selecting method 'test.Foo test.I.m()' the class loader " +41"PreemptingClassLoader @";42static String expectedErrorMessage1_part2 = " for super interface test.I, and the class loader 'app' of the " +43"selected method's interface, test.J have different Class objects for the " +44"type test.Foo used in the signature (test.I is in unnamed module of loader " +45"PreemptingClassLoader @";46static String expectedErrorMessage1_part3 = ", parent loader 'app'; test.J is in unnamed module of loader 'app')";4748static String expectedErrorMessage2_part1 = "loader constraint violation in interface itable initialization for " +49"class test.C: when selecting method 'test.Foo test.I.m()' the class loader " +50"'ItableLdrCnstrnt_Test_Loader' @";51static String expectedErrorMessage2_part2 = " for super interface test.I, and the class loader 'app' of the " +52"selected method's interface, test.J have different Class objects for the " +53"type test.Foo used in the signature (test.I is in unnamed module of loader " +54"'ItableLdrCnstrnt_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 itable creation.59//60// In this test, during itable creation for class C, method "m()LFoo;" for61// C's super interface I has a different class Foo than the selected method's62// type super interface J. The selected method is not an overpass method nor63// otherwise excluded from loader constraint checking. So, a LinkageError64// exception should be thrown because the loader constraint check will fail.65public static void test(String loaderName,66String expectedErrorMessage_part1,67String expectedErrorMessage_part2,68String expectedErrorMessage_part3) throws Exception {69Class<?> c = test.Foo.class; // Forces standard class loader to load Foo.70String[] classNames = {"test.Task", "test.Foo", "test.C", "test.I"};71ClassLoader l = new PreemptingClassLoader(loaderName, classNames);72Runnable r = (Runnable) l.loadClass("test.Task").newInstance();73try {74r.run();75throw new RuntimeException("Expected LinkageError exception not thrown");76} catch (LinkageError e) {77String errorMsg = e.getMessage();78if (!errorMsg.contains(expectedErrorMessage_part1) ||79!errorMsg.contains(expectedErrorMessage_part2) ||80!errorMsg.contains(expectedErrorMessage_part3)) {81System.out.println("Expected: " + expectedErrorMessage_part1 + "<id>" +82expectedErrorMessage_part2 + "<id>" +83expectedErrorMessage_part3 + "\n" +84"but got: " + errorMsg);85throw new RuntimeException("Wrong LinkageError exception thrown: " + errorMsg);86}87System.out.println("Passed with message: " + errorMsg);88}89}9091public static void main(String... args) throws Exception {92test(null, expectedErrorMessage1_part1, expectedErrorMessage1_part2, expectedErrorMessage1_part3);93test("ItableLdrCnstrnt_Test_Loader", expectedErrorMessage2_part1, expectedErrorMessage2_part2, expectedErrorMessage2_part3);94}95}969798