2
* Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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.
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
26
* @summary Test that stack tracing isn't broken if an exception is thrown
29
* An exception is thrown by a hidden class. Verify that the exception's
30
* stack trace contains the name of the current test class (i.e., verify
31
* that the stack trace is not broken).
33
* @modules jdk.compiler
34
* @run main HiddenClassStack
37
import java.io.ByteArrayOutputStream;
38
import java.io.PrintStream;
39
import java.lang.invoke.MethodType;
40
import java.lang.invoke.MethodHandles;
41
import java.lang.invoke.MethodHandles.Lookup;
42
import static java.lang.invoke.MethodHandles.Lookup.ClassOption.*;
43
import jdk.test.lib.compiler.InMemoryJavaCompiler;
45
public class HiddenClassStack {
47
static byte klassbuf[] = InMemoryJavaCompiler.compile("TestClass",
48
"public class TestClass { " +
49
" public TestClass() { " +
50
" throw new RuntimeException(\"boom\"); " +
53
public static void main(String[] args) throws Throwable {
55
// An exception is thrown by class loaded by lookup.defineHiddenClass().
56
// Verify that the exception's stack trace contains name of the current
59
Lookup lookup = MethodHandles.lookup();
60
Class<?> cl = lookup.defineHiddenClass(klassbuf, false, NESTMATE).lookupClass();
61
Object obj = cl.newInstance();
62
throw new Exception("Expected RuntimeException not thrown");
63
} catch (RuntimeException e) {
64
if (!e.getMessage().contains("boom")) {
65
throw new RuntimeException("Wrong RuntimeException, e: " + e.toString());
67
ByteArrayOutputStream byteOS = new ByteArrayOutputStream();
68
PrintStream printStream = new PrintStream(byteOS);
69
e.printStackTrace(printStream);
71
String stackTrace = byteOS.toString("ASCII");
72
if (!stackTrace.contains(HiddenClassStack.class.getName())) {
73
throw new RuntimeException("HiddenClassStack missing from stacktrace: " +