Path: blob/master/test/hotspot/jtreg/vmTestbase/nsk/share/gc/GenClassesBuilder.java
41161 views
/*1* Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223package nsk.share.gc;2425import jdk.test.lib.JDKToolLauncher;26import jdk.test.lib.Utils;27import jdk.test.lib.process.ProcessTools;2829import java.io.IOException;30import java.nio.file.Files;31import java.nio.file.Path;32import java.nio.file.Paths;33import java.util.Arrays;34import java.util.stream.Stream;3536/**37* Uses {@link nsk.share.gc.Generator} to genearate {@code nsk.share.gc.newclass}38* classes and compile them to 'classes' directory.39*/40public class GenClassesBuilder {41public static void main(String[] args) {42Path srcDst = Paths.get(43"genSrc",44"nsk",45"share",46"gc",47"newclass").toAbsolutePath();48Path classesDir = Paths.get("classes").toAbsolutePath();49generateSource(srcDst);50compileSource(srcDst, classesDir);51}5253private static void compileSource(Path srcDst, Path classesDir) {54JDKToolLauncher javac = JDKToolLauncher.create("javac")55.addToolArg("-d")56.addToolArg(classesDir.toString())57.addToolArg("-cp")58.addToolArg(Utils.TEST_CLASS_PATH);59try (Stream<Path> stream = Files.walk(srcDst)) {60stream.map(Path::toAbsolutePath)61.map(Path::toString)62.filter(s -> s.endsWith(".java"))63.forEach(javac::addToolArg);64} catch (IOException e) {65throw new Error("traverse source dir " + srcDst, e);66}67String[] command = javac.getCommand();68try {69ProcessTools.executeCommand(command)70.shouldHaveExitValue(0);71} catch (Error | RuntimeException e) {72throw e;73} catch (Throwable e) {74throw new Error("execution of javac(" + Arrays.toString(command) + ") failed", e);75}76}7778private static void generateSource(Path dir) {79try {80Files.createDirectories(dir);81} catch (IOException e) {82throw new Error("can't create dirs for" + dir, e);83}84try {85Generator.main(new String[]{dir.toString()});86} catch (Exception e) {87throw new Error("can't generate classes", e);88}89}90}919293