Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/langtools/tools/lib/toolbox/ModuleBuilder.java
41149 views
1
/*
2
* Copyright (c) 2015, 2017, 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 toolbox;
25
26
import java.io.File;
27
import java.io.IOException;
28
import java.nio.file.Files;
29
import java.nio.file.Path;
30
import java.nio.file.Paths;
31
import java.util.ArrayList;
32
import java.util.Arrays;
33
import java.util.LinkedHashSet;
34
import java.util.List;
35
import java.util.Set;
36
import java.util.stream.Collectors;
37
38
/**
39
* Builder for module declarations.
40
*/
41
public class ModuleBuilder {
42
43
private final ToolBox tb;
44
private final String name;
45
private String comment = "";
46
private boolean open;
47
private List<String> requires = new ArrayList<>();
48
private List<String> exports = new ArrayList<>();
49
private List<String> opens = new ArrayList<>();
50
private List<String> uses = new ArrayList<>();
51
private List<String> provides = new ArrayList<>();
52
private List<String> content = new ArrayList<>();
53
private Set<Path> modulePath = new LinkedHashSet<>();
54
55
/**
56
* Creates a builder for a module.
57
* @param tb a Toolbox that can be used to compile the module declaration
58
* @param name the name of the module to be built
59
*/
60
public ModuleBuilder(ToolBox tb, String name) {
61
this(tb, false, name);
62
}
63
64
/**
65
* Creates a builder for a module.
66
* @param tb a Toolbox that can be used to compile the module declaration
67
* @param open whether or not this is an open module
68
* @param name the name of the module to be built
69
*/
70
public ModuleBuilder(ToolBox tb, boolean open, String name) {
71
this.tb = tb;
72
this.open = open;
73
this.name = name;
74
}
75
76
/**
77
* Sets the doc comment for the declaration.
78
* @param comment the content of the comment, excluding the initial
79
* '/**', leading whitespace and asterisks, and the final trailing '&#02a;/'.
80
* @return this builder
81
*/
82
public ModuleBuilder comment(String comment) {
83
this.comment = comment;
84
return this;
85
}
86
87
/**
88
* Adds a "requires" directive to the declaration.
89
* @param module the name of the module that is required
90
* @param modulePath a path in while to locate the modules
91
* if the declaration is compiled
92
* @return this builder
93
*/
94
public ModuleBuilder requires(String module, Path... modulePath) {
95
addDirective(requires, "requires " + module + ";");
96
this.modulePath.addAll(Arrays.asList(modulePath));
97
return this;
98
99
}
100
101
/**
102
* Adds a "requires static" directive to the declaration.
103
* @param module the name of the module that is required
104
* @param modulePath a path in which to locate the modules
105
* if the declaration is compiled
106
* @return this builder
107
*/
108
public ModuleBuilder requiresStatic(String module, Path... modulePath) {
109
addDirective(requires, "requires static " + module + ";");
110
this.modulePath.addAll(Arrays.asList(modulePath));
111
return this;
112
}
113
114
/**
115
* Adds a "requires transitive" directive to the declaration.
116
* @param module the name of the module that is required
117
* @param modulePath a path in which to locate the modules
118
* if the declaration is compiled
119
* @return this builder
120
*/
121
public ModuleBuilder requiresTransitive(String module, Path... modulePath) {
122
addDirective(requires, "requires transitive " + module + ";");
123
this.modulePath.addAll(Arrays.asList(modulePath));
124
return this;
125
}
126
127
/**
128
* Adds a "requires static transitive" directive to the declaration.
129
* @param module the name of the module that is required
130
* @param modulePath a path in which to locate the modules
131
* if the declaration is compiled
132
* @return this builder
133
*/
134
public ModuleBuilder requiresStaticTransitive(String module, Path... modulePath) {
135
addDirective(requires, "requires static transitive " + module + ";");
136
this.modulePath.addAll(Arrays.asList(modulePath));
137
return this;
138
}
139
140
/**
141
* Adds an unqualified "exports" directive to the declaration.
142
* @param pkg the name of the package to be exported
143
* @return this builder
144
*/
145
public ModuleBuilder exports(String pkg) {
146
return addDirective(exports, "exports " + pkg + ";");
147
}
148
149
/**
150
* Adds a qualified "exports" directive to the declaration.
151
* @param pkg the name of the package to be exported
152
* @param module the name of the module to which it is to be exported
153
* @return this builder
154
*/
155
public ModuleBuilder exportsTo(String pkg, String module) {
156
return addDirective(exports, "exports " + pkg + " to " + module + ";");
157
}
158
159
/**
160
* Adds an unqualified "opens" directive to the declaration.
161
* @param pkg the name of the package to be opened
162
* @return this builder
163
*/
164
public ModuleBuilder opens(String pkg) {
165
return addDirective(opens, "opens " + pkg + ";");
166
}
167
168
/**
169
* Adds a qualified "opens" directive to the declaration.
170
* @param pkg the name of the package to be opened
171
* @param module the name of the module to which it is to be opened
172
* @return this builder
173
*/
174
public ModuleBuilder opensTo(String pkg, String module) {
175
return addDirective(opens, "opens " + pkg + " to " + module + ";");
176
}
177
178
/**
179
* Adds a "uses" directive to the declaration.
180
* @param service the name of the service type
181
* @return this builder
182
*/
183
public ModuleBuilder uses(String service) {
184
return addDirective(uses, "uses " + service + ";");
185
}
186
187
/**
188
* Adds a "provides" directive to the declaration.
189
* @param service the name of the service type
190
* @param implementation the name of the implementation type
191
* @return this builder
192
*/
193
public ModuleBuilder provides(String service, String implementation) {
194
return addDirective(provides, "provides " + service + " with " + implementation + ";");
195
}
196
197
private ModuleBuilder addDirective(List<String> directives, String directive) {
198
directives.add(directive);
199
return this;
200
}
201
202
/**
203
* Adds type definitions to the module.
204
* @param content a series of strings, each representing the content of
205
* a compilation unit to be included with the module
206
* @return this builder
207
*/
208
public ModuleBuilder classes(String... content) {
209
this.content.addAll(Arrays.asList(content));
210
return this;
211
}
212
213
/**
214
* Writes the module declaration and associated additional compilation
215
* units to a module directory within a given directory.
216
* @param srcDir the directory in which a directory will be created
217
* to contain the source files for the module
218
* @return the directory containing the source files for the module
219
*/
220
public Path write(Path srcDir) throws IOException {
221
Files.createDirectories(srcDir);
222
List<String> sources = new ArrayList<>();
223
StringBuilder sb = new StringBuilder();
224
if (!comment.isEmpty()) {
225
sb.append("/**\n * ")
226
.append(comment.replace("\n", "\n * "))
227
.append("\n */\n");
228
}
229
if (open) {
230
sb.append("open ");
231
}
232
sb.append("module ").append(name).append(" {\n");
233
requires.forEach(r -> sb.append(" " + r + "\n"));
234
exports.forEach(e -> sb.append(" " + e + "\n"));
235
opens.forEach(o -> sb.append(" " + o + "\n"));
236
uses.forEach(u -> sb.append(" " + u + "\n"));
237
provides.forEach(p -> sb.append(" " + p + "\n"));
238
sb.append("}");
239
sources.add(sb.toString());
240
sources.addAll(content);
241
Path moduleSrc = srcDir.resolve(name);
242
tb.writeJavaFiles(moduleSrc, sources.toArray(new String[]{}));
243
return moduleSrc;
244
}
245
246
/**
247
* Writes the source files for the module to an interim directory,
248
* and then compiles them to a given directory.
249
* @param modules the directory in which a directory will be created
250
* to contain the compiled class files for the module
251
* @throws IOException if an error occurs while compiling the files
252
*/
253
public void build(Path modules) throws IOException {
254
build(Paths.get(modules + "Src"), modules);
255
}
256
257
/**
258
* Writes the source files for the module to a specified directory,
259
* and then compiles them to a given directory.
260
* @param srcDir the directory in which a directory will be created
261
* to contain the source files for the module
262
* @param modules the directory in which a directory will be created
263
* to contain the compiled class files for the module
264
* @throws IOException if an error occurs while compiling the files
265
*/
266
public void build(Path src, Path modules) throws IOException {
267
Path moduleSrc = write(src);
268
String mp = modulePath.stream()
269
.map(Path::toString)
270
.collect(Collectors.joining(File.pathSeparator));
271
new JavacTask(tb)
272
.outdir(Files.createDirectories(modules.resolve(name)))
273
.options("--module-path", mp)
274
.files(tb.findJavaFiles(moduleSrc))
275
.run()
276
.writeAll();
277
}
278
}
279
280