test/hotspot/jtreg/runtime/cds/appcds/test-classes/JvmtiApp.java
author jiefu
Wed, 06 Nov 2019 13:43:25 +0800
changeset 58944 bb2a436e616c
parent 57567 b000362a89a0
permissions -rw-r--r--
8233659: [TESTBUG] runtime/cds/appcds/CommandLineFlagCombo.java fails when jfr is disabled Reviewed-by: iklam
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
48138
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
     1
/*
57567
b000362a89a0 8202339: [TESTBUG] Consolidate the tests in runtime/SharedArchiveFile and runtime/appcds
coleenp
parents: 49739
diff changeset
     2
 * Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved.
48138
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
     4
 *
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
     7
 * published by the Free Software Foundation.
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
     8
 *
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    13
 * accompanied this code).
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    14
 *
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    18
 *
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    21
 * questions.
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    22
 *
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    23
 */
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    24
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    25
import sun.hotspot.WhiteBox;
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    26
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    27
public class JvmtiApp {
49739
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents: 48138
diff changeset
    28
    static Class forname(String cn) {
48138
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    29
        try {
49739
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents: 48138
diff changeset
    30
            return Class.forName(cn);
48138
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    31
        } catch (Throwable t) {
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    32
            return null;
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    33
        }
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    34
    }
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    35
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    36
    static void failed(String msg) {
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    37
        System.out.println("TEST FAILED: " + msg);
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    38
        System.exit(1);
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    39
    }
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    40
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    41
    // See ../JvmtiAddPath.java for how the classpaths are configured.
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    42
    public static void main(String args[]) {
49739
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents: 48138
diff changeset
    43
        String cn = "Hello";
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents: 48138
diff changeset
    44
        if (args.length >= 3) {
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents: 48138
diff changeset
    45
            cn = args[args.length - 1];
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents: 48138
diff changeset
    46
        }
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents: 48138
diff changeset
    47
48138
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    48
        if (args[0].equals("noadd")) {
49739
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents: 48138
diff changeset
    49
            if (forname(cn) != null) {
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents: 48138
diff changeset
    50
                failed(cn + " class was loaded unexpectedly");
48138
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    51
            }
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    52
            // We use -verbose:class to verify that Extra.class IS loaded by AppCDS if
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    53
            // the boot classpath HAS NOT been appended.
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    54
            ExtraClass.doit();
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    55
            System.exit(0);
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    56
        }
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    57
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    58
        WhiteBox wb = WhiteBox.getWhiteBox();
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    59
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    60
        if (args[0].equals("bootonly")) {
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    61
            wb.addToBootstrapClassLoaderSearch(args[1]);
49739
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents: 48138
diff changeset
    62
            Class cls = forname(cn);
48138
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    63
            if (cls == null) {
49739
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents: 48138
diff changeset
    64
                failed("Cannot find " + cn + " class");
48138
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    65
            }
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    66
            if (cls.getClassLoader() != null) {
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    67
                failed("Hello class not loaded by boot classloader");
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    68
            }
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    69
        } else if (args[0].equals("apponly")) {
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    70
            wb.addToSystemClassLoaderSearch(args[1]);
49739
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents: 48138
diff changeset
    71
            Class cls = forname(cn);
48138
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    72
            if (cls == null) {
49739
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents: 48138
diff changeset
    73
                failed("Cannot find " + cn + " class");
48138
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    74
            }
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    75
            if (cls.getClassLoader() != JvmtiApp.class.getClassLoader()) {
49739
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents: 48138
diff changeset
    76
                failed(cn + " class not loaded by app classloader");
48138
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    77
            }
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    78
        } else if (args[0].equals("noadd-appcds")) {
49739
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents: 48138
diff changeset
    79
            cn = (args.length == 1) ? "Hello" : args[1];
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents: 48138
diff changeset
    80
            Class cls = forname(cn);
48138
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    81
            if (cls == null) {
49739
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents: 48138
diff changeset
    82
                failed("Cannot find " + cn + " class");
48138
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    83
            }
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    84
            if (cls.getClassLoader() != JvmtiApp.class.getClassLoader()) {
49739
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents: 48138
diff changeset
    85
                failed(cn + " class not loaded by app classloader");
48138
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    86
            }
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    87
        } else if (args[0].equals("appandboot")) {
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    88
            wb.addToBootstrapClassLoaderSearch(args[1]);
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    89
            wb.addToSystemClassLoaderSearch(args[2]);
49739
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents: 48138
diff changeset
    90
            cn = (args.length == 3) ? "Hello" : args[3];
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents: 48138
diff changeset
    91
            Class cls = forname(cn);
48138
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    92
            if (cls == null) {
49739
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents: 48138
diff changeset
    93
                failed("Cannot find " + cn + " class");
48138
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    94
            }
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    95
            if (cls.getClassLoader() != null) {
49739
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents: 48138
diff changeset
    96
                failed(cn + " class not loaded by boot classloader");
48138
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    97
            }
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    98
        } else {
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
    99
            failed("unknown option " + args[0]);
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   100
        }
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   101
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   102
        // We use -verbose:class to verify that Extra.class IS NOT loaded by AppCDS if
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   103
        // the boot classpath HAS been appended.
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   104
        ExtraClass.doit();
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   105
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   106
        System.out.println("Test passed: " + args[0]);
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   107
    }
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   108
}
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   109
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   110
class ExtraClass {
78b2ecdd3c4b 8188791: Move AppCDS from closed repo to open repo
iklam
parents:
diff changeset
   111
    static void doit() {}
49739
00805b129186 8194812: Extend class-data sharing to support the module path
ccheung
parents: 48138
diff changeset
   112
}