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

     1 /*
     2  * Copyright (c) 2015, 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  */
    24 /*
    25  * @test
    26  * @bug 8051045
    27  * @summary Test that exceptions from invokedynamic are wrapped in BootstrapMethodError
    28  * @modules java.base/jdk.internal.org.objectweb.asm
    29  * @run main BootstrapMethodErrorTest
    30  */
    32 import java.lang.reflect.Method;
    33 import java.lang.invoke.MethodHandle;
    34 import java.lang.invoke.MethodHandles;
    35 import static java.lang.invoke.MethodHandles.*;
    36 import static java.lang.invoke.MethodType.*;
    38 import jdk.internal.org.objectweb.asm.ClassWriter;
    39 import jdk.internal.org.objectweb.asm.Handle;
    40 import jdk.internal.org.objectweb.asm.MethodVisitor;
    41 import jdk.internal.org.objectweb.asm.Opcodes;
    43 public class BootstrapMethodErrorTest extends ClassLoader implements Opcodes {
    45   @Override
    46   public Class findClass(String name) throws ClassNotFoundException {
    47     byte[] b;
    48     try {
    49       b = loadClassData(name);
    50     } catch (Throwable th) {
    51       throw new ClassNotFoundException("Loading error", th);
    52     }
    53     return defineClass(name, b, 0, b.length);
    54   }
    56   private byte[] loadClassData(String name) throws Exception {
    57     ClassWriter cw = new ClassWriter(0);
    58     MethodVisitor mv;
    60     if (name.equals("C")) {
    61       cw.visit(52, ACC_SUPER | ACC_PUBLIC, "C", null, "java/lang/Object", null);
    62       {
    63         mv = cw.visitMethod(ACC_PRIVATE | ACC_STATIC, "m", "()V", null, null);
    64         mv.visitCode();
    65         mv.visitInsn(RETURN);
    66         mv.visitMaxs(0, 1);
    67         mv.visitEnd();
    68       }
    69       cw.visitEnd();
    70       return cw.toByteArray();
    71     } else if (name.equals("Exec")) {
    72       cw.visit(52, ACC_SUPER | ACC_PUBLIC, "Exec", null, "java/lang/Object", null);
    73       {
    74         mv = cw.visitMethod(ACC_PUBLIC | ACC_STATIC, "invokeRef", "()V", null, null);
    75         mv.visitCode();
    76         Handle h = new Handle(H_INVOKESTATIC, "C", "m", "()V");
    77         mv.visitInvokeDynamicInsn("C", "()V", h);
    78         mv.visitInsn(RETURN);
    79         mv.visitMaxs(0, 0);
    80         mv.visitEnd();
    81       }
    82       cw.visitEnd();
    83       return cw.toByteArray();
    84     }
    85     return null;
    86   }
    88   public static void main(String[] args) throws ClassNotFoundException, IllegalAccessException, NoSuchMethodException {
    89     new BootstrapMethodErrorTest().test();
    90   }
    92   public void test() throws ClassNotFoundException, IllegalAccessException, NoSuchMethodException {
    93     Class.forName("C", true, this);
    94     Class<?> exec = Class.forName("Exec", true, this);
    96     try {
    97       exec.getMethod("invokeRef").invoke(null);
    98     } catch (Throwable e) {
    99       Throwable c = e.getCause();
   100       if (c == null) {
   101         throw new RuntimeException(
   102             "Expected BootstrapMethodError wrapped in an InvocationTargetException but it wasn't wrapped", e);
   103       } else if (c instanceof BootstrapMethodError) {
   104         // Only way to pass test, all else should throw
   105         return;
   106       } else {
   107         throw new RuntimeException(
   108             "Expected BootstrapMethodError but got another Error: "
   109             + c.getClass().getName(),
   110             c);
   111       }
   112     }
   113     throw new RuntimeException("Expected BootstrapMethodError but no Error at all was thrown");
   114   }
   115 }

mercurial