jdk/test/java/lang/Class/GetModuleTest.java
author amlu
Fri, 18 Nov 2016 14:39:03 +0800
changeset 42166 5582c83b1156
parent 40964 c4a7c6d1cfb3
child 42338 a60f280f803c
permissions -rw-r--r--
8169836: ProblemList update for java/lang/management/MemoryMXBean/PendingAllGC.sh Reviewed-by: mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
40684
2e37c119dc2a 8164982: Fix legal notices in java/lang, java/net, java/util tests.
shurailine
parents: 38966
diff changeset
     1
/*
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     2
 * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     4
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     8
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    13
 * accompanied this code).
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    14
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    18
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    21
 * questions.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    22
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    23
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    24
/*
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    25
 * @test
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    26
 * @summary Exercise Class#getModule
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    27
 * @modules java.base/jdk.internal.org.objectweb.asm
38966
308f5cc7d5f6 8158855: Fix remaining module dependences in java/lang
shurailine
parents: 36511
diff changeset
    28
 *          java.base/jdk.internal.misc
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    29
 *          java.desktop
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    30
 * @run testng GetModuleTest
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    31
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    33
import java.awt.Component;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
import java.lang.reflect.Field;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
import java.lang.reflect.Module;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
import jdk.internal.org.objectweb.asm.ClassWriter;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
import static jdk.internal.org.objectweb.asm.Opcodes.*;
38966
308f5cc7d5f6 8158855: Fix remaining module dependences in java/lang
shurailine
parents: 36511
diff changeset
    39
import jdk.internal.misc.Unsafe;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
import org.testng.annotations.DataProvider;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
import org.testng.annotations.Test;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
import static org.testng.Assert.*;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
public class GetModuleTest {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    46
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    47
    static final Unsafe U;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
    static {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
        try {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
            Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
            theUnsafe.setAccessible(true);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
            U = (Unsafe) theUnsafe.get(null);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
        } catch (Exception e) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    54
            throw new AssertionError(e);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    55
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    56
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    57
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    58
    private static final Module TEST_MODULE = GetModuleTest.class.getModule();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    59
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    60
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    61
    @DataProvider(name = "testclasses")
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    62
    public Object[][] testClasses() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    63
        return new Object[][] {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    65
            // unnamed module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    66
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    67
            { GetModuleTest.class,      null },
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    68
            { GetModuleTest[].class,    null },
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    69
            { GetModuleTest[][].class,  null },
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    71
            // should return named module
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    72
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    73
            { int.class,            "java.base" },
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
            { int[].class,          "java.base" },
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    75
            { int[][].class,        "java.base" },
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    76
            { void.class,           "java.base" },
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    77
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    78
            { Object.class,         "java.base" },
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    79
            { Object[].class,       "java.base" },
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    80
            { Object[][].class,     "java.base" },
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    81
            { Component.class,      "java.desktop" },
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    82
            { Component[].class,    "java.desktop" },
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    83
            { Component[][].class,  "java.desktop" },
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
        };
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
    @Test(dataProvider = "testclasses")
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
    public void testGetModule(Class<?> type, String expected) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
        Module m = type.getModule();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
        assertNotNull(m);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    91
        if (expected == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    92
            assertTrue(m == TEST_MODULE);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    93
        } else {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
            assertEquals(m.getName(), expected);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    95
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    96
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    97
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    99
    @DataProvider(name = "hostclasses")
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   100
    public Object[][] hostClasses() {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   101
        return new Object[][] {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   102
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   103
            { GetModuleTest.class,      null },
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   104
            { Object.class,             null },
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   105
            { Component.class,          null },
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   106
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   107
        };
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   108
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   109
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   110
    /**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   111
     * Exercise Class::getModule on VM anonymous classes
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   112
     */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   113
    @Test(dataProvider = "hostclasses")
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   114
    public void testGetModuleOnVMAnonymousClass(Class<?> hostClass, String ignore) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   115
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   116
        // choose a class name in the same package as the host class
40964
c4a7c6d1cfb3 8058575: IllegalAccessError trying to access package-private class from VM anonymous class
hseigel
parents: 40684
diff changeset
   117
        String prefix = hostClass.getPackageName();
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   118
        if (prefix.length() > 0)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   119
            prefix = prefix.replace('.', '/') + "/";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   120
        String className = prefix + "Anon";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   121
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   122
        // create the class
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   123
        String superName = "java/lang/Object";
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   124
        ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   125
                                         + ClassWriter.COMPUTE_FRAMES);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   126
        cw.visit(V1_8, ACC_PUBLIC + ACC_FINAL + ACC_SUPER,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   127
                 className, null, superName, null);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   128
        byte[] classBytes = cw.toByteArray();
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   129
        int cpPoolSize = constantPoolSize(classBytes);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   130
        Class<?> anonClass
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   131
            = U.defineAnonymousClass(hostClass, classBytes, new Object[cpPoolSize]);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   132
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   133
        assertTrue(anonClass.getModule() == hostClass.getModule());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   134
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   135
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   136
    private static int constantPoolSize(byte[] classFile) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   137
        return ((classFile[8] & 0xFF) << 8) | (classFile[9] & 0xFF);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   138
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   139
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   140
}