Path: blob/master/test/jdk/java/lang/StackWalker/CallerFromMain.java
41149 views
/*1* Copyright (c) 2015, 2018, 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* @bug 814045026* @library /test/lib27* @summary Test if the getCallerClass method returns empty optional28* @run main CallerFromMain exec29*/3031import jdk.test.lib.process.ProcessTools;32import jdk.test.lib.process.OutputAnalyzer;3334public class CallerFromMain {3536private static final StackWalker sw = StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE);37public static void main(String[] args) throws Exception {38if (args.length > 0) {39ProcessBuilder pb = ProcessTools.createTestJvm("CallerFromMain");40OutputAnalyzer output = ProcessTools.executeProcess(pb);41System.out.println(output.getOutput());42output.shouldHaveExitValue(0);43return;44}4546// StackWalker::getCallerClass47// CallerFromMain::main48// no caller49try {50Class<?> c = sw.getCallerClass();51throw new RuntimeException("UOE not thrown. Caller: " + c);52} catch (IllegalCallerException e) {}5354// StackWalker::getCallerClass55// Runnable::run56// Thread::run57Thread t1 = new Thread(new Runnable() {58@Override59public void run() {60Class<?> c = sw.getCallerClass();61System.out.println("Call from Thread.run: " + c);62assertThreadClassAsCaller(c);63}64});65t1.setDaemon(true);66t1.start();6768// StackWalker::getCallerClass69// CallerFromMain::doit70// Thread::run71Thread t2 = new Thread(CallerFromMain::doit);72t2.setDaemon(true);73t2.start();7475// StackWalker::getCallerClass76// MyRunnable::run77// Thread::run78Thread t3 = new Thread(new MyRunnable());79t3.setDaemon(true);80t3.start();8182// StackWalker::getCallerClass83// Runnable::run84// MyThread::run85Thread t4 = new MyThread(new Runnable() {86@Override87public void run() {88Class<?> c = sw.getCallerClass();89System.out.println("Call from MyThread.run: " + c);90if (c != MyThread.class) {91throw new RuntimeException("Expected MyThread.class but got " + c);92}93}94});95t4.setDaemon(true);96t4.start();9798t1.join();99t2.join();100t3.join();101t4.join();102}103104static class MyThread extends Thread {105final Runnable runnable;106MyThread(Runnable runnable) {107super("MyThread");108this.runnable = runnable;109}110public void run() {111runnable.run();112}113}114115static class MyRunnable implements Runnable {116@Override117public void run() {118Class<?> c = sw.getCallerClass();119System.out.println("Call from Thread::run: " + c);120assertThreadClassAsCaller(c);121}122}123124static void doit() {125Class<?> c = sw.getCallerClass();126System.out.println("Call from CallerFromMain.doit: " + c);127assertThreadClassAsCaller(c);128}129130static void assertThreadClassAsCaller(Class<?> caller) {131if (caller != Thread.class) {132throw new RuntimeException("Expected Thread.class but got " + caller);133}134}135}136137138