CompileFail.java revision 1048
1048N/A/*
1048N/A * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
1048N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1048N/A *
1048N/A * This code is free software; you can redistribute it and/or modify it
1048N/A * under the terms of the GNU General Public License version 2 only, as
1048N/A * published by the Free Software Foundation.
1048N/A *
1048N/A * This code is distributed in the hope that it will be useful, but WITHOUT
1048N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1048N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
1048N/A * version 2 for more details (a copy is included in the LICENSE file that
1048N/A * accompanied this code).
1048N/A *
1048N/A * You should have received a copy of the GNU General Public License version
1048N/A * 2 along with this work; if not, write to the Free Software Foundation,
1048N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1048N/A *
1048N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1048N/A * or visit www.oracle.com if you need additional information or have any
1048N/A * questions.
1048N/A */
1048N/A
1048N/Aimport java.io.*;
1048N/Aimport java.util.*;
1048N/A
1048N/A/*
1048N/A * Utility class to emulate jtreg @compile/fail, but also checking the specific
1048N/A * exit code, given as the first arg.
1048N/A */
1048N/Apublic class CompileFail {
1048N/A public static void main(String... args) {
1048N/A if (args.length < 2)
1048N/A throw new IllegalArgumentException("insufficient args");
1048N/A int expected_rc = getReturnCode(args[0]);
1048N/A
1048N/A List<String> javacArgs = new ArrayList<>();
1048N/A javacArgs.addAll(Arrays.asList(
1048N/A "-bootclasspath", System.getProperty("sun.boot.class.path"),
1048N/A "-d", "."
1048N/A ));
1048N/A
1048N/A File testSrc = new File(System.getProperty("test.src"));
1048N/A for (int i = 1; i < args.length; i++) {
1048N/A String arg = args[i];
1048N/A if (arg.endsWith(".java"))
1048N/A javacArgs.add(new File(testSrc, arg).getPath());
1048N/A else
1048N/A javacArgs.add(arg);
1048N/A }
1048N/A
1048N/A int rc = com.sun.tools.javac.Main.compile(
1048N/A javacArgs.toArray(new String[javacArgs.size()]));
1048N/A
1048N/A if (rc != expected_rc)
1048N/A throw new Error("unexpected exit code: " + rc
1048N/A + ", expected: " + expected_rc);
1048N/A }
1048N/A
1048N/A static int getReturnCode(String name) {
1048N/A switch (name) {
1048N/A case "OK":
1048N/A return EXIT_OK;
1048N/A
1048N/A case "ERROR":
1048N/A return EXIT_ERROR;
1048N/A
1048N/A case "CMDERR":
1048N/A return EXIT_CMDERR;
1048N/A
1048N/A case "SYSERR":
1048N/A return EXIT_SYSERR;
1048N/A
1048N/A case "ABNORMAL":
1048N/A return EXIT_ABNORMAL;
1048N/A
1048N/A default:
1048N/A throw new IllegalArgumentException(name);
1048N/A }
1048N/A }
1048N/A
1048N/A // The following is cut-n-paste from com.sun.tools.javac.main.Main
1048N/A static final int
1048N/A EXIT_OK = 0, // Compilation completed with no errors.
1048N/A EXIT_ERROR = 1, // Completed but reported errors.
1048N/A EXIT_CMDERR = 2, // Bad command-line arguments
1048N/A EXIT_SYSERR = 3, // System error or resource exhaustion.
1048N/A EXIT_ABNORMAL = 4; // Compiler terminated abnormally
1048N/A}