< prev index next >

test/jdk/com/sun/jdi/TestScaffold.java

Print this page
*** 1,7 ***
  /*
!  * Copyright (c) 2001, 2018, Oracle and/or its affiliates. All rights reserved.
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   *
   * This code is free software; you can redistribute it and/or modify it
   * under the terms of the GNU General Public License version 2 only, as
   * published by the Free Software Foundation.
--- 1,7 ---
  /*
!  * Copyright (c) 2001, 2021, Oracle and/or its affiliates. All rights reserved.
   * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   *
   * This code is free software; you can redistribute it and/or modify it
   * under the terms of the GNU General Public License version 2 only, as
   * published by the Free Software Foundation.

*** 22,10 ***
--- 22,11 ---
   */
  
  import com.sun.jdi.*;
  import com.sun.jdi.request.*;
  import com.sun.jdi.event.*;
+ import java.lang.reflect.InvocationTargetException;
  import java.util.*;
  import java.io.*;
  
  /**
   * Framework used by all JDI regression tests

*** 454,10 ***
--- 455,14 ---
          testFailed = true;
      }
  
      private ArgInfo parseArgs(String args[]) {
          ArgInfo argInfo = new ArgInfo();
+         if (System.getProperty("main.wrapper") != null) {
+             argInfo.targetAppCommandLine  = TestScaffold.class.getName() + " " + System.getProperty("main.wrapper") + " ";
+         }
+ 
          for (int i = 0; i < args.length; i++) {
              if (args[i].equals("-connect")) {
                  i++;
                  argInfo.connectorSpec = args[i];
              } else if (args[i].equals("-trace")) {

*** 938,6 ***
--- 943,77 ---
          }
  
          vmDied = true;
          vmDisconnected = true;
      }
+ 
+     public static void main(String[] args) throws Throwable {
+         String wrapper = args[0];
+         String className = args[1];
+         String[] classArgs = new String[args.length - 2];
+         System.arraycopy(args, 2, classArgs, 0, args.length - 2);
+         Class c = Class.forName(className);
+         java.lang.reflect.Method mainMethod = c.getMethod("main", new Class[] { String[].class });
+         mainMethod.setAccessible(true);
+ 
+         if (wrapper.equals("Virtual")) {
+             MainThreadGroup tg = new MainThreadGroup();
+             // TODO fix to set virtual scheduler group when become available
+             Thread vthread = startVirtualThread(() -> {
+                 try {
+                     mainMethod.invoke(null, new Object[] { classArgs });
+                 } catch (InvocationTargetException e) {
+                     tg.uncaughtThrowable = e.getCause();
+                 } catch (Throwable error) {
+                     tg.uncaughtThrowable = error;
+                 }
+             });
+             vthread.join();
+         } else if (wrapper.equals("Kernel")) {
+             MainThreadGroup tg = new MainThreadGroup();
+             Thread t = new Thread(tg, () -> {
+                 try {
+                     mainMethod.invoke(null, new Object[] { classArgs });
+                 } catch (InvocationTargetException e) {
+                     tg.uncaughtThrowable = e.getCause();
+                 } catch (Throwable error) {
+                     tg.uncaughtThrowable = error;
+                 }
+             });
+             t.start();
+             t.join();
+             if (tg.uncaughtThrowable != null) {
+                 throw new RuntimeException(tg.uncaughtThrowable);
+             }
+         } else {
+             mainMethod.invoke(null, new Object[] { classArgs });
+         }
+     }
+ 
+     static class MainThreadGroup extends ThreadGroup {
+         MainThreadGroup() {
+             super("MainThreadGroup");
+         }
+ 
+         public void uncaughtException(Thread t, Throwable e) {
+             if (e instanceof ThreadDeath) {
+                 return;
+             }
+             e.printStackTrace(System.err);
+             uncaughtThrowable = e;
+         }
+         Throwable uncaughtThrowable = null;
+     }
+ 
+     static Thread startVirtualThread(Runnable task) {
+         try {
+             Object builder = Thread.class.getMethod("ofVirtual").invoke(null);
+             Class<?> clazz = Class.forName("java.lang.Thread$Builder");
+             java.lang.reflect.Method start = clazz.getMethod("start", Runnable.class);
+             return (Thread) start.invoke(builder, task);
+         } catch (RuntimeException | Error e) {
+             throw e;
+         } catch (Exception e) {
+             throw new RuntimeException(e);
+         }
+     }
  }
< prev index next >