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