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