8243417: Clean up com.sun.tools.javac.main.CommandLine

Reviewed-by: prappo
This commit is contained in:
Jonathan Gibbons 2020-05-13 10:39:35 -07:00
parent 3d50f242c2
commit 49bfbd3bc7
6 changed files with 21 additions and 36 deletions
src
jdk.compiler/share/classes/com/sun/tools
jdk.javadoc/share/classes/jdk/javadoc/internal/tool
test/langtools/tools/javac/main

@ -183,14 +183,14 @@ public class Arguments {
* @param ownName the name of this tool; used to prefix messages * @param ownName the name of this tool; used to prefix messages
* @param args the args to be processed * @param args the args to be processed
*/ */
public void init(String ownName, String... args) { public void init(String ownName, Iterable<String> args) {
this.ownName = ownName; this.ownName = ownName;
errorMode = ErrorMode.LOG; errorMode = ErrorMode.LOG;
files = new LinkedHashSet<>(); files = new LinkedHashSet<>();
deferredFileManagerOptions = new LinkedHashMap<>(); deferredFileManagerOptions = new LinkedHashMap<>();
fileObjects = null; fileObjects = null;
classNames = new LinkedHashSet<>(); classNames = new LinkedHashSet<>();
processArgs(List.from(args), Option.getJavaCompilerOptions(), cmdLineHelper, true, false); processArgs(args, Option.getJavaCompilerOptions(), cmdLineHelper, true, false);
if (errors) { if (errors) {
log.printLines(PrefixKind.JAVAC, "msg.usage", ownName); log.printLines(PrefixKind.JAVAC, "msg.usage", ownName);
} }

@ -56,10 +56,10 @@ public class CommandLine {
* @return the arguments, with @files expanded * @return the arguments, with @files expanded
* @throws IOException if there is a problem reading any of the @files * @throws IOException if there is a problem reading any of the @files
*/ */
public static String[] parse(String[] args) throws IOException { public static List<String> parse(List<String> args) throws IOException {
List<String> newArgs = new ArrayList<>(); List<String> newArgs = new ArrayList<>();
appendParsedCommandArgs(newArgs, Arrays.asList(args)); appendParsedCommandArgs(newArgs, args);
return newArgs.toArray(new String[newArgs.size()]); return newArgs;
} }
private static void appendParsedCommandArgs(List<String> newArgs, List<String> args) throws IOException { private static void appendParsedCommandArgs(List<String> newArgs, List<String> args) throws IOException {
@ -104,27 +104,6 @@ public class CommandLine {
return newArgs; return newArgs;
} }
/**
* Process the given environment variable and appends any Win32-style
* command files for the specified command line arguments and return
* the resulting arguments. A command file argument
* is of the form '@file' where 'file' is the name of the file whose
* contents are to be parsed for additional arguments. The contents of
* the command file are parsed using StreamTokenizer and the original
* '@file' argument replaced with the resulting tokens. Recursive command
* files are not supported. The '@' character itself can be quoted with
* the sequence '@@'.
* @param envVariable the env variable to process
* @param args the arguments that may contain @files
* @return the arguments, with environment variable's content and expansion of @files
* @throws IOException if there is a problem reading any of the @files
* @throws com.sun.tools.javac.main.CommandLine.UnmatchedQuote
*/
public static String[] parse(String envVariable, String[] args) throws IOException, UnmatchedQuote {
List<String> out = parse(envVariable, Arrays.asList(args));
return out.toArray(new String[out.size()]);
}
private static void loadCmdFile(String name, List<String> args) throws IOException { private static void loadCmdFile(String name, List<String> args) throws IOException {
try (Reader r = Files.newBufferedReader(Paths.get(name), Charset.defaultCharset())) { try (Reader r = Files.newBufferedReader(Paths.get(name), Charset.defaultCharset())) {
Tokenizer t = new Tokenizer(r); Tokenizer t = new Tokenizer(r);

@ -217,8 +217,9 @@ public class Main {
} }
// prefix argv with contents of environment variable and expand @-files // prefix argv with contents of environment variable and expand @-files
Iterable<String> allArgs;
try { try {
argv = CommandLine.parse(ENV_OPT_NAME, argv); allArgs = CommandLine.parse(ENV_OPT_NAME, List.from(argv));
} catch (UnmatchedQuote ex) { } catch (UnmatchedQuote ex) {
reportDiag(Errors.UnmatchedQuote(ex.variableName)); reportDiag(Errors.UnmatchedQuote(ex.variableName));
return Result.CMDERR; return Result.CMDERR;
@ -232,7 +233,7 @@ public class Main {
} }
Arguments args = Arguments.instance(context); Arguments args = Arguments.instance(context);
args.init(ownName, argv); args.init(ownName, allArgs);
if (log.nerrors > 0) if (log.nerrors > 0)
return Result.CMDERR; return Result.CMDERR;

@ -117,12 +117,13 @@ public abstract class OptionHelper {
* @param args the arguments to traverse. * @param args the arguments to traverse.
*/ */
void traverse(String[] args) { void traverse(String[] args) {
Iterable<String> allArgs;
try { try {
args = CommandLine.parse(args); // Detect @file and load it as a command line. allArgs = CommandLine.parse(List.of(args)); // Detect @file and load it as a command line.
} catch (java.io.IOException e) { } catch (java.io.IOException e) {
throw new IllegalArgumentException("Problem reading @"+e.getMessage()); throw new IllegalArgumentException("Problem reading @"+e.getMessage());
} }
ArgumentIterator argIter = new ArgumentIterator(Arrays.asList(args)); ArgumentIterator argIter = new ArgumentIterator(allArgs);
nextArg: nextArg:
while (argIter.hasNext()) { while (argIter.hasNext()) {

@ -341,13 +341,14 @@ public class Start {
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
Result begin(String... argv) { Result begin(String... argv) {
// Preprocess @file arguments // Preprocess @file arguments
List<String> allArgs;
try { try {
argv = CommandLine.parse(argv); allArgs = CommandLine.parse(List.of(argv));
} catch (IOException e) { } catch (IOException e) {
error("main.cant.read", e.getMessage()); error("main.cant.read", e.getMessage());
return ERROR; return ERROR;
} }
return begin(Arrays.asList(argv), Collections.emptySet()); return begin(allArgs, Collections.emptySet());
} }
// Called by the JSR 199 API // Called by the JSR 199 API

@ -35,6 +35,9 @@ import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.PrintStream; import java.io.PrintStream;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.Arrays;
import java.util.stream.Collectors;
import java.util.List;
import toolbox.*; import toolbox.*;
@ -135,14 +138,14 @@ public class EnvVariableTest extends TestRunner {
* print the result. * print the result.
*/ */
public static class Tester { public static class Tester {
private static final String[] EMPTY_ARRAY = new String[0]; private static final List<String> EMPTY_LIST = List.of();
static String arrayToString(String... args) { static String arrayToString(String... args) {
return String.join(", ", args); return List.of(args).stream().collect(Collectors.joining(", "));
} }
public static void main(String... args) throws IOException { public static void main(String... args) throws IOException {
try { try {
String[] argv = CommandLine.parse("JDK_JAVAC_OPTIONS", EMPTY_ARRAY); List<String> argv = CommandLine.parse("JDK_JAVAC_OPTIONS", EMPTY_LIST);
System.out.print(arrayToString(argv)); System.out.print(argv.stream().collect(Collectors.joining(", ")));
} catch (CommandLine.UnmatchedQuote ex) { } catch (CommandLine.UnmatchedQuote ex) {
System.out.print("Exception: " + ex.variableName); System.out.print("Exception: " + ex.variableName);
} }