test/runtime/invokedynamic/BootstrapMethodErrorTest.java

Mon, 06 Nov 2017 16:51:47 +0800

author
aoqi
date
Mon, 06 Nov 2017 16:51:47 +0800
changeset 7997
6cbff0651f1a
parent 7865
81bed6c76a89
permissions
-rw-r--r--

[Code Reorganization] remove trailing whitespace to pass jcheck test

aeriksso@7865 1 /*
aeriksso@7865 2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
aeriksso@7865 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aeriksso@7865 4 *
aeriksso@7865 5 * This code is free software; you can redistribute it and/or modify it
aeriksso@7865 6 * under the terms of the GNU General Public License version 2 only, as
aeriksso@7865 7 * published by the Free Software Foundation.
aeriksso@7865 8 *
aeriksso@7865 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aeriksso@7865 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aeriksso@7865 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aeriksso@7865 12 * version 2 for more details (a copy is included in the LICENSE file that
aeriksso@7865 13 * accompanied this code).
aeriksso@7865 14 *
aeriksso@7865 15 * You should have received a copy of the GNU General Public License version
aeriksso@7865 16 * 2 along with this work; if not, write to the Free Software Foundation,
aeriksso@7865 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aeriksso@7865 18 *
aeriksso@7865 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aeriksso@7865 20 * or visit www.oracle.com if you need additional information or have any
aeriksso@7865 21 * questions.
aeriksso@7865 22 */
aeriksso@7865 23
aeriksso@7865 24 /*
aeriksso@7865 25 * @test
aeriksso@7865 26 * @bug 8051045
aeriksso@7865 27 * @summary Test that exceptions from invokedynamic are wrapped in BootstrapMethodError
aeriksso@7865 28 * @modules java.base/jdk.internal.org.objectweb.asm
aeriksso@7865 29 * @run main BootstrapMethodErrorTest
aeriksso@7865 30 */
aeriksso@7865 31
aeriksso@7865 32 import java.lang.reflect.Method;
aeriksso@7865 33 import java.lang.invoke.MethodHandle;
aeriksso@7865 34 import java.lang.invoke.MethodHandles;
aeriksso@7865 35 import static java.lang.invoke.MethodHandles.*;
aeriksso@7865 36 import static java.lang.invoke.MethodType.*;
aeriksso@7865 37
aeriksso@7865 38 import jdk.internal.org.objectweb.asm.ClassWriter;
aeriksso@7865 39 import jdk.internal.org.objectweb.asm.Handle;
aeriksso@7865 40 import jdk.internal.org.objectweb.asm.MethodVisitor;
aeriksso@7865 41 import jdk.internal.org.objectweb.asm.Opcodes;
aeriksso@7865 42
aeriksso@7865 43 public class BootstrapMethodErrorTest extends ClassLoader implements Opcodes {
aeriksso@7865 44
aeriksso@7865 45 @Override
aeriksso@7865 46 public Class findClass(String name) throws ClassNotFoundException {
aeriksso@7865 47 byte[] b;
aeriksso@7865 48 try {
aeriksso@7865 49 b = loadClassData(name);
aeriksso@7865 50 } catch (Throwable th) {
aeriksso@7865 51 throw new ClassNotFoundException("Loading error", th);
aeriksso@7865 52 }
aeriksso@7865 53 return defineClass(name, b, 0, b.length);
aeriksso@7865 54 }
aeriksso@7865 55
aeriksso@7865 56 private byte[] loadClassData(String name) throws Exception {
aeriksso@7865 57 ClassWriter cw = new ClassWriter(0);
aeriksso@7865 58 MethodVisitor mv;
aeriksso@7865 59
aeriksso@7865 60 if (name.equals("C")) {
aeriksso@7865 61 cw.visit(52, ACC_SUPER | ACC_PUBLIC, "C", null, "java/lang/Object", null);
aeriksso@7865 62 {
aeriksso@7865 63 mv = cw.visitMethod(ACC_PRIVATE | ACC_STATIC, "m", "()V", null, null);
aeriksso@7865 64 mv.visitCode();
aeriksso@7865 65 mv.visitInsn(RETURN);
aeriksso@7865 66 mv.visitMaxs(0, 1);
aeriksso@7865 67 mv.visitEnd();
aeriksso@7865 68 }
aeriksso@7865 69 cw.visitEnd();
aeriksso@7865 70 return cw.toByteArray();
aeriksso@7865 71 } else if (name.equals("Exec")) {
aeriksso@7865 72 cw.visit(52, ACC_SUPER | ACC_PUBLIC, "Exec", null, "java/lang/Object", null);
aeriksso@7865 73 {
aeriksso@7865 74 mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "invokeRef", "()V", null, null);
aeriksso@7865 75 mv.visitCode();
aeriksso@7865 76 Handle h = new Handle(H_INVOKESTATIC, "C", "m", "()V");
aeriksso@7865 77 mv.visitInvokeDynamicInsn("C", "()V", h);
aeriksso@7865 78 mv.visitInsn(RETURN);
aeriksso@7865 79 mv.visitMaxs(0, 0);
aeriksso@7865 80 mv.visitEnd();
aeriksso@7865 81 }
aeriksso@7865 82 cw.visitEnd();
aeriksso@7865 83 return cw.toByteArray();
aeriksso@7865 84 }
aeriksso@7865 85 return null;
aeriksso@7865 86 }
aeriksso@7865 87
aeriksso@7865 88 public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, NoSuchMethodException {
aeriksso@7865 89 new BootstrapMethodErrorTest().test();
aeriksso@7865 90 }
aeriksso@7865 91
aeriksso@7865 92 public void test() throws ClassNotFoundException, IllegalAccessException, NoSuchMethodException {
aeriksso@7865 93 Class.forName("C", true, this);
aeriksso@7865 94 Class<?> exec = Class.forName("Exec", true, this);
aeriksso@7865 95
aeriksso@7865 96 try {
aeriksso@7865 97 exec.getMethod("invokeRef").invoke(null);
aeriksso@7865 98 } catch (Throwable e) {
aeriksso@7865 99 Throwable c = e.getCause();
aeriksso@7865 100 if (c == null) {
aeriksso@7865 101 throw new RuntimeException(
aeriksso@7865 102 "Expected BootstrapMethodError wrapped in an InvocationTargetException but it wasn't wrapped", e);
aeriksso@7865 103 } else if (c instanceof BootstrapMethodError) {
aeriksso@7865 104 // Only way to pass test, all else should throw
aeriksso@7865 105 return;
aeriksso@7865 106 } else {
aeriksso@7865 107 throw new RuntimeException(
aeriksso@7865 108 "Expected BootstrapMethodError but got another Error: "
aeriksso@7865 109 + c.getClass().getName(),
aeriksso@7865 110 c);
aeriksso@7865 111 }
aeriksso@7865 112 }
aeriksso@7865 113 throw new RuntimeException("Expected BootstrapMethodError but no Error at all was thrown");
aeriksso@7865 114 }
aeriksso@7865 115 }

mercurial