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 33 import java.util.Optional; 34 import jdk.testlibrary.ProcessTools; 35 import jdk.testlibrary.OutputAnalyzer; 36 37 public class CallerFromMain { 38 39 public static void getCaller(Class<?> caller) { 40 // StackWalker::getCallerClass 41 // CallerFromMain::doIt 42 // caller 43 Optional<Class<?>> c = sw.getCallerClass(); 44 if (caller != null && c.get() != caller) { 45 throw new RuntimeException("Expected " + caller + " but got " + c); 46 } 47 if (caller == null && c.isPresent()) { 48 throw new RuntimeException("Expected empty optional: " + c); 49 } 50 } 51 52 private static final StackWalker sw = StackWalker.create(StackWalker.Option.RETAIN_CLASS_REFERENCE); 53 public static void main(String[] args) throws Exception { 54 if (args.length > 0) { 55 ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(true, "CallerFromMain"); 56 OutputAnalyzer output = ProcessTools.executeProcess(pb); 57 System.out.println(output.getOutput()); 58 output.shouldHaveExitValue(0); 59 return; 60 } 61 62 // StackWalker::getCallerClass 63 // CallerFromMain::main 64 // no caller 65 Optional<Class<?>> c = sw.getCallerClass(); 66 System.out.println("Call from main: " + c); 67 if (c.isPresent()) { 68 throw new RuntimeException("Expected empty optional: " + c); 69 } 70 71 // call from Thread::run 72 Thread t1 = new Thread(new Runnable() { 73 @Override 74 public void run() { 75 Optional<Class<?>> c = sw.getCallerClass(); 76 System.out.println("Call from Thread.run: " + c); 77 78 if (c.get() != Thread.class) { 79 throw new RuntimeException("Expected Thread.class but got " + c); 80 } 81 } 82 }); 83 84 t1.setDaemon(true); 85 t1.start(); 86 87 // call from MyThread::run 88 Thread t2 = new MyThread(new Runnable() { 89 @Override 90 public void run() { 91 Optional<Class<?>> c = sw.getCallerClass(); 92 System.out.println("Call from MyThread.run: " + c); 93 94 if (c.get() != MyThread.class) { 95 throw new RuntimeException("Expected MyThread.class but got " + c); 96 } 97 } 98 }); 99 t2.setDaemon(true); 100 t2.start(); 101 102 t1.join(); 103 t2.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