1 /*
   2  * Copyright (c) 2020, 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 /*
  25  * @test
  26  * @summary Test that stack tracing isn't broken if an exception is thrown
  27  *          in a hidden class.
  28  * @library /test/lib
  29  * @modules java.compiler
  30  *          java.management
  31  * @run main HiddenClassStack
  32  */
  33 
  34 import java.io.ByteArrayOutputStream;
  35 import java.io.PrintStream;
  36 import java.lang.invoke.MethodType;
  37 import java.lang.invoke.MethodHandles;
  38 import java.lang.invoke.MethodHandles.Lookup;
  39 import static java.lang.invoke.MethodHandles.Lookup.ClassOption.*;
  40 import jdk.test.lib.compiler.InMemoryJavaCompiler;
  41 
  42 // This test is based on vmTestbase/vm/mlvm/anonloader/func/classNameInStackTrace/Test.java
  43 public class HiddenClassStack {
  44 
  45     static byte klassbuf[] = InMemoryJavaCompiler.compile("TestClass",
  46         "public class TestClass { " +
  47         "    public TestClass() { " +
  48         "        throw new RuntimeException(\"boom\"); " +
  49         " } } ");
  50 
  51     public static void main(String[] args) throws Throwable {
  52 
  53         // An exception is thrown by class loaded by lookup.defineHiddenClass().
  54         // Verify that the exception's stack trace contains name of the current
  55         // test class.
  56         try {
  57             Lookup lookup = MethodHandles.lookup();
  58             Class<?> cl = lookup.defineHiddenClass(klassbuf, false, NESTMATE).lookupClass();
  59             Object obj = cl.newInstance();
  60             throw new Exception("Expected RuntimeException not thrown");
  61         } catch (RuntimeException e) {
  62             if (!e.getMessage().contains("boom")) {
  63                 throw new RuntimeException("Wrong RuntimeException, e: " + e.toString());
  64             }
  65             ByteArrayOutputStream byteOS = new ByteArrayOutputStream();
  66             PrintStream printStream = new PrintStream(byteOS);
  67             e.printStackTrace(printStream);
  68             printStream.close();
  69             String stackTrace = byteOS.toString("ASCII");
  70             if (!stackTrace.contains(HiddenClassStack.class.getName())) {
  71                 throw new RuntimeException("HiddenClassStack missing from stacktrace: " +
  72                                            stackTrace);
  73             }
  74         }
  75     }
  76 }