1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/compiler/jsr292/methodHandleExceptions/TestAMEnotNPE.java Fri Sep 27 13:36:25 2013 -0400 1.3 @@ -0,0 +1,143 @@ 1.4 +/* 1.5 + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +import java.lang.reflect.InvocationTargetException; 1.29 +import jdk.internal.org.objectweb.asm.ClassWriter; 1.30 +import jdk.internal.org.objectweb.asm.Handle; 1.31 +import jdk.internal.org.objectweb.asm.MethodVisitor; 1.32 +import jdk.internal.org.objectweb.asm.Opcodes; 1.33 + 1.34 +/** 1.35 + * @test 1.36 + * @bug 8025260 1.37 + * @summary Ensure that AbstractMethodError is thrown, not NullPointerException, through MethodHandles::jump_from_method_handle code path 1.38 + * 1.39 + * @compile -XDignore.symbol.file ByteClassLoader.java I.java C.java TestAMEnotNPE.java 1.40 + * @run main/othervm TestAMEnotNPE 1.41 + */ 1.42 + 1.43 +public class TestAMEnotNPE implements Opcodes { 1.44 + 1.45 + /** 1.46 + * The bytes for D, a NOT abstract class extending abstract class C 1.47 + * without supplying an implementation for abstract method m. 1.48 + * There is a default method in the interface I, but it should lose to 1.49 + * the abstract class. 1.50 + 1.51 + class D extends C { 1.52 + D() { super(); } 1.53 + // does not define m 1.54 + } 1.55 + 1.56 + * @return 1.57 + * @throws Exception 1.58 + */ 1.59 + public static byte[] bytesForD() throws Exception { 1.60 + 1.61 + ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES|ClassWriter.COMPUTE_MAXS); 1.62 + MethodVisitor mv; 1.63 + 1.64 + cw.visit(V1_8, ACC_PUBLIC + ACC_SUPER, "D", null, "C", null); 1.65 + 1.66 + { 1.67 + mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null); 1.68 + mv.visitCode(); 1.69 + mv.visitVarInsn(ALOAD, 0); 1.70 + mv.visitMethodInsn(INVOKESPECIAL, "C", "<init>", "()V"); 1.71 + mv.visitInsn(RETURN); 1.72 + mv.visitMaxs(0, 0); 1.73 + mv.visitEnd(); 1.74 + } 1.75 + cw.visitEnd(); 1.76 + 1.77 + return cw.toByteArray(); 1.78 + } 1.79 + 1.80 + 1.81 + /** 1.82 + * The bytecodes for an invokeExact of a particular methodHandle, I.m, invoked on a D 1.83 + 1.84 + class T { 1.85 + T() { super(); } // boring constructor 1.86 + int test() { 1.87 + MethodHandle mh = `I.m():int`; 1.88 + D d = new D(); 1.89 + return mh.invokeExact(d); // Should explode here, AbstractMethodError 1.90 + } 1.91 + } 1.92 + 1.93 + * @return 1.94 + * @throws Exception 1.95 + */ 1.96 + public static byte[] bytesForT() throws Exception { 1.97 + 1.98 + ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES|ClassWriter.COMPUTE_MAXS); 1.99 + MethodVisitor mv; 1.100 + 1.101 + cw.visit(V1_8, ACC_PUBLIC + ACC_SUPER, "T", null, "java/lang/Object", null); 1.102 + { 1.103 + mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null); 1.104 + mv.visitCode(); 1.105 + mv.visitVarInsn(ALOAD, 0); 1.106 + mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V"); 1.107 + mv.visitInsn(RETURN); 1.108 + mv.visitMaxs(0,0); 1.109 + mv.visitEnd(); 1.110 + } 1.111 + { 1.112 + mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "test", "()I", null, null); 1.113 + mv.visitCode(); 1.114 + mv.visitLdcInsn(new Handle(Opcodes.H_INVOKEINTERFACE, "I", "m", "()I")); 1.115 + mv.visitTypeInsn(NEW, "D"); 1.116 + mv.visitInsn(DUP); 1.117 + mv.visitMethodInsn(INVOKESPECIAL, "D", "<init>", "()V"); 1.118 + mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/invoke/MethodHandle", "invokeExact", "(LI;)I"); 1.119 + mv.visitInsn(IRETURN); 1.120 + mv.visitMaxs(0,0); 1.121 + mv.visitEnd(); 1.122 + } 1.123 + cw.visitEnd(); 1.124 + return cw.toByteArray(); 1.125 + } 1.126 + 1.127 + public static void main(String args[] ) throws Throwable { 1.128 + ByteClassLoader bcl = new ByteClassLoader(); 1.129 + Class<?> d = bcl.loadBytes("D", bytesForD()); 1.130 + Class<?> t = bcl.loadBytes("T", bytesForT()); 1.131 + try { 1.132 + Object result = t.getMethod("test").invoke(null); 1.133 + System.out.println("Expected AbstractMethodError wrapped in InvocationTargetException, saw no exception"); 1.134 + throw new Error("Missing expected exception"); 1.135 + } catch (InvocationTargetException e) { 1.136 + Throwable th = e.getCause(); 1.137 + if (th instanceof AbstractMethodError) { 1.138 + th.printStackTrace(System.out); 1.139 + System.out.println("PASS, saw expected exception (AbstractMethodError, wrapped in InvocationTargetException)."); 1.140 + } else { 1.141 + System.out.println("Expected AbstractMethodError wrapped in InvocationTargetException, saw " + th); 1.142 + throw th; 1.143 + } 1.144 + } 1.145 + } 1.146 +}