1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
|
/*
* Copyright (c) 2015, 2017, 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.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package toolbox;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
/**
* Builder for module declarations.
*/
public class ModuleBuilder {
private final ToolBox tb;
private final String name;
private String comment = "";
private List<String> requires = new ArrayList<>();
private List<String> exports = new ArrayList<>();
private List<String> opens = new ArrayList<>();
private List<String> uses = new ArrayList<>();
private List<String> provides = new ArrayList<>();
private List<String> content = new ArrayList<>();
private Set<Path> modulePath = new LinkedHashSet<>();
/**
* Creates a builder for a module.
* @param tb a Toolbox that can be used to compile the module declaration.
* @param name the name of the module to be built
*/
public ModuleBuilder(ToolBox tb, String name) {
this.tb = tb;
this.name = name;
}
/**
* Sets the doc comment for the declaration.
* @param comment the content of the comment, excluding the initial
* '/**', leading whitespace and asterisks, and the final trailing 'a;/'.
* @return this builder
*/
public ModuleBuilder comment(String comment) {
this.comment = comment;
return this;
}
/**
* Adds a "requires" directive to the declaration.
* @param module the name of the module that is required
* @param modulePath a path in while to locate the modules
* if the declaration is compiled
* @return this builder
*/
public ModuleBuilder requires(String module, Path... modulePath) {
addDirective(requires, "requires " + module + ";");
this.modulePath.addAll(Arrays.asList(modulePath));
return this;
}
/**
* Adds a "requires static" directive to the declaration.
* @param module the name of the module that is required
* @param modulePath a path in which to locate the modules
* if the declaration is compiled
* @return this builder
*/
public ModuleBuilder requiresStatic(String module, Path... modulePath) {
addDirective(requires, "requires static " + module + ";");
this.modulePath.addAll(Arrays.asList(modulePath));
return this;
}
/**
* Adds a "requires transitive" directive to the declaration.
* @param module the name of the module that is required
* @param modulePath a path in which to locate the modules
* if the declaration is compiled
* @return this builder
*/
public ModuleBuilder requiresTransitive(String module, Path... modulePath) {
addDirective(requires, "requires transitive " + module + ";");
this.modulePath.addAll(Arrays.asList(modulePath));
return this;
}
/**
* Adds a "requires static transitive" directive to the declaration.
* @param module the name of the module that is required
* @param modulePath a path in which to locate the modules
* if the declaration is compiled
* @return this builder
*/
public ModuleBuilder requiresStaticTransitive(String module, Path... modulePath) {
addDirective(requires, "requires static transitive " + module + ";");
this.modulePath.addAll(Arrays.asList(modulePath));
return this;
}
/**
* Adds an unqualified "exports" directive to the declaration.
* @param pkg the name of the package to be exported
* @return this builder
*/
public ModuleBuilder exports(String pkg) {
return addDirective(exports, "exports " + pkg + ";");
}
/**
* Adds a qualified "exports" directive to the declaration.
* @param pkg the name of the package to be exported
* @param module the name of the module to which it is to be exported
* @return this builder
*/
public ModuleBuilder exportsTo(String pkg, String module) {
return addDirective(exports, "exports " + pkg + " to " + module + ";");
}
/**
* Adds an unqualified "opens" directive to the declaration.
* @param pkg the name of the package to be opened
* @return this builder
*/
public ModuleBuilder opens(String pkg) {
return addDirective(opens, "opens " + pkg + ";");
}
/**
* Adds a qualified "opens" directive to the declaration.
* @param pkg the name of the package to be opened
* @param module the name of the module to which it is to be opened
* @return this builder
*/
public ModuleBuilder opensTo(String pkg, String module) {
return addDirective(opens, "opens " + pkg + " to " + module + ";");
}
/**
* Adds a "uses" directive to the declaration.
* @param service the name of the service type
* @return this builder
*/
public ModuleBuilder uses(String service) {
return addDirective(uses, "uses " + service + ";");
}
/**
* Adds a "provides" directive to the declaration.
* @param service the name of the service type
* @param implementation the name of the implementation type
* @return this builder
*/
public ModuleBuilder provides(String service, String implementation) {
return addDirective(provides, "provides " + service + " with " + implementation + ";");
}
private ModuleBuilder addDirective(List<String> directives, String directive) {
directives.add(directive);
return this;
}
/**
* Adds type definitions to the module.
* @param content a series of strings, each representing the content of
* a compilation unit to be included with the module
* @return this builder
*/
public ModuleBuilder classes(String... content) {
this.content.addAll(Arrays.asList(content));
return this;
}
/**
* Writes the module declaration and associated additional compilation
* units to a module directory within a given directory.
* @param srcDir the directory in which a directory will be created
* to contain the source files for the module
* @return the directory containing the source files for the module
*/
public Path write(Path srcDir) throws IOException {
Files.createDirectories(srcDir);
List<String> sources = new ArrayList<>();
StringBuilder sb = new StringBuilder();
if (!comment.isEmpty()) {
sb.append("/**\n * ")
.append(comment.replace("\n", "\n * "))
.append("\n */\n");
}
sb.append("module ").append(name).append(" {\n");
requires.forEach(r -> sb.append(" " + r + "\n"));
exports.forEach(e -> sb.append(" " + e + "\n"));
opens.forEach(o -> sb.append(" " + o + "\n"));
uses.forEach(u -> sb.append(" " + u + "\n"));
provides.forEach(p -> sb.append(" " + p + "\n"));
sb.append("}");
sources.add(sb.toString());
sources.addAll(content);
Path moduleSrc = srcDir.resolve(name);
tb.writeJavaFiles(moduleSrc, sources.toArray(new String[]{}));
return moduleSrc;
}
/**
* Writes the source files for the module to an interim directory,
* and then compiles them to a given directory.
* @param modules the directory in which a directory will be created
* to contain the compiled class files for the module
* @throws IOException if an error occurs while compiling the files
*/
public void build(Path modules) throws IOException {
build(Paths.get(modules + "Src"), modules);
}
/**
* Writes the source files for the module to a specified directory,
* and then compiles them to a given directory.
* @param srcDir the directory in which a directory will be created
* to contain the source files for the module
* @param modules the directory in which a directory will be created
* to contain the compiled class files for the module
* @throws IOException if an error occurs while compiling the files
*/
public void build(Path src, Path modules) throws IOException {
Path moduleSrc = write(src);
String mp = modulePath.stream()
.map(Path::toString)
.collect(Collectors.joining(File.pathSeparator));
new JavacTask(tb)
.outdir(Files.createDirectories(modules.resolve(name)))
.options("--module-path", mp)
.files(tb.findJavaFiles(moduleSrc))
.run()
.writeAll();
}
}
|