1 /*
2  * Copyright (c) 2011, 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 
24 import java.io.*;
25 import java.util.*;
26 import com.sun.tools.javac.main.Main;
27 
28 /*
29  * Utility class to emulate jtreg @compile/fail, but also checking the specific
30  * exit code, given as the first arg.
31  */
32 public class CompileFail {
main(String... args)33     public static void main(String... args) {
34         if (args.length < 2)
35             throw new IllegalArgumentException("insufficient args");
36         int expected_rc = getReturnCode(args[0]);
37 
38         List<String> javacArgs = new ArrayList<>();
39         javacArgs.addAll(Arrays.asList(
40             "-bootclasspath", System.getProperty("sun.boot.class.path"),
41             "-d", "."
42         ));
43 
44         File testSrc = new File(System.getProperty("test.src"));
45         for (int i = 1; i < args.length; i++) { // skip first arg
46             String arg = args[i];
47             if (arg.endsWith(".java"))
48                 javacArgs.add(new File(testSrc, arg).getPath());
49             else
50                 javacArgs.add(arg);
51         }
52 
53         int rc = com.sun.tools.javac.Main.compile(
54             javacArgs.toArray(new String[javacArgs.size()]));
55 
56         if (rc != expected_rc)
57             throw new Error("unexpected exit code: " + rc
58                         + ", expected: " + expected_rc);
59     }
60 
getReturnCode(String name)61     static int getReturnCode(String name) {
62         return Main.Result.valueOf(name).exitCode;
63     }
64 
65 }
66