< prev index next >

test/java/lang/StackWalker/CallerFromMain.java

Print this page




  34 import jdk.testlibrary.OutputAnalyzer;
  35 
  36 public 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 (IllegalStateException 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);




  34 import jdk.testlibrary.OutputAnalyzer;
  35 
  36 public 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);


< prev index next >