Path: blob/master/test/hotspot/jtreg/runtime/HiddenClasses/HiddenClassStack.java
40942 views
/*1* Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @summary Test that stack tracing isn't broken if an exception is thrown26* in a hidden class.27* DESCRIPTION28* An exception is thrown by a hidden class. Verify that the exception's29* stack trace contains the name of the current test class (i.e., verify30* that the stack trace is not broken).31* @library /test/lib32* @modules jdk.compiler33* @run main HiddenClassStack34*/3536import java.io.ByteArrayOutputStream;37import java.io.PrintStream;38import java.lang.invoke.MethodType;39import java.lang.invoke.MethodHandles;40import java.lang.invoke.MethodHandles.Lookup;41import static java.lang.invoke.MethodHandles.Lookup.ClassOption.*;42import jdk.test.lib.compiler.InMemoryJavaCompiler;4344public class HiddenClassStack {4546static byte klassbuf[] = InMemoryJavaCompiler.compile("TestClass",47"public class TestClass { " +48" public TestClass() { " +49" throw new RuntimeException(\"boom\"); " +50" } } ");5152public static void main(String[] args) throws Throwable {5354// An exception is thrown by class loaded by lookup.defineHiddenClass().55// Verify that the exception's stack trace contains name of the current56// test class.57try {58Lookup lookup = MethodHandles.lookup();59Class<?> cl = lookup.defineHiddenClass(klassbuf, false, NESTMATE).lookupClass();60Object obj = cl.newInstance();61throw new Exception("Expected RuntimeException not thrown");62} catch (RuntimeException e) {63if (!e.getMessage().contains("boom")) {64throw new RuntimeException("Wrong RuntimeException, e: " + e.toString());65}66ByteArrayOutputStream byteOS = new ByteArrayOutputStream();67PrintStream printStream = new PrintStream(byteOS);68e.printStackTrace(printStream);69printStream.close();70String stackTrace = byteOS.toString("ASCII");71if (!stackTrace.contains(HiddenClassStack.class.getName())) {72throw new RuntimeException("HiddenClassStack missing from stacktrace: " +73stackTrace);74}75}76}77}787980