Path: blob/master/test/langtools/tools/lib/toolbox/ModuleBuilder.java
41149 views
/*1* Copyright (c) 2015, 2017, 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 toolbox;2425import java.io.File;26import java.io.IOException;27import java.nio.file.Files;28import java.nio.file.Path;29import java.nio.file.Paths;30import java.util.ArrayList;31import java.util.Arrays;32import java.util.LinkedHashSet;33import java.util.List;34import java.util.Set;35import java.util.stream.Collectors;3637/**38* Builder for module declarations.39*/40public class ModuleBuilder {4142private final ToolBox tb;43private final String name;44private String comment = "";45private boolean open;46private List<String> requires = new ArrayList<>();47private List<String> exports = new ArrayList<>();48private List<String> opens = new ArrayList<>();49private List<String> uses = new ArrayList<>();50private List<String> provides = new ArrayList<>();51private List<String> content = new ArrayList<>();52private Set<Path> modulePath = new LinkedHashSet<>();5354/**55* Creates a builder for a module.56* @param tb a Toolbox that can be used to compile the module declaration57* @param name the name of the module to be built58*/59public ModuleBuilder(ToolBox tb, String name) {60this(tb, false, name);61}6263/**64* Creates a builder for a module.65* @param tb a Toolbox that can be used to compile the module declaration66* @param open whether or not this is an open module67* @param name the name of the module to be built68*/69public ModuleBuilder(ToolBox tb, boolean open, String name) {70this.tb = tb;71this.open = open;72this.name = name;73}7475/**76* Sets the doc comment for the declaration.77* @param comment the content of the comment, excluding the initial78* '/**', leading whitespace and asterisks, and the final trailing 'a;/'.79* @return this builder80*/81public ModuleBuilder comment(String comment) {82this.comment = comment;83return this;84}8586/**87* Adds a "requires" directive to the declaration.88* @param module the name of the module that is required89* @param modulePath a path in while to locate the modules90* if the declaration is compiled91* @return this builder92*/93public ModuleBuilder requires(String module, Path... modulePath) {94addDirective(requires, "requires " + module + ";");95this.modulePath.addAll(Arrays.asList(modulePath));96return this;9798}99100/**101* Adds a "requires static" directive to the declaration.102* @param module the name of the module that is required103* @param modulePath a path in which to locate the modules104* if the declaration is compiled105* @return this builder106*/107public ModuleBuilder requiresStatic(String module, Path... modulePath) {108addDirective(requires, "requires static " + module + ";");109this.modulePath.addAll(Arrays.asList(modulePath));110return this;111}112113/**114* Adds a "requires transitive" directive to the declaration.115* @param module the name of the module that is required116* @param modulePath a path in which to locate the modules117* if the declaration is compiled118* @return this builder119*/120public ModuleBuilder requiresTransitive(String module, Path... modulePath) {121addDirective(requires, "requires transitive " + module + ";");122this.modulePath.addAll(Arrays.asList(modulePath));123return this;124}125126/**127* Adds a "requires static transitive" directive to the declaration.128* @param module the name of the module that is required129* @param modulePath a path in which to locate the modules130* if the declaration is compiled131* @return this builder132*/133public ModuleBuilder requiresStaticTransitive(String module, Path... modulePath) {134addDirective(requires, "requires static transitive " + module + ";");135this.modulePath.addAll(Arrays.asList(modulePath));136return this;137}138139/**140* Adds an unqualified "exports" directive to the declaration.141* @param pkg the name of the package to be exported142* @return this builder143*/144public ModuleBuilder exports(String pkg) {145return addDirective(exports, "exports " + pkg + ";");146}147148/**149* Adds a qualified "exports" directive to the declaration.150* @param pkg the name of the package to be exported151* @param module the name of the module to which it is to be exported152* @return this builder153*/154public ModuleBuilder exportsTo(String pkg, String module) {155return addDirective(exports, "exports " + pkg + " to " + module + ";");156}157158/**159* Adds an unqualified "opens" directive to the declaration.160* @param pkg the name of the package to be opened161* @return this builder162*/163public ModuleBuilder opens(String pkg) {164return addDirective(opens, "opens " + pkg + ";");165}166167/**168* Adds a qualified "opens" directive to the declaration.169* @param pkg the name of the package to be opened170* @param module the name of the module to which it is to be opened171* @return this builder172*/173public ModuleBuilder opensTo(String pkg, String module) {174return addDirective(opens, "opens " + pkg + " to " + module + ";");175}176177/**178* Adds a "uses" directive to the declaration.179* @param service the name of the service type180* @return this builder181*/182public ModuleBuilder uses(String service) {183return addDirective(uses, "uses " + service + ";");184}185186/**187* Adds a "provides" directive to the declaration.188* @param service the name of the service type189* @param implementation the name of the implementation type190* @return this builder191*/192public ModuleBuilder provides(String service, String implementation) {193return addDirective(provides, "provides " + service + " with " + implementation + ";");194}195196private ModuleBuilder addDirective(List<String> directives, String directive) {197directives.add(directive);198return this;199}200201/**202* Adds type definitions to the module.203* @param content a series of strings, each representing the content of204* a compilation unit to be included with the module205* @return this builder206*/207public ModuleBuilder classes(String... content) {208this.content.addAll(Arrays.asList(content));209return this;210}211212/**213* Writes the module declaration and associated additional compilation214* units to a module directory within a given directory.215* @param srcDir the directory in which a directory will be created216* to contain the source files for the module217* @return the directory containing the source files for the module218*/219public Path write(Path srcDir) throws IOException {220Files.createDirectories(srcDir);221List<String> sources = new ArrayList<>();222StringBuilder sb = new StringBuilder();223if (!comment.isEmpty()) {224sb.append("/**\n * ")225.append(comment.replace("\n", "\n * "))226.append("\n */\n");227}228if (open) {229sb.append("open ");230}231sb.append("module ").append(name).append(" {\n");232requires.forEach(r -> sb.append(" " + r + "\n"));233exports.forEach(e -> sb.append(" " + e + "\n"));234opens.forEach(o -> sb.append(" " + o + "\n"));235uses.forEach(u -> sb.append(" " + u + "\n"));236provides.forEach(p -> sb.append(" " + p + "\n"));237sb.append("}");238sources.add(sb.toString());239sources.addAll(content);240Path moduleSrc = srcDir.resolve(name);241tb.writeJavaFiles(moduleSrc, sources.toArray(new String[]{}));242return moduleSrc;243}244245/**246* Writes the source files for the module to an interim directory,247* and then compiles them to a given directory.248* @param modules the directory in which a directory will be created249* to contain the compiled class files for the module250* @throws IOException if an error occurs while compiling the files251*/252public void build(Path modules) throws IOException {253build(Paths.get(modules + "Src"), modules);254}255256/**257* Writes the source files for the module to a specified directory,258* and then compiles them to a given directory.259* @param srcDir the directory in which a directory will be created260* to contain the source files for the module261* @param modules the directory in which a directory will be created262* to contain the compiled class files for the module263* @throws IOException if an error occurs while compiling the files264*/265public void build(Path src, Path modules) throws IOException {266Path moduleSrc = write(src);267String mp = modulePath.stream()268.map(Path::toString)269.collect(Collectors.joining(File.pathSeparator));270new JavacTask(tb)271.outdir(Files.createDirectories(modules.resolve(name)))272.options("--module-path", mp)273.files(tb.findJavaFiles(moduleSrc))274.run()275.writeAll();276}277}278279280