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