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.desktop 29 * jdk.unsupported 30 * @run testng GetModuleTest 31 */ 32 33 import java.awt.Component; 34 import java.lang.reflect.Field; 35 import java.lang.reflect.Module; 36 37 import jdk.internal.org.objectweb.asm.ClassWriter; 38 import static jdk.internal.org.objectweb.asm.Opcodes.*; 39 import sun.misc.Unsafe; 40 41 import org.testng.annotations.DataProvider; 42 import org.testng.annotations.Test; 43 import static org.testng.Assert.*; 44 45 public 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 { GetModuleTest[].class, null }, 105 { Object.class, null }, 106 { Object[].class, null }, 107 { Component.class, null }, 108 { Component[].class, null }, 109 110 }; 111 } 112 113 /** 114 * Exercise Class::getModule on VM anonymous classes 115 */ 116 @Test(dataProvider = "hostclasses") 117 public void testGetModuleOnVMAnonymousClass(Class<?> hostClass, String ignore) { 118 119 // choose a class name in the same package as the host class 120 String prefix = packageName(hostClass); 121 if (prefix.length() > 0) 122 prefix = prefix.replace('.', '/') + "/"; 123 String className = prefix + "Anon"; 124 125 // create the class 126 String superName = "java/lang/Object"; 127 ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS 128 + ClassWriter.COMPUTE_FRAMES); 129 cw.visit(V1_8, ACC_PUBLIC + ACC_FINAL + ACC_SUPER, 130 className, null, superName, null); 131 byte[] classBytes = cw.toByteArray(); 132 int cpPoolSize = constantPoolSize(classBytes); 133 Class<?> anonClass 134 = U.defineAnonymousClass(hostClass, classBytes, new Object[cpPoolSize]); 135 136 assertTrue(anonClass.getModule() == hostClass.getModule()); 137 } 138 139 private static String packageName(Class<?> c) { 140 if (c.isArray()) { 141 return packageName(c.getComponentType()); 142 } else { 143 String name = c.getName(); 144 int dot = name.lastIndexOf('.'); 145 if (dot == -1) return ""; 146 return name.substring(0, dot); 147 } 148 } 149 150 private static int constantPoolSize(byte[] classFile) { 151 return ((classFile[8] & 0xFF) << 8) | (classFile[9] & 0xFF); 152 } 153 154 }