Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/jdk17u
Path: blob/master/src/jdk.jartool/share/classes/sun/tools/jar/GNUStyleOptions.java
66646 views
1
/*
2
* Copyright (c) 2015, 2018, 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
26
package sun.tools.jar;
27
28
import java.io.File;
29
import java.io.PrintWriter;
30
import java.lang.module.ModuleDescriptor.Version;
31
import java.nio.file.Path;
32
import java.nio.file.Paths;
33
import java.util.regex.Pattern;
34
import java.util.regex.PatternSyntaxException;
35
import jdk.internal.module.ModulePath;
36
import jdk.internal.module.ModuleResolution;
37
import java.time.ZoneOffset;
38
import java.time.ZonedDateTime;
39
import java.time.LocalDateTime;
40
import java.time.format.DateTimeFormatter;
41
import java.time.format.DateTimeParseException;
42
43
/**
44
* Parser for GNU Style Options.
45
*/
46
class GNUStyleOptions {
47
48
// Valid --date range
49
static final ZonedDateTime DATE_MIN = ZonedDateTime.parse("1980-01-01T00:00:02Z");
50
static final ZonedDateTime DATE_MAX = ZonedDateTime.parse("2099-12-31T23:59:59Z");
51
52
static class BadArgs extends Exception {
53
static final long serialVersionUID = 0L;
54
55
boolean showUsage;
56
57
BadArgs(String key, String arg) { super(Main.formatMsg(key, arg)); }
58
BadArgs(String key) { super(Main.getMsg(key)); }
59
60
BadArgs showUsage(boolean b) {
61
showUsage = b;
62
return this;
63
}
64
}
65
66
static Option[] recognizedOptions = {
67
// Main operations
68
new Option(false, OptionType.MAIN_OPERATION, "--create", "-c") {
69
void process(Main tool, String opt, String arg) throws BadArgs {
70
if (tool.iflag || tool.tflag || tool.uflag || tool.xflag || tool.dflag || tool.validate)
71
throw new BadArgs("error.multiple.main.operations").showUsage(true);
72
tool.cflag = true;
73
}
74
},
75
new Option(true, OptionType.MAIN_OPERATION, "--generate-index", "-i") {
76
void process(Main tool, String opt, String arg) throws BadArgs {
77
if (tool.cflag || tool.tflag || tool.uflag || tool.xflag || tool.dflag || tool.validate)
78
throw new BadArgs("error.multiple.main.operations").showUsage(true);
79
tool.iflag = true;
80
tool.rootjar = arg;
81
}
82
},
83
new Option(false, OptionType.MAIN_OPERATION, "--list", "-t") {
84
void process(Main tool, String opt, String arg) throws BadArgs {
85
if (tool.cflag || tool.iflag || tool.uflag || tool.xflag || tool.dflag || tool.validate)
86
throw new BadArgs("error.multiple.main.operations").showUsage(true);
87
tool.tflag = true;
88
}
89
},
90
new Option(false, OptionType.MAIN_OPERATION, "--update", "-u") {
91
void process(Main tool, String opt, String arg) throws BadArgs {
92
if (tool.cflag || tool.iflag || tool.tflag || tool.xflag || tool.dflag || tool.validate)
93
throw new BadArgs("error.multiple.main.operations").showUsage(true);
94
tool.uflag = true;
95
}
96
},
97
new Option(false, OptionType.MAIN_OPERATION, "--extract", "-x") {
98
void process(Main tool, String opt, String arg) throws BadArgs {
99
if (tool.cflag || tool.iflag || tool.tflag || tool.uflag || tool.dflag || tool.validate)
100
throw new BadArgs("error.multiple.main.operations").showUsage(true);
101
tool.xflag = true;
102
}
103
},
104
new Option(false, OptionType.MAIN_OPERATION, "--describe-module", "-d") {
105
void process(Main tool, String opt, String arg) throws BadArgs {
106
if (tool.cflag || tool.iflag || tool.tflag || tool.uflag || tool.xflag || tool.validate)
107
throw new BadArgs("error.multiple.main.operations").showUsage(true);
108
tool.dflag = true;
109
}
110
},
111
new Option(false, OptionType.MAIN_OPERATION, "--validate") {
112
void process(Main tool, String opt, String arg) throws BadArgs {
113
if (tool.cflag || tool.iflag || tool.tflag || tool.uflag || tool.xflag || tool.dflag)
114
throw new BadArgs("error.multiple.main.operations").showUsage(true);
115
tool.validate = true;
116
}
117
},
118
119
// Additional options
120
new Option(true, OptionType.ANY, "--file", "-f") {
121
void process(Main jartool, String opt, String arg) {
122
jartool.fname = arg;
123
}
124
},
125
new Option(false, OptionType.ANY, "--verbose", "-v") {
126
void process(Main jartool, String opt, String arg) {
127
jartool.vflag = true;
128
}
129
},
130
new Option(true, OptionType.CREATE_UPDATE, "--main-class", "-e") {
131
void process(Main jartool, String opt, String arg) {
132
jartool.ename = arg;
133
}
134
},
135
new Option(true, OptionType.CREATE_UPDATE, "--manifest", "-m") {
136
void process(Main jartool, String opt, String arg) {
137
jartool.mname = arg;
138
}
139
},
140
new Option(false, OptionType.CREATE_UPDATE, "--no-manifest", "-M") {
141
void process(Main jartool, String opt, String arg) {
142
jartool.Mflag = true;
143
}
144
},
145
new Option(true, OptionType.CREATE_UPDATE, "--module-version") {
146
void process(Main jartool, String opt, String arg) {
147
jartool.moduleVersion = Version.parse(arg);
148
}
149
},
150
new Option(true, OptionType.CREATE_UPDATE, "--hash-modules") {
151
void process(Main jartool, String opt, String arg) throws BadArgs {
152
try {
153
jartool.modulesToHash = Pattern.compile(arg);
154
} catch (PatternSyntaxException e) {
155
throw new BadArgs("err.badpattern", arg).showUsage(true);
156
}
157
}
158
},
159
new Option(true, OptionType.CREATE_UPDATE, "--module-path", "-p") {
160
void process(Main jartool, String opt, String arg) {
161
String[] dirs = arg.split(File.pathSeparator);
162
Path[] paths = new Path[dirs.length];
163
int i = 0;
164
for (String dir : dirs) {
165
paths[i++] = Paths.get(dir);
166
}
167
jartool.moduleFinder = ModulePath.of(Runtime.version(), true, paths);
168
}
169
},
170
new Option(false, OptionType.CREATE_UPDATE, "--do-not-resolve-by-default") {
171
void process(Main jartool, String opt, String arg) {
172
ModuleResolution mres = jartool.moduleResolution;
173
jartool.moduleResolution = mres.withDoNotResolveByDefault();
174
}
175
boolean isExtra() { return true; }
176
},
177
new Option(true, OptionType.CREATE_UPDATE, "--warn-if-resolved") {
178
void process(Main jartool, String opt, String arg) throws BadArgs {
179
ModuleResolution mres = ModuleResolution.empty();
180
if (jartool.moduleResolution.doNotResolveByDefault()) {
181
mres.withDoNotResolveByDefault();
182
}
183
if (arg.equals("deprecated")) {
184
jartool.moduleResolution = mres.withDeprecated();
185
} else if (arg.equals("deprecated-for-removal")) {
186
jartool.moduleResolution = mres.withDeprecatedForRemoval();
187
} else if (arg.equals("incubating")) {
188
jartool.moduleResolution = mres.withIncubating();
189
} else {
190
throw new BadArgs("error.bad.reason", arg);
191
}
192
}
193
boolean isExtra() { return true; }
194
},
195
new Option(false, OptionType.CREATE_UPDATE_INDEX, "--no-compress", "-0") {
196
void process(Main jartool, String opt, String arg) {
197
jartool.flag0 = true;
198
}
199
},
200
new Option(true, OptionType.CREATE_UPDATE_INDEX, "--date") {
201
void process(Main jartool, String opt, String arg) throws BadArgs {
202
try {
203
ZonedDateTime date = ZonedDateTime.parse(arg, DateTimeFormatter.ISO_ZONED_DATE_TIME)
204
.withZoneSameInstant(ZoneOffset.UTC);
205
if (date.isBefore(DATE_MIN) || date.isAfter(DATE_MAX)) {
206
throw new BadArgs("error.date.out.of.range", arg);
207
}
208
jartool.date = date.toLocalDateTime();
209
} catch (DateTimeParseException x) {
210
throw new BadArgs("error.date.notvalid", arg);
211
}
212
}
213
},
214
215
// Hidden options
216
new Option(false, OptionType.OTHER, "-P") {
217
void process(Main jartool, String opt, String arg) {
218
jartool.pflag = true;
219
}
220
boolean isHidden() { return true; }
221
},
222
223
// Other options
224
new Option(true, true, OptionType.OTHER, "--help", "-h", "-?") {
225
void process(Main jartool, String opt, String arg) throws BadArgs {
226
if (jartool.info == null) {
227
if (arg == null) {
228
jartool.info = GNUStyleOptions::printHelp; // Main.Info.HELP;
229
return;
230
}
231
if (!arg.equals("compat"))
232
throw new BadArgs("error.illegal.option", arg).showUsage(true);
233
// jartool.info = Main.Info.COMPAT_HELP;
234
jartool.info = GNUStyleOptions::printCompatHelp;
235
}
236
}
237
},
238
new Option(false, OptionType.OTHER, "--help-extra") {
239
void process(Main jartool, String opt, String arg) throws BadArgs {
240
jartool.info = GNUStyleOptions::printHelpExtra;
241
}
242
},
243
new Option(false, OptionType.OTHER, "--version") {
244
void process(Main jartool, String opt, String arg) {
245
if (jartool.info == null)
246
jartool.info = GNUStyleOptions::printVersion;
247
}
248
}
249
};
250
251
enum OptionType {
252
MAIN_OPERATION("main"),
253
ANY("any"),
254
CREATE("create"),
255
CREATE_UPDATE("create.update"),
256
CREATE_UPDATE_INDEX("create.update.index"),
257
OTHER("other");
258
259
/** Resource lookup section prefix. */
260
final String name;
261
262
OptionType(String name) { this.name = name; }
263
}
264
265
static abstract class Option {
266
final boolean hasArg;
267
final boolean argIsOptional;
268
final String[] aliases;
269
final OptionType type;
270
271
Option(boolean hasArg, OptionType type, String... aliases) {
272
this(hasArg, false, type, aliases);
273
}
274
275
Option(boolean hasArg, boolean argIsOptional, OptionType type, String... aliases) {
276
this.hasArg = hasArg;
277
this.argIsOptional = argIsOptional;
278
this.type = type;
279
this.aliases = aliases;
280
}
281
282
boolean isHidden() { return false; }
283
284
boolean isExtra() { return false; }
285
286
boolean matches(String opt) {
287
for (String a : aliases) {
288
if (a.equals(opt)) {
289
return true;
290
} else if (opt.startsWith("--") && hasArg && opt.startsWith(a + "=")) {
291
return true;
292
} else if (opt.startsWith("--help") && opt.startsWith(a + ":")) {
293
return true;
294
}
295
}
296
return false;
297
}
298
299
abstract void process(Main jartool, String opt, String arg) throws BadArgs;
300
}
301
302
static int parseOptions(Main jartool, String[] args) throws BadArgs {
303
int count = 0;
304
if (args.length == 0) {
305
jartool.info = GNUStyleOptions::printUsageTryHelp; // never be here
306
return 0;
307
}
308
309
// process options
310
for (; count < args.length; count++) {
311
if (args[count].charAt(0) != '-' || args[count].equals("-C") ||
312
args[count].equals("--release"))
313
break;
314
315
String name = args[count];
316
if (name.equals("-XDsuppress-tool-removal-message")) {
317
jartool.suppressDeprecateMsg = true;
318
continue;
319
}
320
Option option = getOption(name);
321
String param = null;
322
if (option.hasArg) {
323
if (name.startsWith("--help")) { // "special" optional separator
324
if (name.indexOf(':') > 0) {
325
param = name.substring(name.indexOf(':') + 1, name.length());
326
}
327
} else if (name.startsWith("--") && name.indexOf('=') > 0) {
328
param = name.substring(name.indexOf('=') + 1, name.length());
329
} else if (count + 1 < args.length) {
330
param = args[++count];
331
}
332
if (!option.argIsOptional &&
333
(param == null || param.isEmpty() || param.charAt(0) == '-')) {
334
throw new BadArgs("error.missing.arg", name).showUsage(true);
335
}
336
}
337
option.process(jartool, name, param);
338
}
339
340
return count;
341
}
342
343
private static Option getOption(String name) throws BadArgs {
344
for (Option o : recognizedOptions) {
345
if (o.matches(name)) {
346
return o;
347
}
348
}
349
throw new BadArgs("error.unrecognized.option", name).showUsage(true);
350
}
351
352
static void printHelpExtra(PrintWriter out) {
353
printHelp0(out, true);
354
}
355
356
static void printHelp(PrintWriter out) {
357
printHelp0(out, false);
358
}
359
360
private static void printHelp0(PrintWriter out, boolean printExtra) {
361
out.format("%s%n", Main.getMsg("main.help.preopt"));
362
for (OptionType type : OptionType.values()) {
363
boolean typeHeadingWritten = false;
364
365
for (Option o : recognizedOptions) {
366
if (!o.type.equals(type))
367
continue;
368
String name = o.aliases[0].substring(1); // there must always be at least one name
369
name = name.charAt(0) == '-' ? name.substring(1) : name;
370
if (o.isHidden() || name.equals("h")) {
371
continue;
372
}
373
if (o.isExtra() && !printExtra) {
374
continue;
375
}
376
if (!typeHeadingWritten) {
377
out.format("%n%s%n", Main.getMsg("main.help.opt." + type.name));
378
typeHeadingWritten = true;
379
}
380
out.format("%s%n", Main.getMsg("main.help.opt." + type.name + "." + name));
381
}
382
}
383
out.format("%n%s%n%n", Main.getMsg("main.help.postopt"));
384
}
385
386
static void printCompatHelp(PrintWriter out) {
387
out.format("%s%n", Main.getMsg("usage.compat"));
388
}
389
390
static void printUsageTryHelp(PrintWriter out) {
391
out.format("%s%n", Main.getMsg("main.usage.summary.try"));
392
}
393
394
static void printVersion(PrintWriter out) {
395
out.format("%s %s%n", "jar", System.getProperty("java.version"));
396
}
397
}
398
399