8224613: javadoc should better handle bad options

Reviewed-by: jjg
This commit is contained in:
Pavel Rappo 2020-05-07 13:59:18 +01:00
parent c2780c9556
commit 93b0516d5d
5 changed files with 383 additions and 23 deletions

View file

@ -127,8 +127,10 @@ public class JavadocTool extends com.sun.tools.javac.main.JavaCompiler {
}
public DocletEnvironment getEnvironment(ToolOptions toolOptions,
List<String> javaNames,
Iterable<? extends JavaFileObject> fileObjects) throws ToolException {
List<String> javaNames,
Iterable<? extends JavaFileObject> fileObjects)
throws ToolException
{
toolEnv = ToolEnvironment.instance(context);
toolEnv.initialize(toolOptions);
ElementsTable etable = new ElementsTable(context, toolOptions);

View file

@ -46,7 +46,7 @@ public class Main {
* The main entry point called by the launcher. This will call
* System.exit with an appropriate return value.
*
* @param args The command line parameters.
* @param args the command-line parameters
*/
public static void main(String... args) {
System.exit(execute(args));
@ -55,7 +55,7 @@ public class Main {
/**
* Programmatic interface.
*
* @param args The command line parameters.
* @param args the command-line parameters
* @return The return code.
*/
public static int execute(String... args) {
@ -67,7 +67,7 @@ public class Main {
* Programmatic interface.
*
* @param writer a stream for all output
* @param args The command line parameters.
* @param args the command-line parameters
* @return The return code.
*/
public static int execute(String[] args, PrintWriter writer) {
@ -80,7 +80,7 @@ public class Main {
*
* @param outWriter a stream for expected output
* @param errWriter a stream for diagnostic output
* @param args The command line parameters.
* @param args the command-line parameters
* @return The return code.
*/
public static int execute(String[] args, PrintWriter outWriter, PrintWriter errWriter) {

View file

@ -495,7 +495,12 @@ public class Start {
arguments.allowEmpty();
doclet.init(locale, messager);
parseArgs(argList, javaNames);
int beforeCount = messager.nerrors;
boolean success = parseArgs(argList, javaNames);
int afterCount = messager.nerrors;
if (!success && beforeCount == afterCount) { // if there were failures but they have not been reported
return CMDERR;
}
if (!arguments.handleReleaseOptions(extra -> true)) {
// Arguments does not always increase the error count in the
@ -579,8 +584,13 @@ public class Start {
}
private Set<? extends Doclet.Option> docletOptions = null;
int handleDocletOption(int idx, List<String> args, boolean isToolOption)
throws OptionException {
/*
* Consumes an option along with its arguments. Returns an advanced index
* modulo the sign. If the value is negative, it means there was a failure
* processing one or more options.
*/
int consumeDocletOption(int idx, List<String> args, boolean isToolOption) throws OptionException {
if (docletOptions == null) {
docletOptions = getSupportedOptionsOf(doclet);
}
@ -594,6 +604,7 @@ public class Start {
argBase = arg;
argVal = null;
}
int m = 1;
String text = null;
for (Doclet.Option opt : docletOptions) {
if (matches(opt, argBase)) {
@ -603,21 +614,25 @@ public class Start {
text = messager.getText("main.unnecessary_arg_provided", argBase);
throw new OptionException(ERROR, this::showUsage, text);
case 1:
opt.process(arg, Arrays.asList(argVal));
if (!opt.process(arg, Collections.singletonList(argVal))) {
m = -1;
}
break;
default:
text = messager.getText("main.only_one_argument_with_equals", argBase);
throw new OptionException(ERROR, this::showUsage, text);
}
} else {
if (args.size() - idx -1 < opt.getArgumentCount()) {
if (args.size() - idx - 1 < opt.getArgumentCount()) {
text = messager.getText("main.requires_argument", arg);
throw new OptionException(ERROR, this::showUsage, text);
}
opt.process(arg, args.subList(idx + 1, args.size()));
if (!opt.process(arg, args.subList(idx + 1, idx + 1 + opt.getArgumentCount()))) {
m = -1;
}
idx += opt.getArgumentCount();
}
return idx;
return m * idx;
}
}
// check if arg is accepted by the tool before emitting error
@ -625,7 +640,7 @@ public class Start {
text = messager.getText("main.invalid_flag", arg);
throw new OptionException(ERROR, this::showUsage, text);
}
return idx;
return m * idx;
}
private static Set<? extends Option> getSupportedOptionsOf(Doclet doclet) {
@ -649,8 +664,7 @@ public class Start {
* @throws ToolException if an error occurs initializing the doclet
* @throws OptionException if an error occurs while processing an option
*/
private Doclet preprocess(List<String> argv)
throws ToolException, OptionException {
private Doclet preprocess(List<String> argv) throws ToolException, OptionException {
// doclet specifying arguments
String userDocletPath = null;
String userDocletName = null;
@ -774,15 +788,19 @@ public class Start {
}
}
private void parseArgs(List<String> args, List<String> javaNames) throws ToolException,
OptionException, com.sun.tools.javac.main.Option.InvalidValueException {
private boolean parseArgs(List<String> args, List<String> javaNames)
throws OptionException, com.sun.tools.javac.main.Option.InvalidValueException
{
boolean success = true;
for (int i = 0; i < args.size(); i++) {
String arg = args.get(i);
ToolOption o = options.getOption(arg);
if (o != null) {
// handle a doclet argument that may be needed however
// don't increment the index, and allow the tool to consume args
handleDocletOption(i, args, true);
if (consumeDocletOption(i, args, true) < 0) {
success = false;
}
if (o.hasArg) {
if (arg.startsWith("--") && arg.contains("=")) {
o.process(arg.substring(arg.indexOf('=') + 1));
@ -800,14 +818,19 @@ public class Start {
String s = arg.substring("-XD".length());
int eq = s.indexOf('=');
String key = (eq < 0) ? s : s.substring(0, eq);
String value = (eq < 0) ? s : s.substring(eq+1);
String value = (eq < 0) ? s : s.substring(eq + 1);
options.compilerOptions().put(key, value);
} else if (arg.startsWith("-")) {
i = handleDocletOption(i, args, false);
i = consumeDocletOption(i, args, false);
if (i < 0) {
i = -i;
success = false;
}
} else {
javaNames.add(arg);
}
}
return success;
}
private <T> boolean isEmpty(Iterable<T> iter) {

View file

@ -696,7 +696,7 @@ public class ToolOptions {
}
void setDumpOnError(boolean v) {
dumpOnError = true;
dumpOnError = v;
}
/**
@ -816,7 +816,7 @@ public class ToolOptions {
/**
* Returns an {@code IllegalOptionValue} exception.
*
* @param arg the arghument to include in the detail message
* @param arg the argument to include in the detail message
* @return the exception
*/
private IllegalOptionValue illegalOptionValue(String arg) {