src/share/classes/com/sun/tools/javac/main/RecognizedOptions.java

changeset 11
b66d15dfd001
parent 1
9a66ca7c79fa
child 54
eaf608c64fec
equal deleted inserted replaced
10:508c01999047 11:b66d15dfd001
23 * have any questions. 23 * have any questions.
24 */ 24 */
25 25
26 package com.sun.tools.javac.main; 26 package com.sun.tools.javac.main;
27 27
28 import com.sun.tools.javac.code.Lint;
28 import com.sun.tools.javac.code.Source; 29 import com.sun.tools.javac.code.Source;
29 import com.sun.tools.javac.code.Type; 30 import com.sun.tools.javac.code.Type;
30 import com.sun.tools.javac.jvm.Target; 31 import com.sun.tools.javac.jvm.Target;
31 import com.sun.tools.javac.main.JavacOption.HiddenOption; 32 import com.sun.tools.javac.main.JavacOption.HiddenOption;
32 import com.sun.tools.javac.main.JavacOption.Option; 33 import com.sun.tools.javac.main.JavacOption.Option;
33 import com.sun.tools.javac.main.JavacOption.XOption; 34 import com.sun.tools.javac.main.JavacOption.XOption;
34 import com.sun.tools.javac.util.List;
35 import com.sun.tools.javac.util.ListBuffer; 35 import com.sun.tools.javac.util.ListBuffer;
36 import com.sun.tools.javac.util.Log;
37 import com.sun.tools.javac.util.Options; 36 import com.sun.tools.javac.util.Options;
38 import com.sun.tools.javac.processing.JavacProcessingEnvironment; 37 import com.sun.tools.javac.processing.JavacProcessingEnvironment;
39 import java.io.File; 38 import java.io.File;
40 import java.io.FileWriter; 39 import java.io.FileWriter;
41 import java.io.PrintWriter; 40 import java.io.PrintWriter;
41 import java.util.Arrays;
42 import java.util.Collection;
42 import java.util.EnumSet; 43 import java.util.EnumSet;
44 import java.util.LinkedHashSet;
43 import java.util.Set; 45 import java.util.Set;
44 import java.util.StringTokenizer; 46 import java.util.StringTokenizer;
45 import javax.lang.model.SourceVersion; 47 import javax.lang.model.SourceVersion;
46 48
47 import static com.sun.tools.javac.main.OptionName.*; 49 import static com.sun.tools.javac.main.OptionName.*;
132 XBOOTCLASSPATH, 134 XBOOTCLASSPATH,
133 EXTDIRS, 135 EXTDIRS,
134 DJAVA_EXT_DIRS, 136 DJAVA_EXT_DIRS,
135 ENDORSEDDIRS, 137 ENDORSEDDIRS,
136 DJAVA_ENDORSED_DIRS, 138 DJAVA_ENDORSED_DIRS,
137 PROC_CUSTOM, 139 PROC,
138 PROCESSOR, 140 PROCESSOR,
139 PROCESSORPATH, 141 PROCESSORPATH,
140 D, 142 D,
141 S, 143 S,
142 IMPLICIT, 144 IMPLICIT,
193 XLINT, 195 XLINT,
194 XLINT_CUSTOM, 196 XLINT_CUSTOM,
195 NOWARN, 197 NOWARN,
196 VERBOSE, 198 VERBOSE,
197 DEPRECATION, 199 DEPRECATION,
198 PROC_CUSTOM, 200 PROC,
199 PROCESSOR, 201 PROCESSOR,
200 IMPLICIT, 202 IMPLICIT,
201 SOURCE, 203 SOURCE,
202 TARGET, 204 TARGET,
203 // VERSION, 205 // VERSION,
243 options.append(option); 245 options.append(option);
244 return options.toArray(new Option[options.length()]); 246 return options.toArray(new Option[options.length()]);
245 } 247 }
246 248
247 /** 249 /**
248 * @param out the writer to use for diagnostic output 250 * Get all the recognized options.
251 * @param helper an {@code OptionHelper} to help when processing options
252 * @return an array of options
249 */ 253 */
250 public static Option[] getAll(final OptionHelper helper) { 254 public static Option[] getAll(final OptionHelper helper) {
251 return new Option[]{ 255 return new Option[] {
252 new Option(G, "opt.g"), 256 new Option(G, "opt.g"),
253 new Option(G_NONE, "opt.g.none") { 257 new Option(G_NONE, "opt.g.none") {
258 @Override
254 public boolean process(Options options, String option) { 259 public boolean process(Options options, String option) {
255 options.put("-g:", "none"); 260 options.put("-g:", "none");
256 return false; 261 return false;
257 } 262 }
258 }, 263 },
259 264
260 new Option(G_CUSTOM, "opt.g.lines.vars.source") { 265 new Option(G_CUSTOM, "opt.g.lines.vars.source",
261 public boolean matches(String s) { 266 Option.ChoiceKind.ANYOF, "lines", "vars", "source"),
262 return s.startsWith("-g:");
263 }
264 public boolean process(Options options, String option) {
265 String suboptions = option.substring(3);
266 options.put("-g:", suboptions);
267 // enter all the -g suboptions as "-g:suboption"
268 for (StringTokenizer t = new StringTokenizer(suboptions, ","); t.hasMoreTokens(); ) {
269 String tok = t.nextToken();
270 String opt = "-g:" + tok;
271 options.put(opt, opt);
272 }
273 return false;
274 }
275 },
276 267
277 new XOption(XLINT, "opt.Xlint"), 268 new XOption(XLINT, "opt.Xlint"),
278 new XOption(XLINT_CUSTOM, "opt.Xlint.suboptlist") { 269 new XOption(XLINT_CUSTOM, "opt.Xlint.suboptlist",
279 public boolean matches(String s) { 270 Option.ChoiceKind.ANYOF, getXLintChoices()),
280 return s.startsWith("-Xlint:");
281 }
282 public boolean process(Options options, String option) {
283 String suboptions = option.substring(7);
284 options.put("-Xlint:", suboptions);
285 // enter all the -Xlint suboptions as "-Xlint:suboption"
286 for (StringTokenizer t = new StringTokenizer(suboptions, ","); t.hasMoreTokens(); ) {
287 String tok = t.nextToken();
288 String opt = "-Xlint:" + tok;
289 options.put(opt, opt);
290 }
291 return false;
292 }
293 },
294 271
295 // -nowarn is retained for command-line backward compatibility 272 // -nowarn is retained for command-line backward compatibility
296 new Option(NOWARN, "opt.nowarn") { 273 new Option(NOWARN, "opt.nowarn") {
297 public boolean process(Options options, String option) { 274 @Override
298 options.put("-Xlint:none", option); 275 public boolean process(Options options, String option) {
299 return false; 276 options.put("-Xlint:none", option);
300 } 277 return false;
301 }, 278 }
279 },
302 280
303 new Option(VERBOSE, "opt.verbose"), 281 new Option(VERBOSE, "opt.verbose"),
304 282
305 // -deprecation is retained for command-line backward compatibility 283 // -deprecation is retained for command-line backward compatibility
306 new Option(DEPRECATION, "opt.deprecation") { 284 new Option(DEPRECATION, "opt.deprecation") {
307 public boolean process(Options options, String option) { 285 @Override
308 options.put("-Xlint:deprecation", option); 286 public boolean process(Options options, String option) {
309 return false; 287 options.put("-Xlint:deprecation", option);
310 } 288 return false;
311 }, 289 }
290 },
312 291
313 new Option(CLASSPATH, "opt.arg.path", "opt.classpath"), 292 new Option(CLASSPATH, "opt.arg.path", "opt.classpath"),
314 new Option(CP, "opt.arg.path", "opt.classpath") { 293 new Option(CP, "opt.arg.path", "opt.classpath") {
294 @Override
315 public boolean process(Options options, String option, String arg) { 295 public boolean process(Options options, String option, String arg) {
316 return super.process(options, "-classpath", arg); 296 return super.process(options, "-classpath", arg);
317 } 297 }
318 }, 298 },
319 new Option(SOURCEPATH, "opt.arg.path", "opt.sourcepath"), 299 new Option(SOURCEPATH, "opt.arg.path", "opt.sourcepath"),
320 new Option(BOOTCLASSPATH, "opt.arg.path", "opt.bootclasspath") { 300 new Option(BOOTCLASSPATH, "opt.arg.path", "opt.bootclasspath") {
301 @Override
321 public boolean process(Options options, String option, String arg) { 302 public boolean process(Options options, String option, String arg) {
322 options.remove("-Xbootclasspath/p:"); 303 options.remove("-Xbootclasspath/p:");
323 options.remove("-Xbootclasspath/a:"); 304 options.remove("-Xbootclasspath/a:");
324 return super.process(options, option, arg); 305 return super.process(options, option, arg);
325 } 306 }
326 }, 307 },
327 new XOption(XBOOTCLASSPATH_PREPEND,"opt.arg.path", "opt.Xbootclasspath.p"), 308 new XOption(XBOOTCLASSPATH_PREPEND,"opt.arg.path", "opt.Xbootclasspath.p"),
328 new XOption(XBOOTCLASSPATH_APPEND, "opt.arg.path", "opt.Xbootclasspath.a"), 309 new XOption(XBOOTCLASSPATH_APPEND, "opt.arg.path", "opt.Xbootclasspath.a"),
329 new XOption(XBOOTCLASSPATH, "opt.arg.path", "opt.bootclasspath") { 310 new XOption(XBOOTCLASSPATH, "opt.arg.path", "opt.bootclasspath") {
311 @Override
330 public boolean process(Options options, String option, String arg) { 312 public boolean process(Options options, String option, String arg) {
331 options.remove("-Xbootclasspath/p:"); 313 options.remove("-Xbootclasspath/p:");
332 options.remove("-Xbootclasspath/a:"); 314 options.remove("-Xbootclasspath/a:");
333 return super.process(options, "-bootclasspath", arg); 315 return super.process(options, "-bootclasspath", arg);
334 } 316 }
335 }, 317 },
336 new Option(EXTDIRS, "opt.arg.dirs", "opt.extdirs"), 318 new Option(EXTDIRS, "opt.arg.dirs", "opt.extdirs"),
337 new XOption(DJAVA_EXT_DIRS, "opt.arg.dirs", "opt.extdirs") { 319 new XOption(DJAVA_EXT_DIRS, "opt.arg.dirs", "opt.extdirs") {
320 @Override
338 public boolean process(Options options, String option, String arg) { 321 public boolean process(Options options, String option, String arg) {
339 return super.process(options, "-extdirs", arg); 322 return super.process(options, "-extdirs", arg);
340 } 323 }
341 }, 324 },
342 new Option(ENDORSEDDIRS, "opt.arg.dirs", "opt.endorseddirs"), 325 new Option(ENDORSEDDIRS, "opt.arg.dirs", "opt.endorseddirs"),
343 new XOption(DJAVA_ENDORSED_DIRS, "opt.arg.dirs", "opt.endorseddirs") { 326 new XOption(DJAVA_ENDORSED_DIRS, "opt.arg.dirs", "opt.endorseddirs") {
327 @Override
344 public boolean process(Options options, String option, String arg) { 328 public boolean process(Options options, String option, String arg) {
345 return super.process(options, "-endorseddirs", arg); 329 return super.process(options, "-endorseddirs", arg);
346 } 330 }
347 }, 331 },
348 new Option(PROC_CUSTOM, "opt.proc.none.only") { 332 new Option(PROC, "opt.proc.none.only",
349 public boolean matches(String s) { 333 Option.ChoiceKind.ONEOF, "none", "only"),
350 return s.equals("-proc:none") || s.equals("-proc:only");
351 }
352
353 public boolean process(Options options, String option) {
354 if (option.equals("-proc:none")) {
355 options.remove("-proc:only");
356 } else {
357 options.remove("-proc:none");
358 }
359 options.put(option, option);
360 return false;
361 }
362 },
363 new Option(PROCESSOR, "opt.arg.class.list", "opt.processor"), 334 new Option(PROCESSOR, "opt.arg.class.list", "opt.processor"),
364 new Option(PROCESSORPATH, "opt.arg.path", "opt.processorpath"), 335 new Option(PROCESSORPATH, "opt.arg.path", "opt.processorpath"),
365 new Option(D, "opt.arg.directory", "opt.d"), 336 new Option(D, "opt.arg.directory", "opt.d"),
366 new Option(S, "opt.arg.directory", "opt.sourceDest"), 337 new Option(S, "opt.arg.directory", "opt.sourceDest"),
367 new Option(IMPLICIT, "opt.implicit") { 338 new Option(IMPLICIT, "opt.implicit",
368 public boolean matches(String s) { 339 Option.ChoiceKind.ONEOF, "none", "class"),
369 return s.equals("-implicit:none") || s.equals("-implicit:class");
370 }
371 public boolean process(Options options, String option, String operand) {
372 int sep = option.indexOf(":");
373 options.put(option.substring(0, sep), option.substring(sep+1));
374 options.put(option,option);
375 return false;
376 }
377 },
378 new Option(ENCODING, "opt.arg.encoding", "opt.encoding"), 340 new Option(ENCODING, "opt.arg.encoding", "opt.encoding"),
379 new Option(SOURCE, "opt.arg.release", "opt.source") { 341 new Option(SOURCE, "opt.arg.release", "opt.source") {
342 @Override
380 public boolean process(Options options, String option, String operand) { 343 public boolean process(Options options, String option, String operand) {
381 Source source = Source.lookup(operand); 344 Source source = Source.lookup(operand);
382 if (source == null) { 345 if (source == null) {
383 helper.error("err.invalid.source", operand); 346 helper.error("err.invalid.source", operand);
384 return true; 347 return true;
385 } 348 }
386 return super.process(options, option, operand); 349 return super.process(options, option, operand);
387 } 350 }
388 }, 351 },
389 new Option(TARGET, "opt.arg.release", "opt.target") { 352 new Option(TARGET, "opt.arg.release", "opt.target") {
353 @Override
390 public boolean process(Options options, String option, String operand) { 354 public boolean process(Options options, String option, String operand) {
391 Target target = Target.lookup(operand); 355 Target target = Target.lookup(operand);
392 if (target == null) { 356 if (target == null) {
393 helper.error("err.invalid.target", operand); 357 helper.error("err.invalid.target", operand);
394 return true; 358 return true;
395 } 359 }
396 return super.process(options, option, operand); 360 return super.process(options, option, operand);
397 } 361 }
398 }, 362 },
399 new Option(VERSION, "opt.version") { 363 new Option(VERSION, "opt.version") {
364 @Override
400 public boolean process(Options options, String option) { 365 public boolean process(Options options, String option) {
401 helper.printVersion(); 366 helper.printVersion();
402 return super.process(options, option); 367 return super.process(options, option);
403 } 368 }
404 }, 369 },
405 new HiddenOption(FULLVERSION) { 370 new HiddenOption(FULLVERSION) {
371 @Override
406 public boolean process(Options options, String option) { 372 public boolean process(Options options, String option) {
407 helper.printFullVersion(); 373 helper.printFullVersion();
408 return super.process(options, option); 374 return super.process(options, option);
409 } 375 }
410 }, 376 },
411 new Option(HELP, "opt.help") { 377 new Option(HELP, "opt.help") {
378 @Override
412 public boolean process(Options options, String option) { 379 public boolean process(Options options, String option) {
413 helper.printHelp(); 380 helper.printHelp();
414 return super.process(options, option); 381 return super.process(options, option);
415 } 382 }
416 }, 383 },
417 new Option(A, "opt.arg.key.equals.value","opt.A") { 384 new Option(A, "opt.arg.key.equals.value","opt.A") {
418 String helpSynopsis() { 385 @Override
419 hasSuffix = true; 386 String helpSynopsis() {
420 return super.helpSynopsis(); 387 hasSuffix = true;
388 return super.helpSynopsis();
389 }
390
391 @Override
392 public boolean matches(String arg) {
393 return arg.startsWith("-A");
394 }
395
396 @Override
397 public boolean hasArg() {
398 return false;
399 }
400 // Mapping for processor options created in
401 // JavacProcessingEnvironment
402 @Override
403 public boolean process(Options options, String option) {
404 int argLength = option.length();
405 if (argLength == 2) {
406 helper.error("err.empty.A.argument");
407 return true;
421 } 408 }
422 409 int sepIndex = option.indexOf('=');
423 public boolean matches(String arg) { 410 String key = option.substring(2, (sepIndex != -1 ? sepIndex : argLength) );
424 return arg.startsWith("-A"); 411 if (!JavacProcessingEnvironment.isValidOptionName(key)) {
412 helper.error("err.invalid.A.key", option);
413 return true;
425 } 414 }
426 415 return process(options, option, option);
427 public boolean hasArg() { 416 }
428 return false;
429 }
430 // Mapping for processor options created in
431 // JavacProcessingEnvironment
432 public boolean process(Options options, String option) {
433 int argLength = option.length();
434 if (argLength == 2) {
435 helper.error("err.empty.A.argument");
436 return true;
437 }
438 int sepIndex = option.indexOf('=');
439 String key = option.substring(2, (sepIndex != -1 ? sepIndex : argLength) );
440 if (!JavacProcessingEnvironment.isValidOptionName(key)) {
441 helper.error("err.invalid.A.key", option);
442 return true;
443 }
444 return process(options, option, option);
445 }
446 }, 417 },
447 new Option(X, "opt.X") { 418 new Option(X, "opt.X") {
419 @Override
448 public boolean process(Options options, String option) { 420 public boolean process(Options options, String option) {
449 helper.printXhelp(); 421 helper.printXhelp();
450 return super.process(options, option); 422 return super.process(options, option);
451 } 423 }
452 }, 424 },
453 425
454 // This option exists only for the purpose of documenting itself. 426 // This option exists only for the purpose of documenting itself.
455 // It's actually implemented by the launcher. 427 // It's actually implemented by the launcher.
456 new Option(J, "opt.arg.flag", "opt.J") { 428 new Option(J, "opt.arg.flag", "opt.J") {
429 @Override
457 String helpSynopsis() { 430 String helpSynopsis() {
458 hasSuffix = true; 431 hasSuffix = true;
459 return super.helpSynopsis(); 432 return super.helpSynopsis();
460 } 433 }
434 @Override
461 public boolean process(Options options, String option) { 435 public boolean process(Options options, String option) {
462 throw new AssertionError 436 throw new AssertionError
463 ("the -J flag should be caught by the launcher."); 437 ("the -J flag should be caught by the launcher.");
464 } 438 }
465 }, 439 },
467 // stop after parsing and attributing. 441 // stop after parsing and attributing.
468 // new HiddenOption("-attrparseonly"), 442 // new HiddenOption("-attrparseonly"),
469 443
470 // new Option("-moreinfo", "opt.moreinfo") { 444 // new Option("-moreinfo", "opt.moreinfo") {
471 new HiddenOption(MOREINFO) { 445 new HiddenOption(MOREINFO) {
446 @Override
472 public boolean process(Options options, String option) { 447 public boolean process(Options options, String option) {
473 Type.moreInfo = true; 448 Type.moreInfo = true;
474 return super.process(options, option); 449 return super.process(options, option);
475 } 450 }
476 }, 451 },
510 // new Option("-scramble", "opt.scramble"), 485 // new Option("-scramble", "opt.scramble"),
511 // new Option("-scrambleall", "opt.scrambleall"), 486 // new Option("-scrambleall", "opt.scrambleall"),
512 487
513 // display warnings for generic unchecked operations 488 // display warnings for generic unchecked operations
514 new HiddenOption(WARNUNCHECKED) { 489 new HiddenOption(WARNUNCHECKED) {
490 @Override
515 public boolean process(Options options, String option) { 491 public boolean process(Options options, String option) {
516 options.put("-Xlint:unchecked", option); 492 options.put("-Xlint:unchecked", option);
517 return false; 493 return false;
518 } 494 }
519 }, 495 },
520 496
521 new XOption(XMAXERRS, "opt.arg.number", "opt.maxerrs"), 497 new XOption(XMAXERRS, "opt.arg.number", "opt.maxerrs"),
522 new XOption(XMAXWARNS, "opt.arg.number", "opt.maxwarns"), 498 new XOption(XMAXWARNS, "opt.arg.number", "opt.maxwarns"),
523 new XOption(XSTDOUT, "opt.arg.file", "opt.Xstdout") { 499 new XOption(XSTDOUT, "opt.arg.file", "opt.Xstdout") {
500 @Override
524 public boolean process(Options options, String option, String arg) { 501 public boolean process(Options options, String option, String arg) {
525 try { 502 try {
526 helper.setOut(new PrintWriter(new FileWriter(arg), true)); 503 helper.setOut(new PrintWriter(new FileWriter(arg), true));
527 } catch (java.io.IOException e) { 504 } catch (java.io.IOException e) {
528 helper.error("err.error.writing.file", arg, e); 505 helper.error("err.error.writing.file", arg, e);
536 513
537 new XOption(XPRINTROUNDS, "opt.printRounds"), 514 new XOption(XPRINTROUNDS, "opt.printRounds"),
538 515
539 new XOption(XPRINTPROCESSORINFO, "opt.printProcessorInfo"), 516 new XOption(XPRINTPROCESSORINFO, "opt.printProcessorInfo"),
540 517
541 new XOption(XPREFER, "opt.prefer") { 518 new XOption(XPREFER, "opt.prefer",
542 public boolean matches(String s) { 519 Option.ChoiceKind.ONEOF, "source", "newer"),
543 return s.equals("-Xprefer:source") || s.equals("-Xprefer:newer");
544 }
545 public boolean process(Options options, String option, String operand) {
546 int sep = option.indexOf(":");
547 options.put(option.substring(0, sep), option.substring(sep+1));
548 options.put(option,option);
549 return false;
550 }
551 },
552 520
553 /* -O is a no-op, accepted for backward compatibility. */ 521 /* -O is a no-op, accepted for backward compatibility. */
554 new HiddenOption(O), 522 new HiddenOption(O),
555 523
556 /* -Xjcov produces tables to support the code coverage tool jcov. */ 524 /* -Xjcov produces tables to support the code coverage tool jcov. */
560 * -XDx=y sets the option x to the value y. 528 * -XDx=y sets the option x to the value y.
561 * -XDx sets the option x to the value x. 529 * -XDx sets the option x to the value x.
562 */ 530 */
563 new HiddenOption(XD) { 531 new HiddenOption(XD) {
564 String s; 532 String s;
533 @Override
565 public boolean matches(String s) { 534 public boolean matches(String s) {
566 this.s = s; 535 this.s = s;
567 return s.startsWith(name.optionName); 536 return s.startsWith(name.optionName);
568 } 537 }
538 @Override
569 public boolean process(Options options, String option) { 539 public boolean process(Options options, String option) {
570 s = s.substring(name.optionName.length()); 540 s = s.substring(name.optionName.length());
571 int eq = s.indexOf('='); 541 int eq = s.indexOf('=');
572 String key = (eq < 0) ? s : s.substring(0, eq); 542 String key = (eq < 0) ? s : s.substring(0, eq);
573 String value = (eq < 0) ? s : s.substring(eq+1); 543 String value = (eq < 0) ? s : s.substring(eq+1);
584 * In apt, the process method adds the candiate class file 554 * In apt, the process method adds the candiate class file
585 * name to a separate list. 555 * name to a separate list.
586 */ 556 */
587 new HiddenOption(SOURCEFILE) { 557 new HiddenOption(SOURCEFILE) {
588 String s; 558 String s;
559 @Override
589 public boolean matches(String s) { 560 public boolean matches(String s) {
590 this.s = s; 561 this.s = s;
591 return s.endsWith(".java") // Java source file 562 return s.endsWith(".java") // Java source file
592 || SourceVersion.isName(s); // Legal type name 563 || SourceVersion.isName(s); // Legal type name
593 } 564 }
565 @Override
594 public boolean process(Options options, String option) { 566 public boolean process(Options options, String option) {
595 if (s.endsWith(".java") ) { 567 if (s.endsWith(".java") ) {
596 File f = new File(s); 568 File f = new File(s);
597 if (!f.exists()) { 569 if (!f.exists()) {
598 helper.error("err.file.not.found", f); 570 helper.error("err.file.not.found", f);
610 } 582 }
611 }, 583 },
612 }; 584 };
613 } 585 }
614 586
587 private static Collection<String> getXLintChoices() {
588 Collection<String> choices = new LinkedHashSet<String>();
589 choices.add("all");
590 for (Lint.LintCategory c : Lint.LintCategory.values())
591 choices.add(c.option);
592 for (Lint.LintCategory c : Lint.LintCategory.values())
593 choices.add("-" + c.option);
594 choices.add("none");
595 return choices;
596 }
597
615 } 598 }

mercurial