< prev index next >

test/lib/jdk/test/lib/process/ProcessTools.java

Print this page
*** 1,4 ***
--- 1,6 ---
+ 
  /*
   * Copyright (c) 2013, 2020, 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

*** 27,10 ***
--- 28,11 ---
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.OutputStream;
  import java.io.PrintStream;
  import java.nio.charset.Charset;
+ import java.lang.reflect.Method;
  import java.nio.file.Paths;
  import java.util.ArrayList;
  import java.util.Arrays;
  import java.util.Collections;
  import java.util.concurrent.CountDownLatch;

*** 293,11 ***
  
          if (addTestVmAndJavaOptions) {
              Collections.addAll(args, Utils.getTestJavaOpts());
          }
  
!         Collections.addAll(args, command);
  
          // Reporting
          StringBuilder cmdLine = new StringBuilder();
          for (String cmd : args)
              cmdLine.append(cmd).append(' ');
--- 295,73 ---
  
          if (addTestVmAndJavaOptions) {
              Collections.addAll(args, Utils.getTestJavaOpts());
          }
  
!         boolean noModule = true;
+         for (String cmd: command) {
+             if (cmd.equals("-m")) {
+                 noModule = false;
+             }
+         }
+ 
+         String[] doubleWordArgs = {"-cp", "-classpath", "--add-opens", "--class-path", "--upgrade-module-path",
+                                    "--add-modules", "-d", "--add-exports", "--patch-module", "--module-path"};
+ 
+         if (noModule && System.getProperty("main.wrapper") != null) {
+             boolean skipNext = false;
+             boolean added = false;
+             for (String cmd : command) {
+                 if (added) {
+                     args.add(cmd);
+                     continue;
+                 }
+ 
+                 if (skipNext) {
+                     skipNext = false;
+                     args.add(cmd);
+                     continue;
+                 }
+                 for (String dWArg : doubleWordArgs) {
+                     if (cmd.equals(dWArg)) {
+                         skipNext = true;
+                         args.add(cmd);
+                         continue;
+                     }
+                 }
+                 if (skipNext) {
+                     continue;
+                 }
+                 if (cmd.startsWith("-cp")) {
+                     skipNext = true;
+                     args.add(cmd);
+                     continue;
+                 }
+                 if (cmd.startsWith("--add-exports")) {
+                     skipNext = true;
+                     args.add(cmd);
+                     continue;
+                 }
+                 if (cmd.startsWith("--patch-module")) {
+                     skipNext = true;
+                     args.add(cmd);
+                     continue;
+                 }
+                 if (cmd.startsWith("-")) {
+                     args.add(cmd);
+                     continue;
+                 }
+                 args.add("jdk.test.lib.process.ProcessTools");
+                 args.add(System.getProperty("main.wrapper"));
+                 added = true;
+                 // Should be main
+                 // System.out.println("Wrapped TOFIND: " + cmd);
+                 args.add(cmd);
+             }
+         } else {
+             Collections.addAll(args, command);
+         }
  
          // Reporting
          StringBuilder cmdLine = new StringBuilder();
          for (String cmd : args)
              cmdLine.append(cmd).append(' ');

*** 622,6 ***
--- 686,61 ---
                  stderrTask.get();
              } catch (ExecutionException e) {
              }
          }
      }
+ 
+     // ProcessTools as a wrapper
+     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);
+         Method mainMethod = c.getMethod("main", new Class[] { String[].class });
+ 
+         if (wrapper.equals("Virtual")) {
+             MainThreadGroup tg = new MainThreadGroup();
+             // TODO fix to set virtual scheduler group when become available
+             Thread vthread = Thread.builder().virtual().task(() -> {
+                     try {
+                         mainMethod.invoke(null, new Object[] { classArgs });
+                     } catch (Throwable error) {
+                         tg.uncaughtThrowable = error;
+                     }
+                 }).build();
+             vthread.start();
+             vthread.join();
+         } else if (wrapper.equals("Kernel")) {
+             MainThreadGroup tg = new MainThreadGroup();
+             Thread t = new Thread(tg, () -> {
+                     try {
+                         mainMethod.invoke(null, new Object[] { classArgs });
+                     } 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;
+     }
  }
< prev index next >