1 /*
   2  * Copyright (c) 2017, 2018, 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 package metaspace.stressHierarchy.common;
  25 
  26 import jdk.test.lib.JDKToolLauncher;
  27 import jdk.test.lib.Utils;
  28 import jdk.test.lib.process.ProcessTools;
  29 
  30 import java.io.IOException;
  31 import java.nio.file.Files;
  32 import java.nio.file.Path;
  33 import java.nio.file.Paths;
  34 import java.util.Arrays;
  35 import java.util.stream.Stream;
  36 
  37 public class GenClassesBuilder {
  38     public static void main(String[] args) {
  39         Path srcDst = Paths.get("genSrc").toAbsolutePath();
  40         Path classesDir = Paths.get(Utils.TEST_CLASSES).toAbsolutePath();
  41         generateSource(srcDst);
  42         compileSource(srcDst, classesDir);
  43     }
  44 
  45     private static void compileSource(Path srcDst, Path classesDir) {
  46         JDKToolLauncher javac = JDKToolLauncher.create("javac")
  47                                                .addToolArg("-d")
  48                                                .addToolArg(classesDir.toString())
  49                                                .addToolArg("-cp")
  50                                                .addToolArg(Utils.TEST_CLASS_PATH);
  51         try (Stream<Path> stream = Files.walk(srcDst)) {
  52             stream.map(Path::toAbsolutePath)
  53                   .map(Path::toString)
  54                   .filter(s -> s.endsWith(".java"))
  55                   .forEach(javac::addToolArg);
  56         } catch (IOException e) {
  57             throw new Error("traverse source dir " + srcDst, e);
  58         }
  59         String[] command = javac.getCommand();
  60         try {
  61             ProcessTools.executeCommand(command)
  62                         .shouldHaveExitValue(0);
  63         } catch (Error | RuntimeException e) {
  64             throw e;
  65         } catch (Throwable e) {
  66             throw new Error("execution of javac(" + Arrays.toString(command) + ") failed", e);
  67         }
  68     }
  69 
  70     private static void generateSource(Path dir) {
  71         try {
  72             Files.createDirectories(dir);
  73         } catch (IOException e) {
  74             throw new Error("can't create dirs for" + dir, e);
  75         }
  76 
  77         try {
  78             HumongousClassGen.main(new String[]{dir.toString()});
  79         } catch (Exception e) {
  80             throw new Error("can't generate classes", e);
  81         }
  82     }
  83 }
  84