author | epavlova |
Wed, 09 Oct 2019 10:59:45 -0700 | |
changeset 58523 | fb3d408c7a7e |
parent 50908 | 7c51db95ccb6 |
permissions | -rw-r--r-- |
50908 | 1 |
/* |
2 |
* Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved. |
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
22 |
*/ |
|
23 |
package com.oracle.mxtool.junit; |
|
24 |
||
25 |
import java.io.BufferedReader; |
|
26 |
import java.io.File; |
|
27 |
import java.io.FileNotFoundException; |
|
28 |
import java.io.FileOutputStream; |
|
29 |
import java.io.FileReader; |
|
30 |
import java.io.IOException; |
|
31 |
import java.io.PrintStream; |
|
32 |
import java.util.ArrayList; |
|
33 |
import java.util.Collections; |
|
34 |
import java.util.List; |
|
35 |
import java.util.Map; |
|
36 |
import java.util.ServiceLoader; |
|
37 |
import java.util.Set; |
|
58523
fb3d408c7a7e
8231903: [Graal] Update com.oracle.mxtool.junit classes
epavlova
parents:
50908
diff
changeset
|
38 |
import java.util.TreeSet; |
50908 | 39 |
|
40 |
import org.junit.internal.JUnitSystem; |
|
41 |
import org.junit.internal.RealSystem; |
|
42 |
import org.junit.runner.Description; |
|
43 |
import org.junit.runner.JUnitCore; |
|
44 |
import org.junit.runner.Request; |
|
45 |
import org.junit.runner.Result; |
|
46 |
import org.junit.runner.Runner; |
|
47 |
import org.junit.runner.notification.Failure; |
|
48 |
import org.junit.runner.notification.RunListener; |
|
49 |
import org.junit.runner.notification.RunNotifier; |
|
50 |
import org.junit.runners.ParentRunner; |
|
51 |
import org.junit.runners.model.RunnerScheduler; |
|
52 |
||
53 |
import junit.runner.Version; |
|
54 |
||
55 |
public class MxJUnitWrapper { |
|
56 |
||
58523
fb3d408c7a7e
8231903: [Graal] Update com.oracle.mxtool.junit classes
epavlova
parents:
50908
diff
changeset
|
57 |
// Unit tests that start a JVM subprocess can use these system properties to |
fb3d408c7a7e
8231903: [Graal] Update com.oracle.mxtool.junit classes
epavlova
parents:
50908
diff
changeset
|
58 |
// add --add-exports and --add-opens as necessary to the JVM command line. |
fb3d408c7a7e
8231903: [Graal] Update com.oracle.mxtool.junit classes
epavlova
parents:
50908
diff
changeset
|
59 |
// |
fb3d408c7a7e
8231903: [Graal] Update com.oracle.mxtool.junit classes
epavlova
parents:
50908
diff
changeset
|
60 |
// Known usages: |
fb3d408c7a7e
8231903: [Graal] Update com.oracle.mxtool.junit classes
epavlova
parents:
50908
diff
changeset
|
61 |
// org.graalvm.compiler.test.SubprocessUtil.getPackageOpeningOptions() |
fb3d408c7a7e
8231903: [Graal] Update com.oracle.mxtool.junit classes
epavlova
parents:
50908
diff
changeset
|
62 |
public static final String OPENED_PACKAGES_PROPERTY_NAME = "com.oracle.mxtool.junit.opens"; |
fb3d408c7a7e
8231903: [Graal] Update com.oracle.mxtool.junit classes
epavlova
parents:
50908
diff
changeset
|
63 |
public static final String EXPORTED_PACKAGES_PROPERTY_NAME = "com.oracle.mxtool.junit.exports"; |
fb3d408c7a7e
8231903: [Graal] Update com.oracle.mxtool.junit classes
epavlova
parents:
50908
diff
changeset
|
64 |
|
50908 | 65 |
public static class MxJUnitConfig { |
66 |
||
67 |
public boolean verbose = false; |
|
68 |
public boolean veryVerbose = false; |
|
69 |
public boolean enableTiming = false; |
|
70 |
public boolean failFast = false; |
|
71 |
public boolean color = false; |
|
72 |
public boolean eagerStackTrace = false; |
|
73 |
public boolean gcAfterTest = false; |
|
74 |
public boolean recordResults = false; |
|
75 |
public int repeatCount = 1; |
|
76 |
} |
|
77 |
||
78 |
private static class RepeatingRunner extends Runner { |
|
79 |
||
80 |
private final Runner parent; |
|
81 |
private int repeat; |
|
82 |
||
83 |
RepeatingRunner(Runner parent, int repeat) { |
|
84 |
this.parent = parent; |
|
85 |
this.repeat = repeat; |
|
86 |
} |
|
87 |
||
88 |
@Override |
|
89 |
public Description getDescription() { |
|
90 |
return parent.getDescription(); |
|
91 |
} |
|
92 |
||
93 |
@Override |
|
94 |
public void run(RunNotifier notifier) { |
|
95 |
for (int i = 0; i < repeat; i++) { |
|
96 |
parent.run(notifier); |
|
97 |
} |
|
98 |
} |
|
99 |
||
100 |
@Override |
|
101 |
public int testCount() { |
|
102 |
return super.testCount() * repeat; |
|
103 |
} |
|
104 |
} |
|
105 |
||
106 |
private static class RepeatingRequest extends Request { |
|
107 |
||
108 |
private final Request request; |
|
109 |
private final int repeat; |
|
110 |
||
111 |
RepeatingRequest(Request request, int repeat) { |
|
112 |
this.request = request; |
|
113 |
this.repeat = repeat; |
|
114 |
} |
|
115 |
||
116 |
@Override |
|
117 |
public Runner getRunner() { |
|
118 |
return new RepeatingRunner(request.getRunner(), repeat); |
|
119 |
} |
|
120 |
} |
|
121 |
||
122 |
/** |
|
123 |
* Run the tests contained in the classes named in the <code>args</code>. A single test method |
|
124 |
* can be specified by adding #method after the class name. Only a single test can be run in |
|
125 |
* this way. If all tests run successfully, exit with a status of 0. Otherwise exit with a |
|
126 |
* status of 1. Write feedback while tests are running and write stack traces for all failed |
|
127 |
* tests after the tests all complete. |
|
128 |
* |
|
129 |
* @param args names of classes in which to find tests to run |
|
130 |
*/ |
|
131 |
public static void main(String... args) { |
|
132 |
JUnitSystem system = new RealSystem(); |
|
133 |
JUnitCore junitCore = new JUnitCore(); |
|
134 |
system.out().println("MxJUnitCore"); |
|
135 |
system.out().println("JUnit version " + Version.id()); |
|
136 |
||
137 |
MxJUnitRequest.Builder builder = new MxJUnitRequest.Builder(); |
|
138 |
MxJUnitConfig config = new MxJUnitConfig(); |
|
139 |
||
140 |
String[] expandedArgs = expandArgs(args); |
|
141 |
int i = 0; |
|
58523
fb3d408c7a7e
8231903: [Graal] Update com.oracle.mxtool.junit classes
epavlova
parents:
50908
diff
changeset
|
142 |
List<String> testSpecs = new ArrayList<>(); |
fb3d408c7a7e
8231903: [Graal] Update com.oracle.mxtool.junit classes
epavlova
parents:
50908
diff
changeset
|
143 |
List<String> openPackagesSpecs = new ArrayList<>(); |
50908 | 144 |
while (i < expandedArgs.length) { |
145 |
String each = expandedArgs[i]; |
|
146 |
if (each.charAt(0) == '-') { |
|
147 |
// command line arguments |
|
148 |
if (each.contentEquals("-JUnitVerbose")) { |
|
149 |
config.verbose = true; |
|
58523
fb3d408c7a7e
8231903: [Graal] Update com.oracle.mxtool.junit classes
epavlova
parents:
50908
diff
changeset
|
150 |
config.enableTiming = true; |
fb3d408c7a7e
8231903: [Graal] Update com.oracle.mxtool.junit classes
epavlova
parents:
50908
diff
changeset
|
151 |
} else if (each.contentEquals("-JUnitOpenPackages")) { |
fb3d408c7a7e
8231903: [Graal] Update com.oracle.mxtool.junit classes
epavlova
parents:
50908
diff
changeset
|
152 |
if (i + 1 >= expandedArgs.length) { |
fb3d408c7a7e
8231903: [Graal] Update com.oracle.mxtool.junit classes
epavlova
parents:
50908
diff
changeset
|
153 |
system.out().println("Must include argument for -JUnitAddExports"); |
fb3d408c7a7e
8231903: [Graal] Update com.oracle.mxtool.junit classes
epavlova
parents:
50908
diff
changeset
|
154 |
System.exit(1); |
fb3d408c7a7e
8231903: [Graal] Update com.oracle.mxtool.junit classes
epavlova
parents:
50908
diff
changeset
|
155 |
} |
fb3d408c7a7e
8231903: [Graal] Update com.oracle.mxtool.junit classes
epavlova
parents:
50908
diff
changeset
|
156 |
openPackagesSpecs.add(expandedArgs[++i]); |
50908 | 157 |
} else if (each.contentEquals("-JUnitVeryVerbose")) { |
58523
fb3d408c7a7e
8231903: [Graal] Update com.oracle.mxtool.junit classes
epavlova
parents:
50908
diff
changeset
|
158 |
config.verbose = true; |
50908 | 159 |
config.veryVerbose = true; |
58523
fb3d408c7a7e
8231903: [Graal] Update com.oracle.mxtool.junit classes
epavlova
parents:
50908
diff
changeset
|
160 |
config.enableTiming = true; |
50908 | 161 |
} else if (each.contentEquals("-JUnitFailFast")) { |
162 |
config.failFast = true; |
|
163 |
} else if (each.contentEquals("-JUnitEnableTiming")) { |
|
164 |
config.enableTiming = true; |
|
165 |
} else if (each.contentEquals("-JUnitColor")) { |
|
166 |
config.color = true; |
|
167 |
} else if (each.contentEquals("-JUnitEagerStackTrace")) { |
|
168 |
config.eagerStackTrace = true; |
|
169 |
} else if (each.contentEquals("-JUnitGCAfterTest")) { |
|
170 |
config.gcAfterTest = true; |
|
171 |
} else if (each.contentEquals("-JUnitRecordResults")) { |
|
172 |
config.recordResults = true; |
|
173 |
} else if (each.contentEquals("-JUnitRepeat")) { |
|
174 |
if (i + 1 >= expandedArgs.length) { |
|
175 |
system.out().println("Must include argument for -JUnitRepeat"); |
|
176 |
System.exit(1); |
|
177 |
} |
|
178 |
try { |
|
179 |
config.repeatCount = Integer.parseInt(expandedArgs[++i]); |
|
180 |
} catch (NumberFormatException e) { |
|
181 |
system.out().println("Expected integer argument for -JUnitRepeat. Found: " + expandedArgs[i]); |
|
182 |
System.exit(1); |
|
183 |
} |
|
184 |
} else { |
|
185 |
system.out().println("Unknown command line argument: " + each); |
|
186 |
} |
|
187 |
||
188 |
} else { |
|
58523
fb3d408c7a7e
8231903: [Graal] Update com.oracle.mxtool.junit classes
epavlova
parents:
50908
diff
changeset
|
189 |
testSpecs.add(each); |
50908 | 190 |
} |
191 |
i++; |
|
192 |
} |
|
193 |
||
58523
fb3d408c7a7e
8231903: [Graal] Update com.oracle.mxtool.junit classes
epavlova
parents:
50908
diff
changeset
|
194 |
ModuleSupport moduleSupport = new ModuleSupport(system.out()); |
fb3d408c7a7e
8231903: [Graal] Update com.oracle.mxtool.junit classes
epavlova
parents:
50908
diff
changeset
|
195 |
Set<String> opened = new TreeSet<>(); |
fb3d408c7a7e
8231903: [Graal] Update com.oracle.mxtool.junit classes
epavlova
parents:
50908
diff
changeset
|
196 |
Set<String> exported = new TreeSet<>(); |
fb3d408c7a7e
8231903: [Graal] Update com.oracle.mxtool.junit classes
epavlova
parents:
50908
diff
changeset
|
197 |
for (String spec : openPackagesSpecs) { |
fb3d408c7a7e
8231903: [Graal] Update com.oracle.mxtool.junit classes
epavlova
parents:
50908
diff
changeset
|
198 |
moduleSupport.openPackages(spec, "-JUnitOpenPackages", opened, exported); |
fb3d408c7a7e
8231903: [Graal] Update com.oracle.mxtool.junit classes
epavlova
parents:
50908
diff
changeset
|
199 |
} |
50908 | 200 |
|
58523
fb3d408c7a7e
8231903: [Graal] Update com.oracle.mxtool.junit classes
epavlova
parents:
50908
diff
changeset
|
201 |
for (String spec : testSpecs) { |
fb3d408c7a7e
8231903: [Graal] Update com.oracle.mxtool.junit classes
epavlova
parents:
50908
diff
changeset
|
202 |
try { |
fb3d408c7a7e
8231903: [Graal] Update com.oracle.mxtool.junit classes
epavlova
parents:
50908
diff
changeset
|
203 |
builder.addTestSpec(spec); |
fb3d408c7a7e
8231903: [Graal] Update com.oracle.mxtool.junit classes
epavlova
parents:
50908
diff
changeset
|
204 |
} catch (MxJUnitRequest.BuilderException ex) { |
fb3d408c7a7e
8231903: [Graal] Update com.oracle.mxtool.junit classes
epavlova
parents:
50908
diff
changeset
|
205 |
system.out().println(ex.getMessage()); |
fb3d408c7a7e
8231903: [Graal] Update com.oracle.mxtool.junit classes
epavlova
parents:
50908
diff
changeset
|
206 |
System.exit(1); |
fb3d408c7a7e
8231903: [Graal] Update com.oracle.mxtool.junit classes
epavlova
parents:
50908
diff
changeset
|
207 |
} |
fb3d408c7a7e
8231903: [Graal] Update com.oracle.mxtool.junit classes
epavlova
parents:
50908
diff
changeset
|
208 |
} |
fb3d408c7a7e
8231903: [Graal] Update com.oracle.mxtool.junit classes
epavlova
parents:
50908
diff
changeset
|
209 |
|
fb3d408c7a7e
8231903: [Graal] Update com.oracle.mxtool.junit classes
epavlova
parents:
50908
diff
changeset
|
210 |
MxJUnitRequest request = builder.build(); |
fb3d408c7a7e
8231903: [Graal] Update com.oracle.mxtool.junit classes
epavlova
parents:
50908
diff
changeset
|
211 |
moduleSupport.processAddExportsAnnotations(request.classes, opened, exported); |
fb3d408c7a7e
8231903: [Graal] Update com.oracle.mxtool.junit classes
epavlova
parents:
50908
diff
changeset
|
212 |
|
fb3d408c7a7e
8231903: [Graal] Update com.oracle.mxtool.junit classes
epavlova
parents:
50908
diff
changeset
|
213 |
if (!opened.isEmpty()) { |
fb3d408c7a7e
8231903: [Graal] Update com.oracle.mxtool.junit classes
epavlova
parents:
50908
diff
changeset
|
214 |
System.setProperty(OPENED_PACKAGES_PROPERTY_NAME, String.join(System.lineSeparator(), opened)); |
fb3d408c7a7e
8231903: [Graal] Update com.oracle.mxtool.junit classes
epavlova
parents:
50908
diff
changeset
|
215 |
} |
fb3d408c7a7e
8231903: [Graal] Update com.oracle.mxtool.junit classes
epavlova
parents:
50908
diff
changeset
|
216 |
if (!exported.isEmpty()) { |
fb3d408c7a7e
8231903: [Graal] Update com.oracle.mxtool.junit classes
epavlova
parents:
50908
diff
changeset
|
217 |
System.setProperty(EXPORTED_PACKAGES_PROPERTY_NAME, String.join(System.lineSeparator(), exported)); |
50908 | 218 |
} |
219 |
||
220 |
for (RunListener p : ServiceLoader.load(RunListener.class)) { |
|
221 |
junitCore.addListener(p); |
|
222 |
} |
|
223 |
||
224 |
Result result = runRequest(junitCore, system, config, request); |
|
225 |
System.exit(result.wasSuccessful() ? 0 : 1); |
|
226 |
} |
|
227 |
||
228 |
private static PrintStream openFile(JUnitSystem system, String name) { |
|
229 |
File file = new File(name).getAbsoluteFile(); |
|
230 |
try { |
|
231 |
FileOutputStream fos = new FileOutputStream(file); |
|
232 |
return new PrintStream(fos, true); |
|
233 |
} catch (FileNotFoundException e) { |
|
234 |
system.out().println("Could not open " + file + " for writing: " + e); |
|
235 |
System.exit(1); |
|
236 |
return null; |
|
237 |
} |
|
238 |
} |
|
239 |
||
240 |
public static Result runRequest(JUnitCore junitCore, JUnitSystem system, MxJUnitConfig config, MxJUnitRequest mxRequest) { |
|
241 |
final TextRunListener textListener; |
|
242 |
if (config.veryVerbose) { |
|
243 |
textListener = new VerboseTextListener(system, mxRequest.classes.size(), VerboseTextListener.SHOW_ALL_TESTS); |
|
244 |
} else if (config.verbose) { |
|
245 |
textListener = new VerboseTextListener(system, mxRequest.classes.size()); |
|
246 |
} else { |
|
247 |
textListener = new TextRunListener(system); |
|
248 |
} |
|
249 |
TimingDecorator timings = config.enableTiming ? new TimingDecorator(textListener) : null; |
|
250 |
MxRunListener mxListener = config.enableTiming ? timings : textListener; |
|
251 |
||
252 |
if (config.color) { |
|
253 |
mxListener = new AnsiTerminalDecorator(mxListener); |
|
254 |
} |
|
255 |
if (config.eagerStackTrace) { |
|
256 |
mxListener = new EagerStackTraceDecorator(mxListener); |
|
257 |
} |
|
258 |
if (config.gcAfterTest) { |
|
259 |
mxListener = new GCAfterTestDecorator(mxListener); |
|
260 |
} |
|
261 |
if (config.recordResults) { |
|
262 |
PrintStream passed = openFile(system, "passed.txt"); |
|
263 |
PrintStream failed = openFile(system, "failed.txt"); |
|
264 |
mxListener = new TestResultLoggerDecorator(passed, failed, mxListener); |
|
265 |
} |
|
266 |
||
267 |
junitCore.addListener(TextRunListener.createRunListener(mxListener)); |
|
268 |
||
269 |
Request request = mxRequest.getRequest(); |
|
270 |
if (mxRequest.methodName == null) { |
|
271 |
if (config.failFast) { |
|
272 |
Runner runner = request.getRunner(); |
|
273 |
if (runner instanceof ParentRunner) { |
|
274 |
ParentRunner<?> parentRunner = (ParentRunner<?>) runner; |
|
275 |
parentRunner.setScheduler(new RunnerScheduler() { |
|
276 |
public void schedule(Runnable childStatement) { |
|
277 |
if (textListener.getLastFailure() == null) { |
|
278 |
childStatement.run(); |
|
279 |
} |
|
280 |
} |
|
281 |
||
282 |
public void finished() { |
|
283 |
} |
|
284 |
}); |
|
285 |
} else { |
|
286 |
system.out().println("Unexpected Runner subclass " + runner.getClass().getName() + " - fail fast not supported"); |
|
287 |
} |
|
288 |
} |
|
289 |
} else { |
|
290 |
if (config.failFast) { |
|
291 |
system.out().println("Single method selected - fail fast not supported"); |
|
292 |
} |
|
293 |
} |
|
294 |
||
295 |
if (config.repeatCount != 1) { |
|
296 |
request = new RepeatingRequest(request, config.repeatCount); |
|
297 |
} |
|
298 |
||
299 |
if (config.enableTiming) { |
|
300 |
Runtime.getRuntime().addShutdownHook(new Thread() { |
|
301 |
@Override |
|
302 |
public void run() { |
|
303 |
printTimings(timings); |
|
304 |
} |
|
305 |
}); |
|
306 |
} |
|
307 |
||
308 |
Result result = junitCore.run(request); |
|
309 |
for (Failure each : mxRequest.missingClasses) { |
|
310 |
result.getFailures().add(each); |
|
311 |
} |
|
312 |
||
313 |
return result; |
|
314 |
} |
|
315 |
||
316 |
private static class Timing<T> implements Comparable<Timing<T>> { |
|
317 |
final T subject; |
|
318 |
final long value; |
|
319 |
||
320 |
Timing(T subject, long value) { |
|
321 |
this.subject = subject; |
|
322 |
this.value = value; |
|
323 |
} |
|
324 |
||
325 |
public int compareTo(Timing<T> o) { |
|
326 |
if (this.value < o.value) { |
|
327 |
return -1; |
|
328 |
} |
|
329 |
if (this.value > o.value) { |
|
330 |
return 1; |
|
331 |
} |
|
332 |
return 0; |
|
333 |
} |
|
334 |
} |
|
335 |
||
336 |
// Should never need to customize so using a system property instead |
|
337 |
// of a command line option for customization is fine. |
|
338 |
private static final int TIMINGS_TO_PRINT = Integer.getInteger("mx.junit.timings_to_print", 10); |
|
339 |
||
340 |
private static void printTimings(TimingDecorator timings) { |
|
341 |
if (TIMINGS_TO_PRINT != 0) { |
|
342 |
List<Timing<Class<?>>> classTimes = new ArrayList<>(timings.classTimes.size()); |
|
343 |
List<Timing<Description>> testTimes = new ArrayList<>(timings.testTimes.size()); |
|
344 |
for (Map.Entry<Class<?>, Long> e : timings.classTimes.entrySet()) { |
|
345 |
classTimes.add(new Timing<>(e.getKey(), e.getValue())); |
|
346 |
} |
|
347 |
for (Map.Entry<Description, Long> e : timings.testTimes.entrySet()) { |
|
348 |
testTimes.add(new Timing<>(e.getKey(), e.getValue())); |
|
349 |
} |
|
350 |
classTimes.sort(Collections.reverseOrder()); |
|
351 |
testTimes.sort(Collections.reverseOrder()); |
|
352 |
||
353 |
System.out.println(); |
|
354 |
System.out.printf("%d longest running test classes:%n", TIMINGS_TO_PRINT); |
|
355 |
for (int i = 0; i < TIMINGS_TO_PRINT && i < classTimes.size(); i++) { |
|
356 |
Timing<Class<?>> timing = classTimes.get(i); |
|
357 |
System.out.printf(" %,10d ms %s%n", timing.value, timing.subject.getName()); |
|
358 |
} |
|
359 |
System.out.printf("%d longest running tests:%n", TIMINGS_TO_PRINT); |
|
360 |
for (int i = 0; i < TIMINGS_TO_PRINT && i < testTimes.size(); i++) { |
|
361 |
Timing<Description> timing = testTimes.get(i); |
|
362 |
System.out.printf(" %,10d ms %s%n", timing.value, timing.subject); |
|
363 |
} |
|
364 |
Object[] current = timings.getCurrentTestDuration(); |
|
365 |
if (current != null) { |
|
366 |
System.out.printf("Test %s not finished after %d ms%n", current[0], current[1]); |
|
367 |
} |
|
368 |
||
369 |
} |
|
370 |
} |
|
371 |
||
372 |
/** |
|
373 |
* Expand any arguments starting with @ and return the resulting argument array. |
|
374 |
* |
|
375 |
* @return the expanded argument array |
|
376 |
*/ |
|
377 |
private static String[] expandArgs(String[] args) { |
|
378 |
List<String> result = null; |
|
379 |
for (int i = 0; i < args.length; i++) { |
|
380 |
String arg = args[i]; |
|
381 |
if (arg.length() > 0 && arg.charAt(0) == '@') { |
|
382 |
if (result == null) { |
|
383 |
result = new ArrayList<>(); |
|
384 |
for (int j = 0; j < i; j++) { |
|
385 |
result.add(args[j]); |
|
386 |
} |
|
387 |
expandArg(arg.substring(1), result); |
|
388 |
} |
|
389 |
} else if (result != null) { |
|
390 |
result.add(arg); |
|
391 |
} |
|
392 |
} |
|
393 |
return result != null ? result.toArray(new String[0]) : args; |
|
394 |
} |
|
395 |
||
396 |
/** |
|
397 |
* Add each line from {@code filename} to the list {@code args}. |
|
398 |
*/ |
|
399 |
private static void expandArg(String filename, List<String> args) { |
|
400 |
BufferedReader br = null; |
|
401 |
try { |
|
402 |
br = new BufferedReader(new FileReader(filename)); |
|
403 |
||
404 |
String buf; |
|
405 |
while ((buf = br.readLine()) != null) { |
|
406 |
args.add(buf); |
|
407 |
} |
|
408 |
br.close(); |
|
409 |
} catch (IOException ioe) { |
|
410 |
ioe.printStackTrace(); |
|
411 |
System.exit(2); |
|
412 |
} finally { |
|
413 |
try { |
|
414 |
if (br != null) { |
|
415 |
br.close(); |
|
416 |
} |
|
417 |
} catch (IOException ioe) { |
|
418 |
ioe.printStackTrace(); |
|
419 |
System.exit(3); |
|
420 |
} |
|
421 |
} |
|
422 |
} |
|
423 |
} |