1 /*
   2  * Copyright (c) 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 import java.awt.Component;
  25 import java.lang.reflect.Field;
  26 import static jdk.internal.org.objectweb.asm.Opcodes.*;
  27 import jdk.internal.org.objectweb.asm.*;
  28 import sun.misc.Unsafe;
  29 
  30 /*
  31  * @test PrimitiveHostClass
  32  * @bug 8140665
  33  * @summary Throws IllegalArgumentException if host class is a primitive class.
  34  * @library /testlibrary
  35  * @modules java.base/jdk.internal.org.objectweb.asm
  36  *          java.base/jdk.internal.misc
  37  * @compile -XDignore.symbol.file PrimitiveHostClass.java
  38  * @run main/othervm PrimitiveHostClass
  39  */
  40 
  41 public class PrimitiveHostClass {
  42 
  43     static final Unsafe U;
  44     static {
  45         try {
  46             Field theUnsafe = Unsafe.class.getDeclaredField("theUnsafe");
  47             theUnsafe.setAccessible(true);
  48             U = (Unsafe) theUnsafe.get(null);
  49         } catch (Exception e) {
  50             throw new AssertionError(e);
  51         }
  52     }
  53 
  54     public static void testVMAnonymousClass(Class<?> hostClass) {
  55 
  56         // choose a class name in the same package as the host class
  57         String prefix = packageName(hostClass);
  58         if (prefix.length() > 0)
  59             prefix = prefix.replace('.', '/') + "/";
  60         String className = prefix + "Anon";
  61 
  62         // create the class
  63         String superName = "java/lang/Object";
  64         ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS
  65                                          + ClassWriter.COMPUTE_FRAMES);
  66         cw.visit(V1_8, ACC_PUBLIC + ACC_FINAL + ACC_SUPER,
  67                  className, null, superName, null);
  68         byte[] classBytes = cw.toByteArray();
  69         int cpPoolSize = constantPoolSize(classBytes);
  70         Class<?> anonClass =
  71             U.defineAnonymousClass(hostClass, classBytes, new Object[cpPoolSize]);
  72     }
  73 
  74     private static String packageName(Class<?> c) {
  75         if (c.isArray()) {
  76             return packageName(c.getComponentType());
  77         } else {
  78             String name = c.getName();
  79             int dot = name.lastIndexOf('.');
  80             if (dot == -1) return "";
  81             return name.substring(0, dot);
  82         }
  83     }
  84 
  85     private static int constantPoolSize(byte[] classFile) {
  86         return ((classFile[8] & 0xFF) << 8) | (classFile[9] & 0xFF);
  87     }
  88 
  89     public static void main(String args[]) {
  90         testVMAnonymousClass(PrimitiveHostClass.class);
  91         try {
  92             testVMAnonymousClass(int.class);
  93             throw new RuntimeException(
  94                 "Expected IllegalArgumentException not thrown");
  95         } catch (IllegalArgumentException e) {
  96             // Expected
  97         }
  98     }
  99 }