1/*
2 * Copyright (c) 2015, 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 * @bug 8140450
27 * @library /lib/testlibrary
28 * @build jdk.testlibrary.*
29 * @summary Test if the getCallerClass method returns empty optional
30 * @run main CallerFromMain exec
31 */
32
33import jdk.testlibrary.ProcessTools;
34import jdk.testlibrary.OutputAnalyzer;
35
36public class CallerFromMain {
37
38    private static final StackWalker sw = StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE);
39    public static void main(String[] args) throws Exception {
40        if (args.length > 0) {
41            ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(true, "CallerFromMain");
42            OutputAnalyzer output = ProcessTools.executeProcess(pb);
43            System.out.println(output.getOutput());
44            output.shouldHaveExitValue(0);
45            return;
46        }
47
48        // StackWalker::getCallerClass
49        // CallerFromMain::main
50        // no caller
51        try {
52            Class<?> c = sw.getCallerClass();
53            throw new RuntimeException("UOE not thrown. Caller: " + c);
54        } catch (IllegalCallerException e) {}
55
56        // StackWalker::getCallerClass
57        // Runnable::run
58        // Thread::run
59        Thread t1 = new Thread(new Runnable() {
60            @Override
61            public void run() {
62                Class<?> c = sw.getCallerClass();
63                System.out.println("Call from Thread.run: " + c);
64                assertThreadClassAsCaller(c);
65            }
66        });
67        t1.setDaemon(true);
68        t1.start();
69
70        // StackWalker::getCallerClass
71        // CallerFromMain::doit
72        // Thread::run
73        Thread t2 = new Thread(CallerFromMain::doit);
74        t2.setDaemon(true);
75        t2.start();
76
77        // StackWalker::getCallerClass
78        // MyRunnable::run
79        // Thread::run
80        Thread t3 = new Thread(new MyRunnable());
81        t3.setDaemon(true);
82        t3.start();
83
84        // StackWalker::getCallerClass
85        // Runnable::run
86        // MyThread::run
87        Thread t4 = new MyThread(new Runnable() {
88            @Override
89            public void run() {
90                Class<?> c = sw.getCallerClass();
91                System.out.println("Call from MyThread.run: " + c);
92                if (c != MyThread.class) {
93                    throw new RuntimeException("Expected MyThread.class but got " + c);
94                }
95            }
96        });
97        t4.setDaemon(true);
98        t4.start();
99
100        t1.join();
101        t2.join();
102        t3.join();
103        t4.join();
104    }
105
106    static class MyThread extends Thread {
107        final Runnable runnable;
108        MyThread(Runnable runnable) {
109            super("MyThread");
110            this.runnable = runnable;
111        }
112        public void run() {
113            runnable.run();
114        }
115    }
116
117    static class MyRunnable implements Runnable {
118        @Override
119        public void run() {
120            Class<?> c = sw.getCallerClass();
121            System.out.println("Call from Thread::run: " + c);
122            assertThreadClassAsCaller(c);
123        }
124     }
125
126    static void doit() {
127        Class<?> c = sw.getCallerClass();
128        System.out.println("Call from CallerFromMain.doit: " + c);
129        assertThreadClassAsCaller(c);
130    }
131
132    static void assertThreadClassAsCaller(Class<?> caller) {
133        if (caller != Thread.class) {
134            throw new RuntimeException("Expected Thread.class but got " + caller);
135        }
136    }
137}
138