1 /**
   2  * Copyright (c) 2014, 2015, 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  * @run testng GetModuleTest
  29  */
  30 
  31 import java.awt.Component;
  32 import java.lang.reflect.Field;
  33 import java.lang.reflect.Module;
  34 
  35 import jdk.internal.org.objectweb.asm.ClassWriter;
  36 import static jdk.internal.org.objectweb.asm.Opcodes.*;
  37 import sun.misc.Unsafe;
  38 
  39 import org.testng.annotations.DataProvider;
  40 import org.testng.annotations.Test;
  41 import static org.testng.Assert.*;
  42 
  43 public class GetModuleTest {
  44 
  45     static final Unsafe U;
  46     static {
  47         try {
  48             Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
  49             theUnsafe.setAccessible(true);
  50             U = (Unsafe) theUnsafe.get(null);
  51         } catch (Exception e) {
  52             throw new AssertionError(e);
  53         }
  54     }
  55 
  56     private static final Module TEST_MODULE = GetModuleTest.class.getModule();
  57 
  58 
  59     @DataProvider(name = "testclasses")
  60     public Object[][] testClasses() {
  61         return new Object[][] {
  62 
  63             // unnamed module
  64 
  65             { GetModuleTest.class,      null },
  66             { GetModuleTest[].class,    null },
  67             { GetModuleTest[][].class,  null },
  68 
  69             // should return named module
  70 
  71             { int.class,            "java.base" },
  72             { int[].class,          "java.base" },
  73             { int[][].class,        "java.base" },
  74             { void.class,           "java.base" },
  75 
  76             { Object.class,         "java.base" },
  77             { Object[].class,       "java.base" },
  78             { Object[][].class,     "java.base" },
  79             { Component.class,      "java.desktop" },
  80             { Component[].class,    "java.desktop" },
  81             { Component[][].class,  "java.desktop" },
  82         };
  83     }
  84 
  85     @Test(dataProvider = "testClasses")
  86     public void testGetModule(Class<?> type, String expected) {
  87         Module m = type.getModule();
  88         assertNotNull(m);
  89         if (expected == null) {
  90             assertTrue(m == TEST_MODULE);
  91         } else {
  92             assertEquals(m.getName(), expected);
  93         }
  94     }
  95 
  96 
  97     @DataProvider(name = "hostclasses")
  98     public Object[][] hostClasses() {
  99         return new Object[][] {
 100 
 101             { GetModuleTest.class,      null },
 102             { GetModuleTest[].class,    null },
 103             { Object.class,             null },
 104             { Object[].class,           null },
 105             { Component.class,          null },
 106             { Component[].class,        null },
 107 
 108         };
 109     }
 110 
 111     /**
 112      * Exercise Class::getModule on VM anonymous classes
 113      */
 114     @Test(dataProvider = "hostclasses")
 115     public void testGetModuleOnVMAnonymousClass(Class<?> hostClass, String ignore) {
 116 
 117         // choose a class name in the same package as the host class
 118         String prefix = packageName(hostClass);
 119         if (prefix.length() > 0)
 120             prefix = prefix.replace('.', '/') + "/";
 121         String className = prefix + "Anon";
 122 
 123         // create the class
 124         String superName = "java/lang/Object";
 125         ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS
 126                                          + ClassWriter.COMPUTE_FRAMES);
 127         cw.visit(V1_8, ACC_PUBLIC + ACC_FINAL + ACC_SUPER,
 128                  className, null, superName, null);
 129         byte[] classBytes = cw.toByteArray();
 130         int cpPoolSize = constantPoolSize(classBytes);
 131         Class<?> anonClass
 132             = U.defineAnonymousClass(hostClass, classBytes, new Object[cpPoolSize]);
 133 
 134         assertTrue(anonClass.getModule() == hostClass.getModule());
 135     }
 136 
 137     private static String packageName(Class<?> c) {
 138         if (c.isArray()) {
 139             return packageName(c.getComponentType());
 140         } else {
 141             String name = c.getName();
 142             int dot = name.lastIndexOf('.');
 143             if (dot == -1) return "";
 144             return name.substring(0, dot);
 145         }
 146     }
 147 
 148     private static int constantPoolSize(byte[] classFile) {
 149         return ((classFile[8] & 0xFF) << 8) | (classFile[9] & 0xFF);
 150     }
 151 
 152 }