/* * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ /* * @test * @compile ../common/Foo.java * ../common/PreemptingClassLoader.java * @run main/othervm Test * @run main/othervm -Xint Test * @run main/othervm -Xcomp Test * @run main/othervm -XX:+TieredCompilation -XX:TieredStopAtLevel=1 Test * @run main/othervm -XX:-TieredCompilation Test */ public class Test { // Check that all names have external formatting ('.' and not '/' in package names). // Check for parent of class loader. static String expectedErrorMessage1 = "loader instance of PreemptingClassLoader (parent: \"app\" " + "jdk.internal.loader.ClassLoaders$AppClassLoader) attempted duplicate class definition for test.Foo."; // Test that the error message is correct when a loader constraint error is // detected during vtable creation. // // In this test, during vtable creation for class Task, method "Task.m()LFoo;" // overrides "J.m()LFoo;". But, Task's class Foo and super type J's class Foo // are different. So, a LinkageError exception should be thrown because the // loader constraint check will fail. public static void test1() throws Exception { String[] classNames = {"test.Foo"}; ClassLoader l = new PreemptingClassLoader(null, classNames, false); l.loadClass("test.Foo"); try { l.loadClass("test.Foo").newInstance(); throw new RuntimeException("Expected LinkageError exception not thrown"); } catch (LinkageError e) { String errorMsg = e.getMessage(); if (!errorMsg.equals(expectedErrorMessage1)) { System.out.println("Expected: " + expectedErrorMessage1 + "\n" + "but got: " + errorMsg); throw new RuntimeException("Wrong LinkageError exception thrown: " + errorMsg); } System.out.println("Passed with message: " + errorMsg); } } // Check that all names have external formatting ('.' and not '/' in package names). // Check for name and parent of class loader. static String expectedErrorMessage2 = "loader \"DuplicateLE_Test_Loader\" (instance of PreemptingClassLoader, parent: \"app\" " + "jdk.internal.loader.ClassLoaders$AppClassLoader) attempted duplicate class definition for test.Foo."; // Same as test1, but ClassLoader has a name. public static void test2() throws Exception { String[] classNames = {"test.Foo"}; ClassLoader l = new PreemptingClassLoader("DuplicateLE_Test_Loader", classNames, false); l.loadClass("test.Foo"); try { l.loadClass("test.Foo"); throw new RuntimeException("Expected LinkageError exception not thrown"); } catch (LinkageError e) { String errorMsg = e.getMessage(); if (!errorMsg.equals(expectedErrorMessage2)) { System.out.println("Expected: " + expectedErrorMessage2 + "\n" + "but got: " + errorMsg); throw new RuntimeException("Wrong LinkageError exception thrown: " + errorMsg); } System.out.println("Passed with message: " + errorMsg); } } public static void main(String args[]) throws Exception { test1(); test2(); } }