test/jdk/java/lang/Class/GetPackageBootLoaderChildLayer.java
author kvn
Mon, 09 Jul 2018 22:20:02 -0700
changeset 51022 4df86b2a804d
parent 50744 6c306d54366d
permissions -rw-r--r--
8206952: java/lang/Class/GetPackageBootLoaderChildLayer.java fails with Graal Summary: don't run test with Graal Reviewed-by: mchung, epavlova
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50744
6c306d54366d 8205533: Class.getPackage() fails with InternalError if class is defined to the bootstrap class loader but module is not in the boot layer
alanb
parents:
diff changeset
     1
/*
6c306d54366d 8205533: Class.getPackage() fails with InternalError if class is defined to the bootstrap class loader but module is not in the boot layer
alanb
parents:
diff changeset
     2
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
6c306d54366d 8205533: Class.getPackage() fails with InternalError if class is defined to the bootstrap class loader but module is not in the boot layer
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6c306d54366d 8205533: Class.getPackage() fails with InternalError if class is defined to the bootstrap class loader but module is not in the boot layer
alanb
parents:
diff changeset
     4
 *
6c306d54366d 8205533: Class.getPackage() fails with InternalError if class is defined to the bootstrap class loader but module is not in the boot layer
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
6c306d54366d 8205533: Class.getPackage() fails with InternalError if class is defined to the bootstrap class loader but module is not in the boot layer
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
6c306d54366d 8205533: Class.getPackage() fails with InternalError if class is defined to the bootstrap class loader but module is not in the boot layer
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
6c306d54366d 8205533: Class.getPackage() fails with InternalError if class is defined to the bootstrap class loader but module is not in the boot layer
alanb
parents:
diff changeset
     8
 *
6c306d54366d 8205533: Class.getPackage() fails with InternalError if class is defined to the bootstrap class loader but module is not in the boot layer
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
6c306d54366d 8205533: Class.getPackage() fails with InternalError if class is defined to the bootstrap class loader but module is not in the boot layer
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
6c306d54366d 8205533: Class.getPackage() fails with InternalError if class is defined to the bootstrap class loader but module is not in the boot layer
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
6c306d54366d 8205533: Class.getPackage() fails with InternalError if class is defined to the bootstrap class loader but module is not in the boot layer
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
6c306d54366d 8205533: Class.getPackage() fails with InternalError if class is defined to the bootstrap class loader but module is not in the boot layer
alanb
parents:
diff changeset
    13
 * accompanied this code).
6c306d54366d 8205533: Class.getPackage() fails with InternalError if class is defined to the bootstrap class loader but module is not in the boot layer
alanb
parents:
diff changeset
    14
 *
6c306d54366d 8205533: Class.getPackage() fails with InternalError if class is defined to the bootstrap class loader but module is not in the boot layer
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
6c306d54366d 8205533: Class.getPackage() fails with InternalError if class is defined to the bootstrap class loader but module is not in the boot layer
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
6c306d54366d 8205533: Class.getPackage() fails with InternalError if class is defined to the bootstrap class loader but module is not in the boot layer
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
6c306d54366d 8205533: Class.getPackage() fails with InternalError if class is defined to the bootstrap class loader but module is not in the boot layer
alanb
parents:
diff changeset
    18
 *
6c306d54366d 8205533: Class.getPackage() fails with InternalError if class is defined to the bootstrap class loader but module is not in the boot layer
alanb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
6c306d54366d 8205533: Class.getPackage() fails with InternalError if class is defined to the bootstrap class loader but module is not in the boot layer
alanb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
6c306d54366d 8205533: Class.getPackage() fails with InternalError if class is defined to the bootstrap class loader but module is not in the boot layer
alanb
parents:
diff changeset
    21
 * questions.
6c306d54366d 8205533: Class.getPackage() fails with InternalError if class is defined to the bootstrap class loader but module is not in the boot layer
alanb
parents:
diff changeset
    22
 */
6c306d54366d 8205533: Class.getPackage() fails with InternalError if class is defined to the bootstrap class loader but module is not in the boot layer
alanb
parents:
diff changeset
    23
6c306d54366d 8205533: Class.getPackage() fails with InternalError if class is defined to the bootstrap class loader but module is not in the boot layer
alanb
parents:
diff changeset
    24
/**
6c306d54366d 8205533: Class.getPackage() fails with InternalError if class is defined to the bootstrap class loader but module is not in the boot layer
alanb
parents:
diff changeset
    25
 * @test
51022
4df86b2a804d 8206952: java/lang/Class/GetPackageBootLoaderChildLayer.java fails with Graal
kvn
parents: 50744
diff changeset
    26
 * @requires !vm.graal.enabled
50744
6c306d54366d 8205533: Class.getPackage() fails with InternalError if class is defined to the bootstrap class loader but module is not in the boot layer
alanb
parents:
diff changeset
    27
 * @modules jdk.attach
6c306d54366d 8205533: Class.getPackage() fails with InternalError if class is defined to the bootstrap class loader but module is not in the boot layer
alanb
parents:
diff changeset
    28
 * @run main/othervm --limit-modules jdk.attach -Djdk.attach.allowAttachSelf
6c306d54366d 8205533: Class.getPackage() fails with InternalError if class is defined to the bootstrap class loader but module is not in the boot layer
alanb
parents:
diff changeset
    29
 *    GetPackageBootLoaderChildLayer
6c306d54366d 8205533: Class.getPackage() fails with InternalError if class is defined to the bootstrap class loader but module is not in the boot layer
alanb
parents:
diff changeset
    30
 * @summary Exercise Class.getPackage on a class defined to the boot loader
6c306d54366d 8205533: Class.getPackage() fails with InternalError if class is defined to the bootstrap class loader but module is not in the boot layer
alanb
parents:
diff changeset
    31
 *    but in a module that is in a child layer rather than the boot layer
6c306d54366d 8205533: Class.getPackage() fails with InternalError if class is defined to the bootstrap class loader but module is not in the boot layer
alanb
parents:
diff changeset
    32
 */
6c306d54366d 8205533: Class.getPackage() fails with InternalError if class is defined to the bootstrap class loader but module is not in the boot layer
alanb
parents:
diff changeset
    33
6c306d54366d 8205533: Class.getPackage() fails with InternalError if class is defined to the bootstrap class loader but module is not in the boot layer
alanb
parents:
diff changeset
    34
import com.sun.tools.attach.VirtualMachine;
6c306d54366d 8205533: Class.getPackage() fails with InternalError if class is defined to the bootstrap class loader but module is not in the boot layer
alanb
parents:
diff changeset
    35
6c306d54366d 8205533: Class.getPackage() fails with InternalError if class is defined to the bootstrap class loader but module is not in the boot layer
alanb
parents:
diff changeset
    36
public class GetPackageBootLoaderChildLayer {
6c306d54366d 8205533: Class.getPackage() fails with InternalError if class is defined to the bootstrap class loader but module is not in the boot layer
alanb
parents:
diff changeset
    37
    public static void main(String[] args) throws Exception {
6c306d54366d 8205533: Class.getPackage() fails with InternalError if class is defined to the bootstrap class loader but module is not in the boot layer
alanb
parents:
diff changeset
    38
        // ensure that the java.management module is not in the boot layer
6c306d54366d 8205533: Class.getPackage() fails with InternalError if class is defined to the bootstrap class loader but module is not in the boot layer
alanb
parents:
diff changeset
    39
        ModuleLayer.boot().findModule("java.management").ifPresent(m -> {
6c306d54366d 8205533: Class.getPackage() fails with InternalError if class is defined to the bootstrap class loader but module is not in the boot layer
alanb
parents:
diff changeset
    40
            throw new RuntimeException("java.management loaded!!!");
6c306d54366d 8205533: Class.getPackage() fails with InternalError if class is defined to the bootstrap class loader but module is not in the boot layer
alanb
parents:
diff changeset
    41
        });
6c306d54366d 8205533: Class.getPackage() fails with InternalError if class is defined to the bootstrap class loader but module is not in the boot layer
alanb
parents:
diff changeset
    42
6c306d54366d 8205533: Class.getPackage() fails with InternalError if class is defined to the bootstrap class loader but module is not in the boot layer
alanb
parents:
diff changeset
    43
        // start local JMX agent via the attach mechanism
6c306d54366d 8205533: Class.getPackage() fails with InternalError if class is defined to the bootstrap class loader but module is not in the boot layer
alanb
parents:
diff changeset
    44
        String vmid = "" + ProcessHandle.current().pid();
6c306d54366d 8205533: Class.getPackage() fails with InternalError if class is defined to the bootstrap class loader but module is not in the boot layer
alanb
parents:
diff changeset
    45
        VirtualMachine vm = VirtualMachine.attach(vmid);
6c306d54366d 8205533: Class.getPackage() fails with InternalError if class is defined to the bootstrap class loader but module is not in the boot layer
alanb
parents:
diff changeset
    46
        vm.startLocalManagementAgent();
6c306d54366d 8205533: Class.getPackage() fails with InternalError if class is defined to the bootstrap class loader but module is not in the boot layer
alanb
parents:
diff changeset
    47
6c306d54366d 8205533: Class.getPackage() fails with InternalError if class is defined to the bootstrap class loader but module is not in the boot layer
alanb
parents:
diff changeset
    48
        // check layer, class loader, and Package object
6c306d54366d 8205533: Class.getPackage() fails with InternalError if class is defined to the bootstrap class loader but module is not in the boot layer
alanb
parents:
diff changeset
    49
        Class<?> clazz = Class.forName("javax.management.MXBean");
6c306d54366d 8205533: Class.getPackage() fails with InternalError if class is defined to the bootstrap class loader but module is not in the boot layer
alanb
parents:
diff changeset
    50
        if (clazz.getModule().getLayer() == ModuleLayer.boot())
6c306d54366d 8205533: Class.getPackage() fails with InternalError if class is defined to the bootstrap class loader but module is not in the boot layer
alanb
parents:
diff changeset
    51
            throw new RuntimeException("Module is in boot layer!!!");
6c306d54366d 8205533: Class.getPackage() fails with InternalError if class is defined to the bootstrap class loader but module is not in the boot layer
alanb
parents:
diff changeset
    52
        ClassLoader loader = clazz.getClassLoader();
6c306d54366d 8205533: Class.getPackage() fails with InternalError if class is defined to the bootstrap class loader but module is not in the boot layer
alanb
parents:
diff changeset
    53
        if (loader != null)
6c306d54366d 8205533: Class.getPackage() fails with InternalError if class is defined to the bootstrap class loader but module is not in the boot layer
alanb
parents:
diff changeset
    54
            throw new RuntimeException("Unexpected class loader: " + loader);
6c306d54366d 8205533: Class.getPackage() fails with InternalError if class is defined to the bootstrap class loader but module is not in the boot layer
alanb
parents:
diff changeset
    55
        Package p = clazz.getPackage();
6c306d54366d 8205533: Class.getPackage() fails with InternalError if class is defined to the bootstrap class loader but module is not in the boot layer
alanb
parents:
diff changeset
    56
        if (!p.getName().equals("javax.management"))
6c306d54366d 8205533: Class.getPackage() fails with InternalError if class is defined to the bootstrap class loader but module is not in the boot layer
alanb
parents:
diff changeset
    57
            throw new RuntimeException("Unexpected package " + p);
6c306d54366d 8205533: Class.getPackage() fails with InternalError if class is defined to the bootstrap class loader but module is not in the boot layer
alanb
parents:
diff changeset
    58
    }
6c306d54366d 8205533: Class.getPackage() fails with InternalError if class is defined to the bootstrap class loader but module is not in the boot layer
alanb
parents:
diff changeset
    59
}