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