Skip to content
This repository has been archived by the owner on Aug 27, 2022. It is now read-only.

Commit

Permalink
8243417: Clean up com.sun.tools.javac.main.CommandLine
Browse files Browse the repository at this point in the history
Reviewed-by: prappo
  • Loading branch information
jonathan-gibbons committed May 13, 2020
1 parent 3d50f24 commit 49bfbd3
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 36 deletions.
Expand Up @@ -183,14 +183,14 @@ public void addClassName(String s) {
* @param ownName the name of this tool; used to prefix messages
* @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;
errorMode = ErrorMode.LOG;
files = new LinkedHashSet<>();
deferredFileManagerOptions = new LinkedHashMap<>();
fileObjects = null;
classNames = new LinkedHashSet<>();
processArgs(List.from(args), Option.getJavaCompilerOptions(), cmdLineHelper, true, false);
processArgs(args, Option.getJavaCompilerOptions(), cmdLineHelper, true, false);
if (errors) {
log.printLines(PrefixKind.JAVAC, "msg.usage", ownName);
}
Expand Down
Expand Up @@ -56,10 +56,10 @@ public class CommandLine {
* @return the arguments, with @files expanded
* @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<>();
appendParsedCommandArgs(newArgs, Arrays.asList(args));
return newArgs.toArray(new String[newArgs.size()]);
appendParsedCommandArgs(newArgs, args);
return newArgs;
}

private static void appendParsedCommandArgs(List<String> newArgs, List<String> args) throws IOException {
Expand Down Expand Up @@ -104,27 +104,6 @@ public static List<String> parse(String envVariable, List<String> args)
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 {
try (Reader r = Files.newBufferedReader(Paths.get(name), Charset.defaultCharset())) {
Tokenizer t = new Tokenizer(r);
Expand Down
Expand Up @@ -217,8 +217,9 @@ public void put(String name, String value) { }
}

// prefix argv with contents of environment variable and expand @-files
Iterable<String> allArgs;
try {
argv = CommandLine.parse(ENV_OPT_NAME, argv);
allArgs = CommandLine.parse(ENV_OPT_NAME, List.from(argv));
} catch (UnmatchedQuote ex) {
reportDiag(Errors.UnmatchedQuote(ex.variableName));
return Result.CMDERR;
Expand All @@ -232,7 +233,7 @@ public void put(String name, String value) { }
}

Arguments args = Arguments.instance(context);
args.init(ownName, argv);
args.init(ownName, allArgs);

if (log.nerrors > 0)
return Result.CMDERR;
Expand Down
Expand Up @@ -117,12 +117,13 @@ public abstract class OptionHelper {
* @param args the arguments to traverse.
*/
void traverse(String[] args) {
Iterable<String> allArgs;
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) {
throw new IllegalArgumentException("Problem reading @"+e.getMessage());
}
ArgumentIterator argIter = new ArgumentIterator(Arrays.asList(args));
ArgumentIterator argIter = new ArgumentIterator(allArgs);

nextArg:
while (argIter.hasNext()) {
Expand Down
Expand Up @@ -341,13 +341,14 @@ void showOption(List<String> names, String parameters, String description) {
@SuppressWarnings("deprecation")
Result begin(String... argv) {
// Preprocess @file arguments
List<String> allArgs;
try {
argv = CommandLine.parse(argv);
allArgs = CommandLine.parse(List.of(argv));
} catch (IOException e) {
error("main.cant.read", e.getMessage());
return ERROR;
}
return begin(Arrays.asList(argv), Collections.emptySet());
return begin(allArgs, Collections.emptySet());
}

// Called by the JSR 199 API
Expand Down
11 changes: 7 additions & 4 deletions test/langtools/tools/javac/main/EnvVariableTest.java
Expand Up @@ -35,6 +35,9 @@
import java.io.IOException;
import java.io.PrintStream;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.stream.Collectors;
import java.util.List;

import toolbox.*;

Expand Down Expand Up @@ -135,14 +138,14 @@ void test(String full, String... expectedArgs) throws Exception {
* print the result.
*/
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) {
return String.join(", ", args);
return List.of(args).stream().collect(Collectors.joining(", "));
}
public static void main(String... args) throws IOException {
try {
String[] argv = CommandLine.parse("JDK_JAVAC_OPTIONS", EMPTY_ARRAY);
System.out.print(arrayToString(argv));
List<String> argv = CommandLine.parse("JDK_JAVAC_OPTIONS", EMPTY_LIST);
System.out.print(argv.stream().collect(Collectors.joining(", ")));
} catch (CommandLine.UnmatchedQuote ex) {
System.out.print("Exception: " + ex.variableName);
}
Expand Down

0 comments on commit 49bfbd3

Please sign in to comment.