36511
|
1 |
/*
|
|
2 |
* Copyright (c) 2015, 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. Oracle designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Oracle in the LICENSE file that accompanied this code.
|
|
10 |
*
|
|
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
15 |
* accompanied this code).
|
|
16 |
*
|
|
17 |
* You should have received a copy of the GNU General Public License version
|
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
20 |
*
|
|
21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
22 |
* or visit www.oracle.com if you need additional information or have any
|
|
23 |
* questions.
|
|
24 |
*/
|
|
25 |
package build.tools.module;
|
|
26 |
|
|
27 |
import java.io.BufferedWriter;
|
|
28 |
import java.nio.file.Files;
|
|
29 |
import java.nio.file.Path;
|
|
30 |
import java.nio.file.Paths;
|
|
31 |
import java.util.HashMap;
|
|
32 |
import java.util.HashSet;
|
|
33 |
import java.util.Map;
|
|
34 |
import java.util.Set;
|
|
35 |
|
|
36 |
/**
|
|
37 |
* A build tool to extend the module-info.java in the source tree
|
|
38 |
* for platform-specific exports, uses, and provides and write
|
|
39 |
* to the specified output file.
|
|
40 |
*
|
|
41 |
* GenModulesList build tool currently generates the modules.list from
|
|
42 |
* the module-info.java from the source tree that will be used for
|
|
43 |
* the make target and dependences.
|
|
44 |
*
|
|
45 |
* The build currently invokes gensrc-$MODULE.gmk after modules.list
|
|
46 |
* is generated. Hence, platform-specific requires is not supported.
|
|
47 |
*/
|
|
48 |
public class GenModuleInfoSource {
|
|
49 |
private final static String USAGE =
|
|
50 |
"Usage: GenModuleInfoSource [option] -o <output file> <module-info-java>\n" +
|
|
51 |
"Options are:\n" +
|
|
52 |
" -exports <package-name>\n" +
|
|
53 |
" -exports <package-name>/<module-name>\n" +
|
|
54 |
" -uses <service>\n" +
|
|
55 |
" -provides <service>/<provider-impl-classname>\n";
|
|
56 |
|
|
57 |
public static void main(String... args) throws Exception {
|
|
58 |
Path outfile = null;
|
|
59 |
Path moduleInfoJava = null;
|
|
60 |
Map<String, Set<String>> options = new HashMap<>();
|
|
61 |
|
|
62 |
// validate input arguments
|
|
63 |
for (int i = 0; i < args.length; i++){
|
|
64 |
String option = args[i];
|
|
65 |
if (option.startsWith("-")) {
|
|
66 |
String arg = args[++i];
|
|
67 |
if (option.equals("-exports") ||
|
|
68 |
option.equals("-uses") ||
|
|
69 |
option.equals("-provides")) {
|
|
70 |
options.computeIfAbsent(option, _k -> new HashSet<>()).add(arg);
|
|
71 |
} else if (option.equals("-o")) {
|
|
72 |
outfile = Paths.get(arg);
|
|
73 |
} else {
|
|
74 |
throw new IllegalArgumentException("invalid option: " + option);
|
|
75 |
}
|
|
76 |
} else if (moduleInfoJava != null) {
|
|
77 |
throw new IllegalArgumentException("more than one module-info.java");
|
|
78 |
} else {
|
|
79 |
moduleInfoJava = Paths.get(option);
|
|
80 |
if (Files.notExists(moduleInfoJava)) {
|
|
81 |
throw new IllegalArgumentException(option + " not exist");
|
|
82 |
}
|
|
83 |
}
|
|
84 |
}
|
|
85 |
|
|
86 |
if (moduleInfoJava == null || outfile == null) {
|
|
87 |
System.err.println(USAGE);
|
|
88 |
System.exit(-1);
|
|
89 |
}
|
|
90 |
// read module-info.java
|
|
91 |
Module.Builder builder = ModuleInfoReader.builder(moduleInfoJava);
|
|
92 |
augment(builder, options);
|
|
93 |
|
|
94 |
// generate new module-info.java
|
|
95 |
Module module = builder.build();
|
|
96 |
Path parent = outfile.getParent();
|
|
97 |
if (parent != null)
|
|
98 |
Files.createDirectories(parent);
|
|
99 |
|
|
100 |
try (BufferedWriter writer = Files.newBufferedWriter(outfile)) {
|
|
101 |
writer.write(module.toString());
|
|
102 |
}
|
|
103 |
}
|
|
104 |
|
|
105 |
private static void augment(Module.Builder builder, Map<String, Set<String>> options) {
|
|
106 |
for (String opt : options.keySet()) {
|
|
107 |
if (opt.equals("-exports")) {
|
|
108 |
for (String arg : options.get(opt)) {
|
|
109 |
int index = arg.indexOf('/');
|
|
110 |
if (index > 0) {
|
|
111 |
String pn = arg.substring(0, index);
|
|
112 |
String mn = arg.substring(index + 1, arg.length());
|
|
113 |
builder.exportTo(pn, mn);
|
|
114 |
} else {
|
|
115 |
builder.export(arg);
|
|
116 |
}
|
|
117 |
}
|
|
118 |
} else if (opt.equals("-uses")) {
|
|
119 |
options.get(opt).stream()
|
|
120 |
.forEach(builder::use);
|
|
121 |
} else if (opt.equals("-provides")) {
|
|
122 |
for (String arg : options.get(opt)) {
|
|
123 |
int index = arg.indexOf('/');
|
|
124 |
if (index <= 0) {
|
|
125 |
throw new IllegalArgumentException("invalid -provide argument: " + arg);
|
|
126 |
}
|
|
127 |
String service = arg.substring(0, index);
|
|
128 |
String impl = arg.substring(index + 1, arg.length());
|
|
129 |
builder.provide(service, impl);
|
|
130 |
}
|
|
131 |
}
|
|
132 |
}
|
|
133 |
}
|
|
134 |
}
|