Skip to content
Permalink
Browse files

8224613: javadoc should better handle bad options

Reviewed-by: jjg
  • Loading branch information
pavelrappo committed May 7, 2020
1 parent c2780c9 commit 93b0516d5d6e88a7e93f3ac06e02c9771def753f
@@ -127,8 +127,10 @@ public static JavadocTool make0(Context context) {
}

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);
@@ -46,7 +46,7 @@
* 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 static void main(String... args) {
/**
* 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 static int execute(String... args) {
* 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 static int execute(String[] args, PrintWriter writer) {
*
* @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) {
@@ -495,7 +495,12 @@ private Result parseAndExecute(List<String> argList, Iterable<? extends JavaFile
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 @@ boolean matches(Doclet.Option option, String arg) {
}

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 @@ int handleDocletOption(int idx, List<String> args, boolean isToolOption)
argBase = arg;
argVal = null;
}
int m = 1;
String text = null;
for (Doclet.Option opt : docletOptions) {
if (matches(opt, argBase)) {
@@ -603,29 +614,33 @@ int handleDocletOption(int idx, List<String> args, boolean isToolOption)
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
if (!isToolOption) {
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 @@ int handleDocletOption(int idx, List<String> args, boolean isToolOption)
* @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 @@ private Doclet preprocess(List<String> argv)
}
}

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 @@ private void parseArgs(List<String> args, List<String> javaNames) throws ToolExc
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) {
@@ -696,7 +696,7 @@ boolean dumpOnError() {
}

void setDumpOnError(boolean v) {
dumpOnError = true;
dumpOnError = v;
}

/**
@@ -816,7 +816,7 @@ Options compilerOptions() {
/**
* 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) {

0 comments on commit 93b0516

Please sign in to comment.
You can’t perform that action at this time.