8243417: Clean up com.sun.tools.javac.main.CommandLine
Reviewed-by: prappo
This commit is contained in:
parent
3d50f242c2
commit
49bfbd3bc7
@ -183,14 +183,14 @@ public class Arguments {
|
||||
* @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);
|
||||
}
|
||||
|
@ -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 {
|
||||
@ -104,27 +104,6 @@ public class CommandLine {
|
||||
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);
|
||||
|
@ -217,8 +217,9 @@ public class Main {
|
||||
}
|
||||
|
||||
// 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;
|
||||
@ -232,7 +233,7 @@ public class Main {
|
||||
}
|
||||
|
||||
Arguments args = Arguments.instance(context);
|
||||
args.init(ownName, argv);
|
||||
args.init(ownName, allArgs);
|
||||
|
||||
if (log.nerrors > 0)
|
||||
return Result.CMDERR;
|
||||
|
@ -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()) {
|
||||
|
@ -341,13 +341,14 @@ public class Start {
|
||||
@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
|
||||
|
@ -35,6 +35,9 @@ import java.io.ByteArrayOutputStream;
|
||||
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.*;
|
||||
|
||||
@ -135,14 +138,14 @@ public class EnvVariableTest extends TestRunner {
|
||||
* 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);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user