test/hotspot/jtreg/runtime/modules/CCE_module_msg.java
author lfoltan
Mon, 25 Jun 2018 11:33:11 -0400
changeset 50761 cb07f4b539fc
parent 50569 60d66a249db6
permissions -rw-r--r--
8169559: Add class loader names to relevant VM messages Summary: Added new method class_in_module_of_loader to provide a standard format for class information within error messages. Reviewed-by: goetz, hseigel, mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
50569
60d66a249db6 8204620: ModuleEntry::is_non_jdk_module() determination for what is a jdk module is incorrect
hseigel
parents: 48791
diff changeset
     2
 * Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
     4
 *
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
     8
 *
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    13
 * accompanied this code).
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    14
 *
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    18
 *
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    21
 * questions.
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    22
 */
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    23
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    24
/**
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    25
 * @test
42575
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
    26
 * @modules java.base/jdk.internal.misc
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
    27
 * @library /test/lib ..
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
    28
 * @compile p2/c2.java
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
    29
 * @compile p4/c4.java
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
    30
 * @build sun.hotspot.WhiteBox
44520
0553e129e0ec 8177530: Module system implementation refresh (4/2017)
alanb
parents: 43665
diff changeset
    31
 * @compile/module=java.base java/lang/ModuleHelper.java
48791
6e079ff6c83c 8186635: ClassFileInstaller should be run as a driver
iignatyev
parents: 47216
diff changeset
    32
 * @run driver ClassFileInstaller sun.hotspot.WhiteBox
42575
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
    33
 *                              sun.hotspot.WhiteBox$WhiteBoxPermission
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
    34
 * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI CCE_module_msg
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    35
 */
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    36
42575
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
    37
import java.io.*;
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
    38
import java.net.URL;
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
    39
import java.net.URLClassLoader;
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
    40
import java.nio.file.Path;
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
    41
import java.nio.file.Paths;
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
    42
import static jdk.test.lib.Asserts.*;
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
    43
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    44
// Test that the message in a runtime ClassCastException contains module info.
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    45
public class CCE_module_msg {
42575
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
    46
    private static final Path CLASSES_DIR = Paths.get("classes");
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    47
42575
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
    48
    public static void main(String[] args) throws Throwable {
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
    49
        // Should not display version
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
    50
        invalidObjectToDerived();
50761
cb07f4b539fc 8169559: Add class loader names to relevant VM messages
lfoltan
parents: 50569
diff changeset
    51
        invalidOriginalInnerToDerived();
50569
60d66a249db6 8204620: ModuleEntry::is_non_jdk_module() determination for what is a jdk module is incorrect
hseigel
parents: 48791
diff changeset
    52
        invalidTimeToDerived();
60d66a249db6 8204620: ModuleEntry::is_non_jdk_module() determination for what is a jdk module is incorrect
hseigel
parents: 48791
diff changeset
    53
        invalidHeadersToDerived();
42575
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
    54
        // Should display version
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
    55
        invalidClassToString();
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
    56
        // Should display customer class loader
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
    57
        invalidClassToStringCustomLoader();
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    58
    }
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    59
42575
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
    60
    public static void invalidObjectToDerived() {
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    61
        java.lang.Object instance = new java.lang.Object();
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    62
        int left = 23;
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    63
        int right = 42;
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    64
        try {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    65
            for (int i = 0; i < 1; i += 1) {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    66
                left = ((Derived) instance).method(left, right);
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    67
            }
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    68
            throw new RuntimeException("ClassCastException wasn't thrown, test failed.");
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    69
        } catch (ClassCastException cce) {
50761
cb07f4b539fc 8169559: Add class loader names to relevant VM messages
lfoltan
parents: 50569
diff changeset
    70
            System.out.println(cce.toString());
cb07f4b539fc 8169559: Add class loader names to relevant VM messages
lfoltan
parents: 50569
diff changeset
    71
            if (!cce.getMessage().contains("class java.lang.Object cannot be cast to class Derived (java.lang.Object is in module java.base of loader 'bootstrap'; Derived is in unnamed module of loader 'app')")) {
cb07f4b539fc 8169559: Add class loader names to relevant VM messages
lfoltan
parents: 50569
diff changeset
    72
                throw new RuntimeException("Wrong message: " + cce.getMessage());
cb07f4b539fc 8169559: Add class loader names to relevant VM messages
lfoltan
parents: 50569
diff changeset
    73
            }
cb07f4b539fc 8169559: Add class loader names to relevant VM messages
lfoltan
parents: 50569
diff changeset
    74
        }
cb07f4b539fc 8169559: Add class loader names to relevant VM messages
lfoltan
parents: 50569
diff changeset
    75
    }
cb07f4b539fc 8169559: Add class loader names to relevant VM messages
lfoltan
parents: 50569
diff changeset
    76
cb07f4b539fc 8169559: Add class loader names to relevant VM messages
lfoltan
parents: 50569
diff changeset
    77
    public static void invalidOriginalInnerToDerived() {
cb07f4b539fc 8169559: Add class loader names to relevant VM messages
lfoltan
parents: 50569
diff changeset
    78
        OriginalInner instance = new OriginalInner();
cb07f4b539fc 8169559: Add class loader names to relevant VM messages
lfoltan
parents: 50569
diff changeset
    79
        int left = 23;
cb07f4b539fc 8169559: Add class loader names to relevant VM messages
lfoltan
parents: 50569
diff changeset
    80
        int right = 42;
cb07f4b539fc 8169559: Add class loader names to relevant VM messages
lfoltan
parents: 50569
diff changeset
    81
        try {
cb07f4b539fc 8169559: Add class loader names to relevant VM messages
lfoltan
parents: 50569
diff changeset
    82
            for (int i = 0; i < 1; i += 1) {
cb07f4b539fc 8169559: Add class loader names to relevant VM messages
lfoltan
parents: 50569
diff changeset
    83
                left = ((Derived) (java.lang.Object)instance).method(left, right);
cb07f4b539fc 8169559: Add class loader names to relevant VM messages
lfoltan
parents: 50569
diff changeset
    84
            }
cb07f4b539fc 8169559: Add class loader names to relevant VM messages
lfoltan
parents: 50569
diff changeset
    85
            throw new RuntimeException("ClassCastException wasn't thrown, test failed.");
cb07f4b539fc 8169559: Add class loader names to relevant VM messages
lfoltan
parents: 50569
diff changeset
    86
        } catch (ClassCastException cce) {
cb07f4b539fc 8169559: Add class loader names to relevant VM messages
lfoltan
parents: 50569
diff changeset
    87
            System.out.println(cce.toString());
cb07f4b539fc 8169559: Add class loader names to relevant VM messages
lfoltan
parents: 50569
diff changeset
    88
            if (!cce.getMessage().contains("class OriginalInner cannot be cast to class Derived (OriginalInner and Derived are in unnamed module of loader 'app')")) {
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    89
                throw new RuntimeException("Wrong message: " + cce.getMessage());
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    90
            }
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    91
        }
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
    92
    }
42575
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
    93
50569
60d66a249db6 8204620: ModuleEntry::is_non_jdk_module() determination for what is a jdk module is incorrect
hseigel
parents: 48791
diff changeset
    94
    // Test with a non-upgradeable 'java.' module other than java.base.
60d66a249db6 8204620: ModuleEntry::is_non_jdk_module() determination for what is a jdk module is incorrect
hseigel
parents: 48791
diff changeset
    95
    public static void invalidTimeToDerived() {
60d66a249db6 8204620: ModuleEntry::is_non_jdk_module() determination for what is a jdk module is incorrect
hseigel
parents: 48791
diff changeset
    96
        java.sql.Time instance = new java.sql.Time(10000);
60d66a249db6 8204620: ModuleEntry::is_non_jdk_module() determination for what is a jdk module is incorrect
hseigel
parents: 48791
diff changeset
    97
        int left = 23;
60d66a249db6 8204620: ModuleEntry::is_non_jdk_module() determination for what is a jdk module is incorrect
hseigel
parents: 48791
diff changeset
    98
        int right = 42;
60d66a249db6 8204620: ModuleEntry::is_non_jdk_module() determination for what is a jdk module is incorrect
hseigel
parents: 48791
diff changeset
    99
        try {
60d66a249db6 8204620: ModuleEntry::is_non_jdk_module() determination for what is a jdk module is incorrect
hseigel
parents: 48791
diff changeset
   100
            for (int i = 0; i < 1; i += 1) {
60d66a249db6 8204620: ModuleEntry::is_non_jdk_module() determination for what is a jdk module is incorrect
hseigel
parents: 48791
diff changeset
   101
                left = ((Derived) (java.lang.Object)instance).method(left, right);
60d66a249db6 8204620: ModuleEntry::is_non_jdk_module() determination for what is a jdk module is incorrect
hseigel
parents: 48791
diff changeset
   102
            }
60d66a249db6 8204620: ModuleEntry::is_non_jdk_module() determination for what is a jdk module is incorrect
hseigel
parents: 48791
diff changeset
   103
            throw new RuntimeException("ClassCastException wasn't thrown, test failed.");
60d66a249db6 8204620: ModuleEntry::is_non_jdk_module() determination for what is a jdk module is incorrect
hseigel
parents: 48791
diff changeset
   104
        } catch (ClassCastException cce) {
50761
cb07f4b539fc 8169559: Add class loader names to relevant VM messages
lfoltan
parents: 50569
diff changeset
   105
            System.out.println(cce.toString());
cb07f4b539fc 8169559: Add class loader names to relevant VM messages
lfoltan
parents: 50569
diff changeset
   106
            if (!cce.getMessage().contains("class java.sql.Time cannot be cast to class Derived (java.sql.Time is in module java.sql of loader 'platform'; Derived is in unnamed module of loader 'app')")) {
50569
60d66a249db6 8204620: ModuleEntry::is_non_jdk_module() determination for what is a jdk module is incorrect
hseigel
parents: 48791
diff changeset
   107
                throw new RuntimeException("Wrong message: " + cce.getMessage());
60d66a249db6 8204620: ModuleEntry::is_non_jdk_module() determination for what is a jdk module is incorrect
hseigel
parents: 48791
diff changeset
   108
            }
60d66a249db6 8204620: ModuleEntry::is_non_jdk_module() determination for what is a jdk module is incorrect
hseigel
parents: 48791
diff changeset
   109
        }
60d66a249db6 8204620: ModuleEntry::is_non_jdk_module() determination for what is a jdk module is incorrect
hseigel
parents: 48791
diff changeset
   110
    }
60d66a249db6 8204620: ModuleEntry::is_non_jdk_module() determination for what is a jdk module is incorrect
hseigel
parents: 48791
diff changeset
   111
60d66a249db6 8204620: ModuleEntry::is_non_jdk_module() determination for what is a jdk module is incorrect
hseigel
parents: 48791
diff changeset
   112
    // Test with a non-upgradeable 'jdk.' module.
60d66a249db6 8204620: ModuleEntry::is_non_jdk_module() determination for what is a jdk module is incorrect
hseigel
parents: 48791
diff changeset
   113
    public static void invalidHeadersToDerived() {
60d66a249db6 8204620: ModuleEntry::is_non_jdk_module() determination for what is a jdk module is incorrect
hseigel
parents: 48791
diff changeset
   114
        com.sun.net.httpserver.Headers instance = new com.sun.net.httpserver.Headers();
60d66a249db6 8204620: ModuleEntry::is_non_jdk_module() determination for what is a jdk module is incorrect
hseigel
parents: 48791
diff changeset
   115
        int left = 23;
60d66a249db6 8204620: ModuleEntry::is_non_jdk_module() determination for what is a jdk module is incorrect
hseigel
parents: 48791
diff changeset
   116
        int right = 42;
60d66a249db6 8204620: ModuleEntry::is_non_jdk_module() determination for what is a jdk module is incorrect
hseigel
parents: 48791
diff changeset
   117
        try {
60d66a249db6 8204620: ModuleEntry::is_non_jdk_module() determination for what is a jdk module is incorrect
hseigel
parents: 48791
diff changeset
   118
            for (int i = 0; i < 1; i += 1) {
60d66a249db6 8204620: ModuleEntry::is_non_jdk_module() determination for what is a jdk module is incorrect
hseigel
parents: 48791
diff changeset
   119
                left = ((Derived) (java.lang.Object)instance).method(left, right);
60d66a249db6 8204620: ModuleEntry::is_non_jdk_module() determination for what is a jdk module is incorrect
hseigel
parents: 48791
diff changeset
   120
            }
60d66a249db6 8204620: ModuleEntry::is_non_jdk_module() determination for what is a jdk module is incorrect
hseigel
parents: 48791
diff changeset
   121
            throw new RuntimeException("ClassCastException wasn't thrown, test failed.");
60d66a249db6 8204620: ModuleEntry::is_non_jdk_module() determination for what is a jdk module is incorrect
hseigel
parents: 48791
diff changeset
   122
        } catch (ClassCastException cce) {
50761
cb07f4b539fc 8169559: Add class loader names to relevant VM messages
lfoltan
parents: 50569
diff changeset
   123
            System.out.println(cce.toString());
cb07f4b539fc 8169559: Add class loader names to relevant VM messages
lfoltan
parents: 50569
diff changeset
   124
            if (!cce.getMessage().contains("class com.sun.net.httpserver.Headers cannot be cast to class Derived (com.sun.net.httpserver.Headers is in module jdk.httpserver of loader 'platform'; Derived is in unnamed module of loader 'app')")) {
50569
60d66a249db6 8204620: ModuleEntry::is_non_jdk_module() determination for what is a jdk module is incorrect
hseigel
parents: 48791
diff changeset
   125
                throw new RuntimeException("Wrong message: " + cce.getMessage());
60d66a249db6 8204620: ModuleEntry::is_non_jdk_module() determination for what is a jdk module is incorrect
hseigel
parents: 48791
diff changeset
   126
            }
60d66a249db6 8204620: ModuleEntry::is_non_jdk_module() determination for what is a jdk module is incorrect
hseigel
parents: 48791
diff changeset
   127
        }
60d66a249db6 8204620: ModuleEntry::is_non_jdk_module() determination for what is a jdk module is incorrect
hseigel
parents: 48791
diff changeset
   128
    }
60d66a249db6 8204620: ModuleEntry::is_non_jdk_module() determination for what is a jdk module is incorrect
hseigel
parents: 48791
diff changeset
   129
42575
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
   130
    public static void invalidClassToString() throws Throwable {
44520
0553e129e0ec 8177530: Module system implementation refresh (4/2017)
alanb
parents: 43665
diff changeset
   131
        // Get the java.lang.Module object for module java.base.
42575
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
   132
        Class jlObject = Class.forName("java.lang.Object");
44520
0553e129e0ec 8177530: Module system implementation refresh (4/2017)
alanb
parents: 43665
diff changeset
   133
        Object jlObject_jlM = jlObject.getModule();
0553e129e0ec 8177530: Module system implementation refresh (4/2017)
alanb
parents: 43665
diff changeset
   134
        assertNotNull(jlObject_jlM, "jlModule object of java.lang.Object should not be null");
42575
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
   135
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
   136
        // Get the class loader for CCE_module_msg and assume it's also used to
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
   137
        // load classes p1.c1 and p2.c2.
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
   138
        ClassLoader this_cldr = CCE_module_msg.class.getClassLoader();
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
   139
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
   140
        // Define a module for p2.
43665
4bb003cad9b9 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42575
diff changeset
   141
        Object m2x = ModuleHelper.ModuleObject("module_two", this_cldr, new String[] { "p2" });
4bb003cad9b9 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42575
diff changeset
   142
        assertNotNull(m2x, "Module should not be null");
46404
ae62ba99a1a7 8165896: Use "open" flag from JVM_DefineModule to export all module packages
rprotacio
parents: 44520
diff changeset
   143
        ModuleHelper.DefineModule(m2x, false, "9.0", "m2x/there", new String[] { "p2" });
44520
0553e129e0ec 8177530: Module system implementation refresh (4/2017)
alanb
parents: 43665
diff changeset
   144
        ModuleHelper.AddReadsModule(m2x, jlObject_jlM);
42575
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
   145
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
   146
        try {
43665
4bb003cad9b9 8173393: Module system implementation refresh (2/2017)
alanb
parents: 42575
diff changeset
   147
            ModuleHelper.AddModuleExportsToAll(m2x, "p2");
42575
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
   148
            Object p2Obj = new p2.c2();
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
   149
            System.out.println((String)p2Obj);
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
   150
            throw new RuntimeException("ClassCastException wasn't thrown, test failed.");
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
   151
        } catch (ClassCastException cce) {
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
   152
            String exception = cce.getMessage();
50761
cb07f4b539fc 8169559: Add class loader names to relevant VM messages
lfoltan
parents: 50569
diff changeset
   153
            System.out.println(cce.toString());
cb07f4b539fc 8169559: Add class loader names to relevant VM messages
lfoltan
parents: 50569
diff changeset
   154
            if (!exception.contains("class p2.c2 cannot be cast to class java.lang.String (p2.c2 is in module module_two@") ||
cb07f4b539fc 8169559: Add class loader names to relevant VM messages
lfoltan
parents: 50569
diff changeset
   155
                !exception.contains(" of loader 'app'; java.lang.String is in module java.base of loader 'bootstrap')")) {
42575
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
   156
                throw new RuntimeException("Wrong message: " + exception);
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
   157
            }
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
   158
        }
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
   159
    }
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
   160
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
   161
    public static void invalidClassToStringCustomLoader() throws Throwable {
44520
0553e129e0ec 8177530: Module system implementation refresh (4/2017)
alanb
parents: 43665
diff changeset
   162
        // Get the java.lang.Module object for module java.base.
42575
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
   163
        Class jlObject = Class.forName("java.lang.Object");
44520
0553e129e0ec 8177530: Module system implementation refresh (4/2017)
alanb
parents: 43665
diff changeset
   164
        Object jlObject_jlM = jlObject.getModule();
0553e129e0ec 8177530: Module system implementation refresh (4/2017)
alanb
parents: 43665
diff changeset
   165
        assertNotNull(jlObject_jlM, "jlModule object of java.lang.Object should not be null");
42575
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
   166
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
   167
        // Create a customer class loader to load class p4/c4.
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
   168
        URL[] urls = new URL[] { CLASSES_DIR.toUri().toURL() };
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
   169
        ClassLoader parent = ClassLoader.getSystemClassLoader();
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
   170
        MyURLClassLoader myCldr = new MyURLClassLoader("MyClassLoader", urls, parent);
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
   171
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
   172
        try {
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
   173
            // Class p4.c4 should be defined to the unnamed module of myCldr
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
   174
            Class p4_c4_class = myCldr.loadClass("p4.c4");
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
   175
            Object c4Obj = p4_c4_class.newInstance();
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
   176
            System.out.println((String)c4Obj);
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
   177
            throw new RuntimeException("ClassCastException wasn't thrown, test failed.");
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
   178
        } catch (ClassCastException cce) {
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
   179
            String exception = cce.getMessage();
50761
cb07f4b539fc 8169559: Add class loader names to relevant VM messages
lfoltan
parents: 50569
diff changeset
   180
            System.out.println(cce.toString());
cb07f4b539fc 8169559: Add class loader names to relevant VM messages
lfoltan
parents: 50569
diff changeset
   181
            if (!exception.contains("class p4.c4 cannot be cast to class java.lang.String (p4.c4 is in unnamed module of loader 'MyClassLoader' @") ||
cb07f4b539fc 8169559: Add class loader names to relevant VM messages
lfoltan
parents: 50569
diff changeset
   182
                !exception.contains("; java.lang.String is in module java.base of loader 'bootstrap')")) {
42575
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
   183
                throw new RuntimeException("Wrong message: " + exception);
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
   184
            }
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
   185
        }
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
   186
    }
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   187
}
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   188
50761
cb07f4b539fc 8169559: Add class loader names to relevant VM messages
lfoltan
parents: 50569
diff changeset
   189
class OriginalInner extends java.lang.Object {
cb07f4b539fc 8169559: Add class loader names to relevant VM messages
lfoltan
parents: 50569
diff changeset
   190
    public int method(int left, int right) {
cb07f4b539fc 8169559: Add class loader names to relevant VM messages
lfoltan
parents: 50569
diff changeset
   191
        return right;
cb07f4b539fc 8169559: Add class loader names to relevant VM messages
lfoltan
parents: 50569
diff changeset
   192
    }
cb07f4b539fc 8169559: Add class loader names to relevant VM messages
lfoltan
parents: 50569
diff changeset
   193
}
cb07f4b539fc 8169559: Add class loader names to relevant VM messages
lfoltan
parents: 50569
diff changeset
   194
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   195
class Derived extends java.lang.Object {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   196
    public int method(int left, int right) {
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   197
        return right;
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   198
    }
5f9eee6b383b 8142968: Module System implementation
alanb
parents:
diff changeset
   199
}
42575
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
   200
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
   201
class MyURLClassLoader extends URLClassLoader {
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
   202
    public MyURLClassLoader(String name,
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
   203
                          URL[] urls,
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
   204
                          ClassLoader parent) {
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
   205
        super(name, urls, parent);
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
   206
    }
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
   207
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
   208
    public Class loadClass(String name) throws ClassNotFoundException {
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
   209
        if (!name.equals("p4.c4")) {
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
   210
            return super.loadClass(name);
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
   211
        }
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
   212
        byte[] data = getClassData(name);
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
   213
        return defineClass(name, data, 0, data.length);
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
   214
    }
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
   215
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
   216
    byte[] getClassData(String name) {
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
   217
        try {
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
   218
           String TempName = name.replaceAll("\\.", "/");
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
   219
           String currentDir = System.getProperty("test.classes");
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
   220
           String filename = currentDir + File.separator + TempName + ".class";
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
   221
           FileInputStream fis = new FileInputStream(filename);
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
   222
           byte[] b = new byte[5000];
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
   223
           int cnt = fis.read(b, 0, 5000);
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
   224
           byte[] c = new byte[cnt];
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
   225
           for (int i=0; i<cnt; i++) c[i] = b[i];
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
   226
              return c;
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
   227
        } catch (IOException e) {
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
   228
           return null;
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
   229
        }
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
   230
    }
bd1618170c93 8165550: Add class loader names to ClassCastException message
rprotacio
parents: 36508
diff changeset
   231
}