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