jdk

Форк
0
/
HiddenClassStack.java 
78 строк · 3.2 Кб
1
/*
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.
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
 * DESCRIPTION
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).
32
 * @library /test/lib
33
 * @modules jdk.compiler
34
 * @run main HiddenClassStack
35
 */
36

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;
44

45
public class HiddenClassStack {
46

47
    static byte klassbuf[] = InMemoryJavaCompiler.compile("TestClass",
48
        "public class TestClass { " +
49
        "    public TestClass() { " +
50
        "        throw new RuntimeException(\"boom\"); " +
51
        " } } ");
52

53
    public static void main(String[] args) throws Throwable {
54

55
        // An exception is thrown by class loaded by lookup.defineHiddenClass().
56
        // Verify that the exception's stack trace contains name of the current
57
        // test class.
58
        try {
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());
66
            }
67
            ByteArrayOutputStream byteOS = new ByteArrayOutputStream();
68
            PrintStream printStream = new PrintStream(byteOS);
69
            e.printStackTrace(printStream);
70
            printStream.close();
71
            String stackTrace = byteOS.toString("ASCII");
72
            if (!stackTrace.contains(HiddenClassStack.class.getName())) {
73
                throw new RuntimeException("HiddenClassStack missing from stacktrace: " +
74
                                           stackTrace);
75
            }
76
        }
77
    }
78
}
79

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.