test/compiler/jsr292/methodHandleExceptions/TestAMEnotNPE.java

Fri, 27 Sep 2013 13:36:25 -0400

author
drchase
date
Fri, 27 Sep 2013 13:36:25 -0400
changeset 5800
dc261f466b6d
child 6134
9d15b81d5d1b
permissions
-rw-r--r--

8025260: Methodhandles/JSR292: NullPointerException (NPE) thrown instead of AbstractMethodError (AME)
Summary: Copied null-checks from templateInterpreter_CPU into methodHandles_CPU
Reviewed-by: jrose, twisti

drchase@5800 1 /*
drchase@5800 2 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
drchase@5800 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
drchase@5800 4 *
drchase@5800 5 * This code is free software; you can redistribute it and/or modify it
drchase@5800 6 * under the terms of the GNU General Public License version 2 only, as
drchase@5800 7 * published by the Free Software Foundation.
drchase@5800 8 *
drchase@5800 9 * This code is distributed in the hope that it will be useful, but WITHOUT
drchase@5800 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
drchase@5800 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
drchase@5800 12 * version 2 for more details (a copy is included in the LICENSE file that
drchase@5800 13 * accompanied this code).
drchase@5800 14 *
drchase@5800 15 * You should have received a copy of the GNU General Public License version
drchase@5800 16 * 2 along with this work; if not, write to the Free Software Foundation,
drchase@5800 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
drchase@5800 18 *
drchase@5800 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
drchase@5800 20 * or visit www.oracle.com if you need additional information or have any
drchase@5800 21 * questions.
drchase@5800 22 *
drchase@5800 23 */
drchase@5800 24
drchase@5800 25 import java.lang.reflect.InvocationTargetException;
drchase@5800 26 import jdk.internal.org.objectweb.asm.ClassWriter;
drchase@5800 27 import jdk.internal.org.objectweb.asm.Handle;
drchase@5800 28 import jdk.internal.org.objectweb.asm.MethodVisitor;
drchase@5800 29 import jdk.internal.org.objectweb.asm.Opcodes;
drchase@5800 30
drchase@5800 31 /**
drchase@5800 32 * @test
drchase@5800 33 * @bug 8025260
drchase@5800 34 * @summary Ensure that AbstractMethodError is thrown, not NullPointerException, through MethodHandles::jump_from_method_handle code path
drchase@5800 35 *
drchase@5800 36 * @compile -XDignore.symbol.file ByteClassLoader.java I.java C.java TestAMEnotNPE.java
drchase@5800 37 * @run main/othervm TestAMEnotNPE
drchase@5800 38 */
drchase@5800 39
drchase@5800 40 public class TestAMEnotNPE implements Opcodes {
drchase@5800 41
drchase@5800 42 /**
drchase@5800 43 * The bytes for D, a NOT abstract class extending abstract class C
drchase@5800 44 * without supplying an implementation for abstract method m.
drchase@5800 45 * There is a default method in the interface I, but it should lose to
drchase@5800 46 * the abstract class.
drchase@5800 47
drchase@5800 48 class D extends C {
drchase@5800 49 D() { super(); }
drchase@5800 50 // does not define m
drchase@5800 51 }
drchase@5800 52
drchase@5800 53 * @return
drchase@5800 54 * @throws Exception
drchase@5800 55 */
drchase@5800 56 public static byte[] bytesForD() throws Exception {
drchase@5800 57
drchase@5800 58 ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES|ClassWriter.COMPUTE_MAXS);
drchase@5800 59 MethodVisitor mv;
drchase@5800 60
drchase@5800 61 cw.visit(V1_8, ACC_PUBLIC + ACC_SUPER, "D", null, "C", null);
drchase@5800 62
drchase@5800 63 {
drchase@5800 64 mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
drchase@5800 65 mv.visitCode();
drchase@5800 66 mv.visitVarInsn(ALOAD, 0);
drchase@5800 67 mv.visitMethodInsn(INVOKESPECIAL, "C", "<init>", "()V");
drchase@5800 68 mv.visitInsn(RETURN);
drchase@5800 69 mv.visitMaxs(0, 0);
drchase@5800 70 mv.visitEnd();
drchase@5800 71 }
drchase@5800 72 cw.visitEnd();
drchase@5800 73
drchase@5800 74 return cw.toByteArray();
drchase@5800 75 }
drchase@5800 76
drchase@5800 77
drchase@5800 78 /**
drchase@5800 79 * The bytecodes for an invokeExact of a particular methodHandle, I.m, invoked on a D
drchase@5800 80
drchase@5800 81 class T {
drchase@5800 82 T() { super(); } // boring constructor
drchase@5800 83 int test() {
drchase@5800 84 MethodHandle mh = `I.m():int`;
drchase@5800 85 D d = new D();
drchase@5800 86 return mh.invokeExact(d); // Should explode here, AbstractMethodError
drchase@5800 87 }
drchase@5800 88 }
drchase@5800 89
drchase@5800 90 * @return
drchase@5800 91 * @throws Exception
drchase@5800 92 */
drchase@5800 93 public static byte[] bytesForT() throws Exception {
drchase@5800 94
drchase@5800 95 ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_FRAMES|ClassWriter.COMPUTE_MAXS);
drchase@5800 96 MethodVisitor mv;
drchase@5800 97
drchase@5800 98 cw.visit(V1_8, ACC_PUBLIC + ACC_SUPER, "T", null, "java/lang/Object", null);
drchase@5800 99 {
drchase@5800 100 mv = cw.visitMethod(ACC_PUBLIC, "<init>", "()V", null, null);
drchase@5800 101 mv.visitCode();
drchase@5800 102 mv.visitVarInsn(ALOAD, 0);
drchase@5800 103 mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V");
drchase@5800 104 mv.visitInsn(RETURN);
drchase@5800 105 mv.visitMaxs(0,0);
drchase@5800 106 mv.visitEnd();
drchase@5800 107 }
drchase@5800 108 {
drchase@5800 109 mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC, "test", "()I", null, null);
drchase@5800 110 mv.visitCode();
drchase@5800 111 mv.visitLdcInsn(new Handle(Opcodes.H_INVOKEINTERFACE, "I", "m", "()I"));
drchase@5800 112 mv.visitTypeInsn(NEW, "D");
drchase@5800 113 mv.visitInsn(DUP);
drchase@5800 114 mv.visitMethodInsn(INVOKESPECIAL, "D", "<init>", "()V");
drchase@5800 115 mv.visitMethodInsn(INVOKEVIRTUAL, "java/lang/invoke/MethodHandle", "invokeExact", "(LI;)I");
drchase@5800 116 mv.visitInsn(IRETURN);
drchase@5800 117 mv.visitMaxs(0,0);
drchase@5800 118 mv.visitEnd();
drchase@5800 119 }
drchase@5800 120 cw.visitEnd();
drchase@5800 121 return cw.toByteArray();
drchase@5800 122 }
drchase@5800 123
drchase@5800 124 public static void main(String args[] ) throws Throwable {
drchase@5800 125 ByteClassLoader bcl = new ByteClassLoader();
drchase@5800 126 Class<?> d = bcl.loadBytes("D", bytesForD());
drchase@5800 127 Class<?> t = bcl.loadBytes("T", bytesForT());
drchase@5800 128 try {
drchase@5800 129 Object result = t.getMethod("test").invoke(null);
drchase@5800 130 System.out.println("Expected AbstractMethodError wrapped in InvocationTargetException, saw no exception");
drchase@5800 131 throw new Error("Missing expected exception");
drchase@5800 132 } catch (InvocationTargetException e) {
drchase@5800 133 Throwable th = e.getCause();
drchase@5800 134 if (th instanceof AbstractMethodError) {
drchase@5800 135 th.printStackTrace(System.out);
drchase@5800 136 System.out.println("PASS, saw expected exception (AbstractMethodError, wrapped in InvocationTargetException).");
drchase@5800 137 } else {
drchase@5800 138 System.out.println("Expected AbstractMethodError wrapped in InvocationTargetException, saw " + th);
drchase@5800 139 throw th;
drchase@5800 140 }
drchase@5800 141 }
drchase@5800 142 }
drchase@5800 143 }

mercurial