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